{"id":6644,"date":"2022-12-20T19:19:32","date_gmt":"2022-12-20T22:19:32","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/moosecookbookbasicsbinarytree_builderandlazybuild-man3\/"},"modified":"2022-12-20T19:19:32","modified_gmt":"2022-12-20T22:19:32","slug":"moosecookbookbasicsbinarytree_builderandlazybuild-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/moosecookbookbasicsbinarytree_builderandlazybuild-man3\/","title":{"rendered":"Moose::Cookbook::Basics::BinaryTree_BuilderAndLazyBuild (man3)"},"content":{"rendered":"<h1 align=\"center\">Moose::Cookbook::Basics::BinaryTree_BuilderAndLazyBuild<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#VERSION\">VERSION<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#CONCLUSION\">CONCLUSION<\/a><br \/> <a href=\"#AUTHORS\">AUTHORS<\/a><br \/> <a href=\"#COPYRIGHT AND LICENSE\">COPYRIGHT AND LICENSE<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Moose::Cookbook::Basics::BinaryTree_BuilderAndLazyBuild \u2212 Builder methods and lazy_build<\/p>\n<h2>VERSION <a name=\"VERSION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">version 2.2014<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">package BinaryTree; <br \/> use Moose; <br \/> has &#8216;node&#8217; => (is => &#8216;rw&#8217;, isa => &#8216;Any&#8217;); <br \/> has &#8216;parent&#8217; => ( <br \/> is => &#8216;rw&#8217;, <br \/> isa => &#8216;BinaryTree&#8217;, <br \/> predicate => &#8216;has_parent&#8217;, <br \/> weak_ref => 1, <br \/> ); <br \/> has &#8216;left&#8217; => ( <br \/> is => &#8216;rw&#8217;, <br \/> isa => &#8216;BinaryTree&#8217;, <br \/> predicate => &#8216;has_left&#8217;, <br \/> lazy => 1, <br \/> builder => &#8216;_build_child_tree&#8217;, <br \/> ); <br \/> has &#8216;right&#8217; => ( <br \/> is => &#8216;rw&#8217;, <br \/> isa => &#8216;BinaryTree&#8217;, <br \/> predicate => &#8216;has_right&#8217;, <br \/> lazy => 1, <br \/> builder => &#8216;_build_child_tree&#8217;, <br \/> ); <br \/> before &#8216;right&#8217;, &#8216;left&#8217; => sub { <br \/> my ($self, $tree) = @_; <br \/> $tree\u2212>parent($self) if defined $tree; <br \/> }; <br \/> sub _build_child_tree { <br \/> my $self = shift; <br \/> return BinaryTree\u2212>new( parent => $self ); <br \/> }<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">If you\u2019ve already read Moose::Cookbook::Basics::BinaryTree_AttributeFeatures, then this example should look very familiar. In fact, all we\u2019ve done here is replace the attribute\u2019s &#8220;default&#8221; parameter with a &#8220;builder&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In this particular case, the &#8220;default&#8221; and &#8220;builder&#8221; options act in exactly the same way. When the &#8220;left&#8221; or &#8220;right&#8221; attribute is read, Moose calls the builder method to initialize the attribute.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note that Moose calls the builder method <i>on the object which has the attribute<\/i>. Here\u2019s an example:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $tree = BinaryTree\u2212>new(); <br \/> my $left = $tree\u2212>left();<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When &#8220;$tree\u2212>left()&#8221; is called, Moose calls &#8220;$tree\u2212>_build_child_tree()&#8221; in order to populate the &#8220;left&#8221; attribute. If we had passed &#8220;left&#8221; to the original constructor, the builder would not be called.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">There are some differences between &#8220;default&#8221; and &#8220;builder&#8221;. Notably, a builder is subclassable, and can be composed from a role. See Moose::Manual::Attributes for more details.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>The lazy_build shortcut<\/b> <br \/> The &#8220;lazy_build&#8221; attribute option can be used as sugar to specify a whole set of attribute options at once:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">has &#8216;animal&#8217; => ( <br \/> is => &#8216;ro&#8217;, <br \/> isa => &#8216;Animal&#8217;, <br \/> lazy_build => 1, <br \/> );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This is a shorthand for:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">has &#8216;animal&#8217; => ( <br \/> is => &#8216;ro&#8217;, <br \/> isa => &#8216;Animal&#8217;, <br \/> required => 1, <br \/> lazy => 1, <br \/> builder => &#8216;_build_animal&#8217;, <br \/> predicate => &#8216;has_animal&#8217;, <br \/> clearer => &#8216;clear_animal&#8217;, <br \/> );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If your attribute starts with an underscore, Moose is smart and will do the right thing with the &#8220;predicate&#8221; and &#8220;clearer&#8221;, making them both start with an underscore. The &#8220;builder&#8221; method <i>always<\/i> starts with an underscore.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can read more about &#8220;lazy_build&#8221; in Moose::Meta::Attribute<\/p>\n<h2>CONCLUSION <a name=\"CONCLUSION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;builder&#8221; option is a more OO-friendly version of the &#8220;default&#8221; functionality. It also separates the default-generating code into a well-defined method. Sprinkling your attribute definitions with anonymous subroutines can be quite ugly and hard to follow.<\/p>\n<h2>AUTHORS <a name=\"AUTHORS\"><\/a> <\/h2>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">Stevan Little <stevan@cpan.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Dave Rolsky <autarch@urth.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Jesse Luehrs <doy@cpan.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Shawn M Moore <sartak@cpan.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>\u00d7\u00d7\u00d7\u00d7 \u00d7\u00a7\u00d7\u00d7\u2019\u00d7\u00d7 (Yuval Kogman) <nothingmuch@woobling.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Karen Etheridge <ether@cpan.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Florian Ragwitz <rafl@debian.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Hans Dieter Pearcey <hdp@cpan.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Chris Prather <chris@prather.org><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p>Matt S Trout <mstrout@cpan.org><\/p>\n<\/td>\n<\/tr>\n<\/table>\n<h2>COPYRIGHT AND LICENSE <a name=\"COPYRIGHT AND LICENSE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This software is copyright (c) 2006 by Infinity Interactive, Inc.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This is free software; you can redistribute it and\/or modify it under the same terms as the Perl 5 programming language system itself.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  Moose::Cookbook::Basics::BinaryTree_BuilderAndLazyBuild \u2212 Builder methods and lazy_build <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3007,3024],"class_list":["post-6644","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-man3","tag-moosecookbookbasicsbinarytree_builderandlazybuild"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=6644"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6644\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=6644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=6644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=6644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}