NAME
Object::Lazy - create objects late from non-owned classes
VERSION
0.01
SYNOPSIS
use Foo 123; # because the class of the real object is Foo, version could be 123
use Object::Lazy;
my $foo = Object::Lazy->new(
sub{
return Foo->new();
},
);
bar($foo);
sub bar {
my $foo = shift;
if ($condition) {
# a foo object will be created
print $foo->output();
}
else {
# foo object is not created
}
return;
}
To combine this and a lazy use, write somthing like that:
use Object::Lazy;
my $foo = Object::Lazy->new(
sub{
my $code = 'use Foo 123';
eval $code;
$@ and die "$code $@";
return Foo->new();
},
);
# and so on
Read topic SUBROUTINES/METHODS to find the entended constructor and all the optional parameters.
DESCRIPTION
This module implements 'lazy evaluation' and can create lazy objects from every class.
Creates a dummy object including a subroutine which knows how to build the real object.
Later, if a method of the object is called, the real object will be built.
Method isa and method can is implemented.
SUBROUTINES/METHODS
method new
short constructor
$object = Object::Lazy->new(sub{
return RealClass->new(...);
});
extended constructor
$object = Object::Lazy->new({
build => sub {
return RealClass->new(...);
},
});
optional parameter isa
There are 3 ways to check the class or inheritance.
If there is no parameter isa, the object must be built before.
If the
use RealClass;
is outside ofbuild =
sub {...}> then the class methodRealClass-
isa(...);> checks the class or inheritance.Otherwise the isa parameter is a full notation of the class and possible of the inheritance.
$object = Object::Lazy->new({ ... isa => 'RealClass', );
or
$object = Object::Lazy->new({ ... isa => [qw(RealClass BaseClassOfRealClass)], );
optional parameter logger
Optional notation of the logger code to show the build process.
$object = Object::Lazy->new({ ... logger => sub { my $at_stack = shift; return qq{Object "RealClass" built $at_stack} }, );
optional parameter ref
Optional notation of the ref answer.
use Scalar::Lazy::Ref; # overwrite CORE::GLOBAL::ref $object = Object::Lazy->new({ ... ref => 'RealClass', ); $boolean_true = ref $object eq 'RealClass';
method isa
If no isa parameter was given at method new, the object will build.
Otherwise the method isa checks by isa class method or only the given parameters.
$boolean = $obejct->isa('RealClass');
or
$boolean = $obejct->isa('BaseClassOfRealClass');
method can
The object will build. After that the can method checks the built object.
$coderef_or_undef = $object->can('method');
DIAGNOSTICS
The constructor can confess at false parameters.
CONFIGURATION AND ENVIRONMENT
nothing
DEPENDENCIES
nothing
INCOMPATIBILITIES
not known
BUGS AND LIMITATIONS
not known
SEE ALSO
Data::Lazy The scalar will be built at my $scalar = shift;
at first sub call.
Scalar::Defer The scalar will be built at my $scalar = shift;
at first sub call.
Class::LazyObject No, I don't write my own class/package.
Object::Realize::Later No, I don't write my own class/package.
Class::Cache There are lazy parameters too.
Object::Trampoline This is nearly the same idea.
Objects::Collection::Object Object created at call of method isa.
AUTHOR
Steffen Winkler
LICENSE AND COPYRIGHT
Copyright (c) 2007, Steffen Winkler <steffenw@cpan.org>
. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.