{"id":7157,"date":"2022-12-20T19:35:40","date_gmt":"2022-12-20T22:35:40","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/moosecookbookrolesapplicationtoinstance-man3\/"},"modified":"2022-12-20T19:35:40","modified_gmt":"2022-12-20T22:35:40","slug":"moosecookbookrolesapplicationtoinstance-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/moosecookbookrolesapplicationtoinstance-man3\/","title":{"rendered":"Moose::Cookbook::Roles::ApplicationToInstance (man3)"},"content":{"rendered":"<h1 align=\"center\">Moose::Cookbook::Roles::ApplicationToInstance<\/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::Roles::ApplicationToInstance \u2212 Applying a role to an object instance<\/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 MyApp::Role::Job::Manager; <br \/> use List::Util qw( first ); <br \/> use Moose::Role; <br \/> has &#8216;employees&#8217; => ( <br \/> is => &#8216;rw&#8217;, <br \/> isa => &#8216;ArrayRef[Employee]&#8217;, <br \/> ); <br \/> sub assign_work { <br \/> my $self = shift; <br \/> my $work = shift; <br \/> my $employee = first { !$_\u2212>has_work } @{ $self\u2212>employees }; <br \/> die &#8216;All my employees have work to do!&#8217; unless $employee; <br \/> $employee\u2212>work($work); <br \/> } <br \/> package main; <br \/> my $lisa = Employee\u2212>new( name => &#8216;Lisa&#8217; ); <br \/> MyApp::Role::Job::Manager\u2212>meta\u2212>apply($lisa); <br \/> my $homer = Employee\u2212>new( name => &#8216;Homer&#8217; ); <br \/> my $bart = Employee\u2212>new( name => &#8216;Bart&#8217; ); <br \/> my $marge = Employee\u2212>new( name => &#8216;Marge&#8217; ); <br \/> $lisa\u2212>employees( [ $homer, $bart, $marge ] ); <br \/> $lisa\u2212>assign_work(&#8216;mow the lawn&#8217;);<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">In this recipe, we show how a role can be applied to an object. In this specific case, we are giving an employee managerial responsibilities.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Applying a role to an object is simple. The Moose::Meta::Role object provides an &#8220;apply&#8221; method. This method will do the right thing when given an object instance.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">MyApp::Role::Job::Manager\u2212>meta\u2212>apply($lisa);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">We could also use the &#8220;apply_all_roles&#8221; function from Moose::Util.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">apply_all_roles( $person, MyApp::Role::Job::Manager\u2212>meta );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The main advantage of using &#8220;apply_all_roles&#8221; is that it can be used to apply more than one role at a time.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">We could also pass parameters to the role we\u2019re applying:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">MyApp::Role::Job::Manager\u2212>meta\u2212>apply( <br \/> $lisa, <br \/> \u2212alias => { assign_work => &#8216;get_off_your_lazy_behind&#8217; }, <br \/> );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">We saw examples of how method exclusion and alias working in Moose::Cookbook::Roles::Restartable_AdvancedComposition.<\/p>\n<h2>CONCLUSION <a name=\"CONCLUSION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Applying a role to an object instance is a useful tool for adding behavior to existing objects. In our example, it is effective used to model a promotion.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">It can also be useful as a sort of controlled monkey-patching for existing code, particularly non-Moose code. For example, you could create a debugging role and apply it to an object at runtime.<\/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::Roles::ApplicationToInstance \u2212 Applying a role to an object instance <\/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,3492],"class_list":["post-7157","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-man3","tag-moosecookbookrolesapplicationtoinstance"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7157","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=7157"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7157\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}