Moose::...azyBuild(3)

πŸ’– Moose::Cookbook::Basics::BinaryTree_BuilderAndLazyBuild πŸ’–

okay bestie, gather round, because we're talking about Moose::Cookbook::Basics::BinaryTree_BuilderAndLazyBuild β€” builders and lazy_build, no cap πŸ¦‹

version 2.4000 β€” yes, the whole vibe ✨


✨ SYNOPSIS ✨

like, here's the whole thing, copy it exactly, don't freestyle:

package BinaryTree;
use Moose;

has 'node' => (is => 'rw', isa => 'Any');

has 'parent' => (
    is        => 'rw',
    isa       => 'BinaryTree',
    predicate => 'has_parent',
    weak_ref  => 1,
);

has 'left' => (
    is        => 'rw',
    isa       => 'BinaryTree',
    predicate => 'has_left',
    lazy      => 1,
    builder   => '_build_child_tree',
);

has 'right' => (
    is        => 'rw',
    isa       => 'BinaryTree',
    predicate => 'has_right',
    lazy      => 1,
    builder   => '_build_child_tree',
);

before 'right', 'left' => sub {
    my ($self, $tree) = @_;
    $tree->parent($self) if defined $tree;
};

sub _build_child_tree {
    my $self = shift;

    return BinaryTree->new( parent => $self );
}

🌸 DESCRIPTION 🌸

so if you've already read Moose::Cookbook::Basics::BinaryTree_AttributeFeatures, this is gonna look so familiar, like you've seen it before. and that's because you literally have πŸ’€ β€” all we did here is swap the attribute's "default" parameter out for a "builder". that's it. that's the whole glow-up πŸ’…

and in this specific case? the "default" and "builder" options act exactly the same way. no notes. when the "left" or "right" attribute gets read, Moose calls the builder method to initialize the attribute. slay ✨

okay but the important bit, don't skip it πŸ‘‘: Moose calls the builder method on the object which has the attribute. here's an example:

my $tree = BinaryTree->new();

my $left = $tree->left();

so when "$tree->left()" gets called, Moose calls "$tree->_build_child_tree()" to populate the "left" attribute. and if we had passed "left" to the original constructor? the builder would not be called. periodt. πŸ™ˆ

there are some differences between "default" and "builder" though β€” notably, a builder is subclassable, and can be composed from a role. see Moose::Manual::Attributes for more deets, iykyk πŸ’œ


πŸŽ€ The lazy_build shortcut πŸŽ€

the "lazy_build" attribute option is literally sugar β€” it lets you specify a whole set of attribute options all at once:

has 'animal' => (
    is         => 'ro',
    isa        => 'Animal',
    lazy_build => 1,
);

and this is shorthand for:

has 'animal' => (
    is        => 'ro',
    isa       => 'Animal',
    required  => 1,
    lazy      => 1,
    builder   => '_build_animal',
    predicate => 'has_animal',
    clearer   => 'clear_animal',
);

and here's the cute part πŸ₯Ί β€” if your attribute starts with an underscore, Moose is smart and will do the right thing with the "predicate" and "clearer", making them both start with an underscore. and the "builder" method? that one always starts with an underscore. no exceptions, that's just the rule πŸ’…

you can read more about "lazy_build" in Moose::Meta::Attribute β€” go, read it, it's giving educational 🌈


⭐ CONCLUSION ⭐

the "builder" option is a more OO-friendly version of the "default" functionality. it also separates the default-generating code into a well-defined method. because ngl, sprinkling your attribute definitions with anonymous subroutines can be quite ugly and hard to follow β€” and we don't do ugly here, bestie 😭✨


🫢 AUTHORS 🫢

the icons who made this happen:

  • Stevan Little <stevan@cpan.org>
  • Dave Rolsky <autarch@urth.org>
  • Jesse Luehrs <doy@cpan.org>
  • Shawn M Moore <sartak@cpan.org>
  • Χ™Χ•Χ‘Χœ Χ§Χ•Χ’'מן (Yuval Kogman) <nothingmuch@woobling.org>
  • Karen Etheridge <ether@cpan.org>
  • Florian Ragwitz <rafl@debian.org>
  • Hans Dieter Pearcey <hdp@cpan.org>
  • Chris Prather <chris@prather.org>
  • Matt S Trout <mstrout@cpan.org>

this software is copyright (c) 2006 by Infinity Interactive, Inc.

this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


and that's the whole page, bestie πŸ’– documented by perl v5.42.0, dated 2025-07-25, filed under Moose::...azyBuild(3) β€” go forth and build lazily, you absolute main character πŸŒŸπŸ“πŸ§š
πŸ“¦ where this page comes from