{"id":7382,"date":"2022-12-20T19:37:53","date_gmt":"2022-12-20T22:37:53","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/moosecookbookbasicspoint_attributesandsubclassing-man3\/"},"modified":"2022-12-20T19:37:53","modified_gmt":"2022-12-20T22:37:53","slug":"moosecookbookbasicspoint_attributesandsubclassing-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/moosecookbookbasicspoint_attributesandsubclassing-man3\/","title":{"rendered":"Moose::Cookbook::Basics::Point_AttributesAndSubclassing (man3)"},"content":{"rendered":"<h1 align=\"center\">Moose::Cookbook::Basics::Point_AttributesAndSubclassing<\/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=\"#FOOTNOTES\">FOOTNOTES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/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::Point_AttributesAndSubclassing \u2212 Point and Point3D classes, showing basic attributes and subclassing.<\/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 Point; <br \/> use Moose; <br \/> has &#8216;x&#8217; => (isa => &#8216;Int&#8217;, is => &#8216;rw&#8217;, required => 1); <br \/> has &#8216;y&#8217; => (isa => &#8216;Int&#8217;, is => &#8216;rw&#8217;, required => 1); <br \/> sub clear { <br \/> my $self = shift; <br \/> $self\u2212>x(0); <br \/> $self\u2212>y(0); <br \/> } <br \/> package Point3D; <br \/> use Moose; <br \/> extends &#8216;Point&#8217;; <br \/> has &#8216;z&#8217; => (isa => &#8216;Int&#8217;, is => &#8216;rw&#8217;, required => 1); <br \/> after &#8216;clear&#8217; => sub { <br \/> my $self = shift; <br \/> $self\u2212>z(0); <br \/> }; <br \/> package main; <br \/> # hash or hashrefs are ok for the constructor <br \/> my $point1 = Point\u2212>new(x => 5, y => 7); <br \/> my $point2 = Point\u2212>new({x => 5, y => 7}); <br \/> my $point3d = Point3D\u2212>new(x => 5, y => 42, z => \u22125);<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This is the classic Point example. It is taken directly from the Perl 6 Apocalypse 12 document, and is similar to the example found in the classic K&#038;R C book as well.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">As with all Perl 5 classes, a Moose class is defined in a package. Moose handles turning on &#8220;strict&#8221; and &#8220;warnings&#8221; for us, so all we need to do is say &#8220;use Moose&#8221;, and no kittens will die.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When Moose is loaded, it exports a set of sugar functions into our package. This means that we import some functions which serve as Moose &#8220;keywords&#8221;. These aren\u2019t real language keywords, they\u2019re just Perl functions exported into our package.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Moose automatically makes our package a subclass of Moose::Object. The Moose::Object class provides us with a constructor that respects our attributes, as well other features. See Moose::Object for details.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Now, onto the keywords. The first one we see here is &#8220;has&#8221;, which defines an instance attribute in our class:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">has &#8216;x&#8217; => (isa => &#8216;Int&#8217;, is => &#8216;rw&#8217;, required => 1);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This will create an attribute named &#8220;x&#8221;. The &#8220;isa&#8221; parameter says that we expect the value stored in this attribute to pass the type constraint for &#8220;Int&#8221; (1). The accessor generated for this attribute will be read-write.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;required => 1&#8221; parameter means that this attribute must be provided when a new object is created. A point object without coordinates doesn\u2019t make much sense, so we don\u2019t allow it.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">We have defined our attributes; next we define our methods. In Moose, as with regular Perl 5 <small>OO,<\/small> a method is just a subroutine defined within the package:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">sub clear { <br \/> my $self = shift; <br \/> $self\u2212>x(0); <br \/> $self\u2212>y(0); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">That concludes the <b>Point<\/b> class.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Next we have a subclass of <b>Point<\/b>, <b>Point3D<\/b>. To declare our superclass, we use the Moose keyword &#8220;extends&#8221;:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">extends &#8216;Point&#8217;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;extends&#8221; keyword works much like &#8220;use base&#8221;\/&#8221;use parent&#8221;. First, it will attempt to load your class if needed. However, unlike &#8220;base&#8221;, the &#8220;extends&#8221; keyword will <i>overwrite<\/i> any previous values in your package\u2019s @ISA, where &#8220;use base&#8221; will &#8220;push&#8221; values onto the package\u2019s @ISA.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">It is my opinion that the behavior of &#8220;extends&#8221; is more intuitive. (2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Next we create a new attribute for <b>Point3D<\/b> called &#8220;z&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">has &#8216;z&#8217; => (isa => &#8216;Int&#8217;, is => &#8216;rw&#8217;, required => 1);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This attribute is just like <b>Point<\/b>\u2019s &#8220;x&#8221; and &#8220;y&#8221; attributes.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;after&#8221; keyword demonstrates a Moose feature called &#8220;method modifiers&#8221; (or &#8220;advice&#8221; for the <small>AOP<\/small> inclined):<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">after &#8216;clear&#8217; => sub { <br \/> my $self = shift; <br \/> $self\u2212>z(0); <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When &#8220;clear&#8221; is called on a <b>Point3D<\/b> object, our modifier method gets called as well. Unsurprisingly, the modifier is called <i>after<\/i> the real method.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In this case, the real &#8220;clear&#8221; method is inherited from <b>Point<\/b>. Our modifier method receives the same arguments as those passed to the modified method (just $self here).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Of course, using the &#8220;after&#8221; modifier is not the only way to accomplish this. This <b>is<\/b> Perl, right? You can get the same results with this code:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">sub clear { <br \/> my $self = shift; <br \/> $self\u2212>SUPER::clear(); <br \/> $self\u2212>z(0); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You could also use another Moose method modifier, &#8220;override&#8221;:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">override &#8216;clear&#8217; => sub { <br \/> my $self = shift; <br \/> super(); <br \/> $self\u2212>z(0); <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;override&#8221; modifier allows you to use the &#8220;super&#8221; keyword to dispatch to the superclass\u2019s method in a very Ruby-ish style.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The choice of whether to use a method modifier, and which one to use, is often a question of style as much as functionality.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Since <b>Point<\/b> inherits from Moose::Object, it will also inherit the default Moose::Object constructor:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $point1 = Point\u2212>new(x => 5, y => 7); <br \/> my $point2 = Point\u2212>new({x => 5, y => 7}); <br \/> my $point3d = Point3D\u2212>new(x => 5, y => 42, z => \u22125);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;new&#8221; constructor accepts a named argument pair for each attribute defined by the class, which you can provide as a hash or hash reference. In this particular example, the attributes are required, and calling &#8220;new&#8221; without them will throw an error.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $point = Point\u2212>new( x => 5 ); # no y, kaboom!<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">From here on, we can use $point and $point3d just as you would any other Perl 5 object. For a more detailed example of what can be done, you can refer to the <i>t\/recipes\/basics_point_attributesandsubclassing.t<\/i> test file.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Moose Objects are Just Hashrefs<\/b> <br \/> While this all may appear rather magical, it\u2019s important to realize that Moose objects are just hash references under the hood (3). For example, you could pass $self to &#8220;Data::Dumper&#8221; and you\u2019d get exactly what you\u2019d expect.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You could even poke around inside the object\u2019s data structure, but that is strongly discouraged.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The fact that Moose objects are hashrefs means it is easy to use Moose to extend non-Moose classes, as long as they too are hash references. If you want to extend a non-hashref class, check out &#8220;MooseX::InsideOut&#8221;.<\/p>\n<h2>CONCLUSION <a name=\"CONCLUSION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This recipe demonstrates some basic Moose concepts, attributes, subclassing, and a simple method modifier.<\/p>\n<h2>FOOTNOTES <a name=\"FOOTNOTES\"><\/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=\"4%\">\n<p style=\"margin-top: 1em\">(1)<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">Moose provides a number of builtin type constraints, of which &#8220;Int&#8221; is one. For more information on the type constraint system, see Moose::Util::TypeConstraints.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"4%\">\n<p>(2)<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"83%\">\n<p>The &#8220;extends&#8221; keyword supports multiple inheritance. Simply pass all of your superclasses to &#8220;extends&#8221; as a list:<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">extends &#8216;Foo&#8217;, &#8216;Bar&#8217;, &#8216;Baz&#8217;;<\/p>\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=\"4%\">\n<p style=\"margin-top: 1em\">(3)<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">Moose supports using instance structures other than blessed hash references (such as glob references \u2212 see MooseX::GlobRef).<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Method Modifiers<\/p>\n<p style=\"margin-left:17%;\">The concept of method modifiers is directly ripped off from <small>CLOS. A<\/small> great explanation of them can be found by following this link.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><http:\/\/www.gigamonkeys.com\/book\/object\u2212reorientation\u2212generic\u2212functions.html><\/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::Point_AttributesAndSubclassing \u2212 Point and Point3D classes, showing basic attributes and subclassing. <\/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,3696],"class_list":["post-7382","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-man3","tag-moosecookbookbasicspoint_attributesandsubclassing"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7382","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=7382"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7382\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}