{"id":6874,"date":"2022-12-20T19:33:45","date_gmt":"2022-12-20T22:33:45","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/moosecookbookrolescomparable_codereuse-man3\/"},"modified":"2022-12-20T19:33:45","modified_gmt":"2022-12-20T22:33:45","slug":"moosecookbookrolescomparable_codereuse-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/moosecookbookrolescomparable_codereuse-man3\/","title":{"rendered":"Moose::Cookbook::Roles::Comparable_CodeReuse (man3)"},"content":{"rendered":"<h1 align=\"center\">Moose::Cookbook::Roles::Comparable_CodeReuse<\/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=\"#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::Roles::Comparable_CodeReuse \u2212 Using roles for code reuse<\/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 Eq; <br \/> use Moose::Role; <br \/> requires &#8216;equal_to&#8217;; <br \/> sub not_equal_to { <br \/> my ( $self, $other ) = @_; <br \/> not $self\u2212>equal_to($other); <br \/> } <br \/> package Comparable; <br \/> use Moose::Role; <br \/> with &#8216;Eq&#8217;; <br \/> requires &#8216;compare&#8217;; <br \/> sub equal_to { <br \/> my ( $self, $other ) = @_; <br \/> $self\u2212>compare($other) == 0; <br \/> } <br \/> sub greater_than { <br \/> my ( $self, $other ) = @_; <br \/> $self\u2212>compare($other) == 1; <br \/> } <br \/> sub less_than { <br \/> my ( $self, $other ) = @_; <br \/> $self\u2212>compare($other) == \u22121; <br \/> } <br \/> sub greater_than_or_equal_to { <br \/> my ( $self, $other ) = @_; <br \/> $self\u2212>greater_than($other) || $self\u2212>equal_to($other); <br \/> } <br \/> sub less_than_or_equal_to { <br \/> my ( $self, $other ) = @_; <br \/> $self\u2212>less_than($other) || $self\u2212>equal_to($other); <br \/> } <br \/> package Printable; <br \/> use Moose::Role; <br \/> requires &#8216;to_string&#8217;; <br \/> package US::Currency; <br \/> use Moose; <br \/> with &#8216;Comparable&#8217;, &#8216;Printable&#8217;; <br \/> has &#8216;amount&#8217; => ( is => &#8216;rw&#8217;, isa => &#8216;Num&#8217;, default => 0 ); <br \/> sub compare { <br \/> my ( $self, $other ) = @_; <br \/> $self\u2212>amount <=> $other\u2212>amount; <br \/> } <br \/> sub to_string { <br \/> my $self = shift; <br \/> sprintf &#8216;$%0.2f USD&#8217; => $self\u2212>amount; <br \/> }<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Roles have two primary purposes: as interfaces, and as a means of code reuse. This recipe demonstrates the latter, with roles that define comparison and display code for objects.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Let\u2019s start with &#8220;Eq&#8221;. First, note that we\u2019ve replaced &#8220;use Moose&#8221; with &#8220;use Moose::Role&#8221;. We also have a new sugar function, &#8220;requires&#8221;:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">requires &#8216;equal_to&#8217;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This says that any class which consumes this role must provide an &#8220;equal_to&#8221; method. It can provide this method directly, or by consuming some other role.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;Eq&#8221; role defines its &#8220;not_equal_to&#8221; method in terms of the required &#8220;equal_to&#8221; method. This lets us minimize the methods that consuming classes must provide.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The next role, &#8220;Comparable&#8221;, builds on the &#8220;Eq&#8221; role. We include &#8220;Eq&#8221; in &#8220;Comparable&#8221; using &#8220;with&#8221;, another new sugar function:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">with &#8216;Eq&#8217;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;with&#8221; function takes a list of roles to consume. In our example, the &#8220;Comparable&#8221; role provides the &#8220;equal_to&#8221; method required by &#8220;Eq&#8221;. However, it could opt not to, in which case a class that consumed &#8220;Comparable&#8221; would have to provide its own &#8220;equal_to&#8221;. In other words, a role can consume another role <i>without<\/i> providing any required methods.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;Comparable&#8221; role requires a method, &#8220;compare&#8221;:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">requires &#8216;compare&#8217;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The &#8220;Comparable&#8221; role also provides a number of other methods, all of which ultimately rely on &#8220;compare&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">sub equal_to { <br \/> my ( $self, $other ) = @_; <br \/> $self\u2212>compare($other) == 0; <br \/> } <br \/> sub greater_than { <br \/> my ( $self, $other ) = @_; <br \/> $self\u2212>compare($other) == 1; <br \/> } <br \/> sub less_than { <br \/> my ( $self, $other ) = @_; <br \/> $self\u2212>compare($other) == \u22121; <br \/> } <br \/> sub greater_than_or_equal_to { <br \/> my ( $self, $other ) = @_; <br \/> $self\u2212>greater_than($other) || $self\u2212>equal_to($other); <br \/> } <br \/> sub less_than_or_equal_to { <br \/> my ( $self, $other ) = @_; <br \/> $self\u2212>less_than($other) || $self\u2212>equal_to($other); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Finally, we define the &#8220;Printable&#8221; role. This role exists solely to provide an interface. It has no methods, just a list of required methods. In this case, it just requires a &#8220;to_string&#8221; method.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">An interface role is useful because it defines both a method and a <i>name<\/i>. We know that any class which does this role has a &#8220;to_string&#8221; method, but we can also assume that this method has the semantics we want. Presumably, in real code we would define those semantics in the documentation for the &#8220;Printable&#8221; role. (1)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Finally, we have the &#8220;US::Currency&#8221; class which consumes both the &#8220;Comparable&#8221; and &#8220;Printable&#8221; roles.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">with &#8216;Comparable&#8217;, &#8216;Printable&#8217;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">It also defines a regular Moose attribute, &#8220;amount&#8221;:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">has &#8216;amount&#8217; => ( is => &#8216;rw&#8217;, isa => &#8216;Num&#8217;, default => 0 );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Finally we see the implementation of the methods required by our roles. We have a &#8220;compare&#8221; method:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">sub compare { <br \/> my ( $self, $other ) = @_; <br \/> $self\u2212>amount <=> $other\u2212>amount; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">By consuming the &#8220;Comparable&#8221; role and defining this method, we gain the following methods for free: &#8220;equal_to&#8221;, &#8220;greater_than&#8221;, &#8220;less_than&#8221;, &#8220;greater_than_or_equal_to&#8221; and &#8220;less_than_or_equal_to&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Then we have our &#8220;to_string&#8221; method:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">sub to_string { <br \/> my $self = shift; <br \/> sprintf &#8216;$%0.2f USD&#8217; => $self\u2212>amount; <br \/> }<\/p>\n<h2>CONCLUSION <a name=\"CONCLUSION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Roles can be very powerful. They are a great way of encapsulating reusable behavior, as well as communicating (semantic and interface) information about the methods our classes provide.<\/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\">Consider two classes, &#8220;Runner&#8221; and &#8220;Process&#8221;, both of which define a &#8220;run&#8221; method. If we just require that an object implements a &#8220;run&#8221; method, we still aren\u2019t saying anything about what that method <i>actually does<\/i>. If we require an object that implements the &#8220;Executable&#8221; role, we\u2019re saying something about semantics.<\/p>\n<\/td>\n<\/tr>\n<\/table>\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::Roles::Comparable_CodeReuse \u2212 Using roles for code reuse <\/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,3238],"class_list":["post-6874","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-man3","tag-moosecookbookrolescomparable_codereuse"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6874","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=6874"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6874\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=6874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=6874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=6874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}