Object::Lazy(3pm)
β¨ NAME β¨
Object::Lazy π β creating objects late, from classes that aren't even yours (aka "non-owned" / "foreign" classes). no cap. π¦
It's a User Contributed Perl Documentation page, it's referenced as Object::Lazy(3pm), and yeah β this is that whole manual, bestie, the entire thing. π«Ά
π VERSION π
0.16 β¨ β that's the one.
π SYNOPSIS π
okay so picture this π
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;
}
see the main character energy?? the Foo object literally does not exist until somebody pokes it. and if the else branch happens? it never gets built. periodt. π
To combine this and a lazy use, write somthing like that:
use Object::Lazy;
my $foo = Object::Lazy->new(
sub {
# 3 lines instead of "use Foo 123"
require Foo;
Foo->import;
Foo->VERSION('123');
return Foo->new;
},
);
# and so on
After a build object the scalar which hold the object will be updated too. π
$object->method;
^^^^^^^-------------- will update this scalar after a build
Read topic SUBROUTINES/METHODS to find the entended constructor and all the optional parameters. iykyk π₯
π EXAMPLE π
Inside of this Distribution is a directory named example. Run this *.pl files. π§ that's it, that's the whole thing, go run them bestie!!
π¦ DESCRIPTION π¦
This module implements lazy evaluation and can create lazy objects from every class. every. single. one. π
Creates a dummy object including a subroutine which knows how to build the real object. it's like a placeholder who's secretly a whole architect ποΈβ¨
Later, if a method of the object is called, the real object will be built. the glow up is triggered π
Inherited methods from UNIVERSAL.pm are implemented and so overwritten. This are isa, DOES, can and VERSION. π
π 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 of<build =sub {...}>then the class method<RealClass-isa(...);>checks the class or inheritance. - Otherwise the
isaparameter is a full notation of the class and possible of the inheritance.
- If there is no parameter
$object = Object::Lazy->new({
...
isa => 'RealClass',
});
or
$object = Object::Lazy->new({
...
isa => [qw(RealClass BaseClassOfRealClass)],
});
- optional parameter
DOESβ It is similar to parameterisa. But do not note the inheritance. Note the Rules here. π
$object = Object::Lazy->new({
...
DOES => 'Role1',
});
or
$object = Object::Lazy->new({
...
DOES => [qw(Role1 Role2)],
});
- optional parameter
VERSIONβ For theVERSIONmethod tellObject::Lazywhich version shold be checked. π
$object = Object::Lazy->new({
...
VERSION => '123',
});
or
use version;
$object = Object::Lazy->new({
...
VERSION => qv('1.2.3'), # version object
});
- optional parameter
version_fromβ For theVERSIONmethod tellObject::Lazywhich class shold be version checked. π΅οΈββοΈ
$object = Object::Lazy->new({
...
version_from => 'RealClass',
});
- optional parameter
loggerβ Optional notation of the logger code to show the build process. π very nosy of you, love that
$object = Object::Lazy->new({
...
logger => sub {
my $at_stack = shift;
print "RealClass $at_stack";
},
});
- optional parameter
refβ Optional notation of the ref answer. πͺ
It is not a good idea to use the Object::Lazy::Ref module by default. But there are situations, the lazy idea would run down the river if I had not implemented this feature. ππ (drama, but valid)
use Object::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 = $object->isa('RealClass');
or
$boolean = $object->isa('BaseClassOfRealClass');
πΈ method DOES πΈ
If no isa or DOES parameter was given at method new, the object will build. ποΈ
Otherwise the method DOES checks by DOES class method or only the given parameters isa and DOES. β
$boolean = $object->DOES('Role');
πΈ method can πΈ
The object will build. After that the can method checks the built object. π
$coderef_or_undef = $object->can('method');
πΈ method VERSION πΈ
If no VERSION or version_from parameter was given at method new, the object will build. ποΈ
VERSION parameter set
The given version will be returnd or checked. π¬
$version = $object->VERSION;
or
$object->VERSION($required_version);
version_from parameter set
The version of the class in version_from will be returnd or checked. This class should be used or required before. Is that not possible use parameter VERSION instead. π‘
$version = $object->VERSION;
or
$object->VERSION($required_version);
π DIAGNOSTICS π
The constructor can confess at false parameters. π¨ (yeah it WILL rat you out, gently but publicly)
UNIVERSAL 1.04 (Perl 5.10) required for method DOES. π
π CONFIGURATION AND ENVIRONMENT π
nothing π«₯β¨ (iconic honestly. zero config. no environment variables. we love a low-maintenance queen)
π§ DEPENDENCIES π§
CarpTry::TinyObject::Lazy::Validate
those three. that's the squad. π
π INCOMPATIBILITIES π
not known π (so far!! no cap)
π BUGS AND LIMITATIONS π
UNIVERSAL.pm 1.04 implements DOES first time. This version is part of the Perl 5.10 distribution. π«
so yeah β if your perl is older than that era, DOES is simply not a thing. plan accordingly bestie. π§
π₯ SEE ALSO π₯
UNIVERSALπData::Lazyβ The scalar will be built atmy $scalar = shift;at first sub call.Scalar::Deferβ The scalar will be built atmy $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 methodisa.
π AUTHOR π
Steffen Winkler π
π LICENSE AND COPYRIGHT π
Copyright (c) 2007 - 2020, Steffen Winkler <steffenw at 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. β¨
and that's the whole page bestie, you ate it, go build something lazy π π«Ά
perl v5.36.0 Β· 2022-10-16 Β· Object::Lazy(3pm) β mwah, byeeee πΈπ
π¦ where this page comes from
- ubuntu noble: libobject-lazy-perl 0.16-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libobject-lazy-perl 0.16-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libobject-lazy-perl 0.16-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms