Object::Lazy(3pm)

πŸ“¦
version 0.16 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

✨ 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 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 DOES β€” It is similar to parameter isa. 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 the VERSION method tell Object::Lazy which 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 the VERSION method tell Object::Lazy which 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 🧚

  • Carp
  • Try::Tiny
  • Object::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 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 πŸ’œ


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