{"id":7445,"date":"2022-12-20T19:38:07","date_gmt":"2022-12-20T22:38:07","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/moosecookbookrolesrestartable_advancedcomposition-man3\/"},"modified":"2022-12-20T19:38:07","modified_gmt":"2022-12-20T22:38:07","slug":"moosecookbookrolesrestartable_advancedcomposition-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/moosecookbookrolesrestartable_advancedcomposition-man3\/","title":{"rendered":"Moose::Cookbook::Roles::Restartable_AdvancedComposition (man3)"},"content":{"rendered":"<h1 align=\"center\">Moose::Cookbook::Roles::Restartable_AdvancedComposition<\/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::Restartable_AdvancedComposition \u2212 Advanced Role Composition \u2212 method exclusion and aliasing<\/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 Restartable; <br \/> use Moose::Role; <br \/> has &#8216;is_paused&#8217; => ( <br \/> is => &#8216;rw&#8217;, <br \/> isa => &#8216;Bool&#8217;, <br \/> default => 0, <br \/> ); <br \/> requires &#8216;save_state&#8217;, &#8216;load_state&#8217;; <br \/> sub stop { 1 } <br \/> sub start { 1 } <br \/> package Restartable::ButUnreliable; <br \/> use Moose::Role; <br \/> with &#8216;Restartable&#8217; => { <br \/> \u2212alias => { <br \/> stop => &#8216;_stop&#8217;, <br \/> start => &#8216;_start&#8217; <br \/> }, <br \/> \u2212excludes => [ &#8216;stop&#8217;, &#8216;start&#8217; ], <br \/> }; <br \/> sub stop { <br \/> my $self = shift; <br \/> $self\u2212>explode() if rand(1) > .5; <br \/> $self\u2212>_stop(); <br \/> } <br \/> sub start { <br \/> my $self = shift; <br \/> $self\u2212>explode() if rand(1) > .5; <br \/> $self\u2212>_start(); <br \/> } <br \/> package Restartable::ButBroken; <br \/> use Moose::Role; <br \/> with &#8216;Restartable&#8217; => { \u2212excludes => [ &#8216;stop&#8217;, &#8216;start&#8217; ] }; <br \/> sub stop { <br \/> my $self = shift; <br \/> $self\u2212>explode(); <br \/> } <br \/> sub start { <br \/> my $self = shift; <br \/> $self\u2212>explode(); <br \/> }<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">In this example, we demonstrate how to exercise fine-grained control over what methods we consume from a role. We have a &#8220;Restartable&#8221; role which provides an &#8220;is_paused&#8221; attribute, and two methods, &#8220;stop&#8221; and &#8220;start&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Then we have two more roles which implement the same interface, each putting their own spin on the &#8220;stop&#8221; and &#8220;start&#8221; methods.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In the &#8220;Restartable::ButUnreliable&#8221; role, we want to provide a new implementation of &#8220;stop&#8221; and &#8220;start&#8221;, but still have access to the original implementation. To do this, we alias the methods from &#8220;Restartable&#8221; to private methods, and provide wrappers around the originals (1).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note that aliasing simply <i>adds<\/i> a name, so we also need to exclude the methods with their original names.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">with &#8216;Restartable&#8217; => { <br \/> \u2212alias => { <br \/> stop => &#8216;_stop&#8217;, <br \/> start => &#8216;_start&#8217; <br \/> }, <br \/> \u2212excludes => [ &#8216;stop&#8217;, &#8216;start&#8217; ], <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In the &#8220;Restartable::ButBroken&#8221; role, we want to provide an entirely new behavior for &#8220;stop&#8221; and &#8220;start&#8221;. We exclude them entirely when composing the &#8220;Restartable&#8221; role into &#8220;Restartable::ButBroken&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">It\u2019s worth noting that the &#8220;\u2212excludes&#8221; parameter also accepts a single string as an argument if you just want to exclude one method.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">with &#8216;Restartable&#8217; => { \u2212excludes => [ &#8216;stop&#8217;, &#8216;start&#8217; ] };<\/p>\n<h2>CONCLUSION <a name=\"CONCLUSION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Exclusion and renaming are a power tool that can be handy, especially when building roles out of other roles. In this example, all of our roles implement the &#8220;Restartable&#8221; role. Each role provides same <small>API,<\/small> but each has a different implementation under the hood.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can also use the method aliasing and excluding features when composing a role into a class.<\/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\">The mention of wrapper should tell you that we could do the same thing using method modifiers, but for the sake of this example, we don\u2019t.<\/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::Restartable_AdvancedComposition \u2212 Advanced Role Composition \u2212 method exclusion and aliasing <\/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,3757],"class_list":["post-7445","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-man3","tag-moosecookbookrolesrestartable_advancedcomposition"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7445","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=7445"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7445\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}