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>
π COPYRIGHT AND LICENSE π
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
- fedora 43: perl-Moose 2.4000-4.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms