{"id":6999,"date":"2022-12-20T19:34:36","date_gmt":"2022-12-20T22:34:36","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/moosecookbookbasicsperson_buildargsandbuild-man3\/"},"modified":"2022-12-20T19:34:36","modified_gmt":"2022-12-20T22:34:36","slug":"moosecookbookbasicsperson_buildargsandbuild-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/moosecookbookbasicsperson_buildargsandbuild-man3\/","title":{"rendered":"Moose::Cookbook::Basics::Person_BUILDARGSAndBUILD (man3)"},"content":{"rendered":"<h1 align=\"center\">Moose::Cookbook::Basics::Person_BUILDARGSAndBUILD<\/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=\"#MORE CONSIDERATIONS\">MORE CONSIDERATIONS<\/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::Person_BUILDARGSAndBUILD \u2212 Using BUILDARGS and BUILD to hook into object construction<\/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 Person; <br \/> has &#8216;ssn&#8217; => ( <br \/> is => &#8216;ro&#8217;, <br \/> isa => &#8216;Str&#8217;, <br \/> predicate => &#8216;has_ssn&#8217;, <br \/> ); <br \/> has &#8216;country_of_residence&#8217; => ( <br \/> is => &#8216;ro&#8217;, <br \/> isa => &#8216;Str&#8217;, <br \/> default => &#8216;usa&#8217; <br \/> ); <br \/> has &#8216;first_name&#8217; => ( <br \/> is => &#8216;ro&#8217;, <br \/> isa => &#8216;Str&#8217;, <br \/> ); <br \/> has &#8216;last_name&#8217; => ( <br \/> is => &#8216;ro&#8217;, <br \/> isa => &#8216;Str&#8217;, <br \/> ); <br \/> around BUILDARGS => sub { <br \/> my $orig = shift; <br \/> my $class = shift; <br \/> if ( @_ == 1 &#038;&#038; ! ref $_[0] ) { <br \/> return $class\u2212>$orig(ssn => $_[0]); <br \/> } <br \/> else { <br \/> return $class\u2212>$orig(@_); <br \/> } <br \/> }; <br \/> sub BUILD { <br \/> my $self = shift; <br \/> if ( $self\u2212>country_of_residence eq &#8216;usa&#8217; ) { <br \/> die &#8216;Cannot create a Person who lives in the USA without an ssn.&#8217; <br \/> unless $self\u2212>has_ssn; <br \/> } <br \/> }<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This recipe demonstrates the use of &#8220;BUILDARGS&#8221; and &#8220;BUILD&#8221;. By defining these methods, we can hook into the object construction process without overriding &#8220;new&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;BUILDARGS&#8221; method is called <i>before<\/i> an object has been created. It is called as a class method, and receives all of the parameters passed to the &#8220;new&#8221; method. It is expected to do something with these arguments and return a hash reference. The keys of the hash must be attribute &#8220;init_arg&#8221;s.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The primary purpose of &#8220;BUILDARGS&#8221; is to allow a class to accept something other than named arguments. In the case of our &#8220;Person&#8221; class, we are allowing it to be called with a single argument, a social security number:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $person = Person\u2212>new(&#8216;123\u221245\u22126789&#8217;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The key part of our &#8220;BUILDARGS&#8221; is this conditional:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if ( @_ == 1 &#038;&#038; ! ref $_[0] ) { <br \/> return $class\u2212>$orig(ssn => $_[0]); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">By default, Moose constructors accept a list of key-value pairs, or a hash reference. We need to make sure that $_[0] is not a reference before assuming it is a social security number.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">We call the original &#8220;BUILDARGS&#8221; method to handle all the other cases. You should always do this in your own &#8220;BUILDARGS&#8221; methods, since Moose::Object provides its own &#8220;BUILDARGS&#8221; method that handles hash references and a list of key-value pairs.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;BUILD&#8221; method is called <i>after<\/i> the object is constructed, but before it is returned to the caller. The &#8220;BUILD&#8221; method provides an opportunity to check the object state as a whole. This is a good place to put logic that cannot be expressed as a type constraint on a single attribute.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In the &#8220;Person&#8221; class, we need to check the relationship between two attributes, &#8220;ssn&#8221; and &#8220;country_of_residence&#8221;. We throw an exception if the object is not logically consistent.<\/p>\n<h2>MORE CONSIDERATIONS <a name=\"MORE CONSIDERATIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This recipe is made significantly simpler because all of the attributes are read-only. If the &#8220;country_of_residence&#8221; attribute were settable, we would need to check that a Person had an &#8220;ssn&#8221; if the new country was &#8220;usa&#8221;. This could be done with a &#8220;before&#8221; modifier.<\/p>\n<h2>CONCLUSION <a name=\"CONCLUSION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">We have repeatedly discouraged overriding &#8220;new&#8221; in Moose classes. This recipe shows how you can use &#8220;BUILDARGS&#8221; and &#8220;BUILD&#8221; to hook into object construction without overriding &#8220;new&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;BUILDARGS&#8221; method lets us expand on Moose\u2019s built-in parameter handling for constructors. The &#8220;BUILD&#8221; method lets us implement logical constraints across the whole object after it is created.<\/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::Person_BUILDARGSAndBUILD \u2212 Using BUILDARGS and BUILD to hook into object construction <\/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,3349],"class_list":["post-6999","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-man3","tag-moosecookbookbasicsperson_buildargsandbuild"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6999","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=6999"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6999\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=6999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=6999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=6999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}