PrePAN

Sign in to PrePAN

MooX::LazyArgs Arguments to lazy attribute construction taken from constructor

Author
torbjorn@github
Date
URL
Status
In Review
Good

Synopsis

{
    package Foo;

    use Moo;

    has an_object => (
        is => "ro",
        predicate => 1,
        lazy_args => [qw/bar baz/],
    );

    has bar => (
        is => "rw",
    );

}

## in main:
my $foo = Foo->new( bar => 2, baz => 3 );

$foo->bar;                      # "2", as normal

$foo->has_an_object;            # undef, as normal

$foo->an_object;                # now it gets built, having set aside
                                # bar and baz from the constructor for
                                # its own use

                                # now if an_object use these as attributes:
$foo->an_object->bar;           # 2
$foo->an_object->baz;           # 3

Description

A more sophisticated lazy construction, the sample code pretty much says it all. The module name is perhaps not 100% presize, it does not reflect that the lazy args come from constructor.

Comments

Hm forgot to throw in class name of an_object, perhaps the first arg of lazy_args
This one went into the fast lane and was released last night as https://metacpan.org/pod/MooX::ObjectBuilder

Please sign up to post a review.