# HG changeset patch # User sergey # Date 1360854842 -14400 # Node ID f2a6bc5f3184da854ce94a59f628541d14156f37 # Parent 2f06250bab5f3f958c90a1b5547c3f97d8121189 +IMPL::Object::InlineFactory: implement object factory as subroutine diff -r 2f06250bab5f -r f2a6bc5f3184 Lib/IMPL/Object/InlineFactory.pm --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/IMPL/Object/InlineFactory.pm Thu Feb 14 19:14:02 2013 +0400 @@ -0,0 +1,44 @@ +package IMPL::Object::InlineFactory; +use strict; +use Carp qw(croak); + +sub new { + my $self = shift; + if(ref $self) { + return &$$self(@_); + } else { + my $factory = shift; + + croak "A code reference is required" + unless ref $factory eq 'CODE'; + + return bless \$factory, $self; + } +} + +1; + +__END__ + +=pod + +=head1 NAME + +C - реализация фабрики на основе процедуры. + +=head1 SYNOPSIS + +=begin code + +use IMPL::require { + InlineFactory => 'IMPL::Object::InlineFactory', + Foo => 'My::App::Foo' +}; + +my $factory = InlineFactory->new(sub { Foo->new(mode => 'direct', @_) }); + +my $obj = $factory->new(timeout => 10); # Foo->new(mode => 'direct', timeout => 10); + +=end code + +=cut \ No newline at end of file