1Moose::Cookbook::Roles:U:sAeprplCiocnatMtroiioobsnueTt:oe:IdCnosPotekarbnlocoeDk(o:3c:)uRmoelnetsa:t:iAopnplicationToInstance(3)
2
3
4

NAME

6       Moose::Cookbook::Roles::ApplicationToInstance - Applying a role to an
7       object instance
8

VERSION

10       version 2.2011
11

SYNOPSIS

13         package MyApp::Role::Job::Manager;
14
15         use List::Util qw( first );
16
17         use Moose::Role;
18
19         has 'employees' => (
20             is  => 'rw',
21             isa => 'ArrayRef[Employee]',
22         );
23
24         sub assign_work {
25             my $self = shift;
26             my $work = shift;
27
28             my $employee = first { !$_->has_work } @{ $self->employees };
29
30             die 'All my employees have work to do!' unless $employee;
31
32             $employee->work($work);
33         }
34
35         package main;
36
37         my $lisa = Employee->new( name => 'Lisa' );
38         MyApp::Role::Job::Manager->meta->apply($lisa);
39
40         my $homer = Employee->new( name => 'Homer' );
41         my $bart  = Employee->new( name => 'Bart' );
42         my $marge = Employee->new( name => 'Marge' );
43
44         $lisa->employees( [ $homer, $bart, $marge ] );
45         $lisa->assign_work('mow the lawn');
46

DESCRIPTION

48       In this recipe, we show how a role can be applied to an object. In this
49       specific case, we are giving an employee managerial responsibilities.
50
51       Applying a role to an object is simple. The Moose::Meta::Role object
52       provides an "apply" method. This method will do the right thing when
53       given an object instance.
54
55         MyApp::Role::Job::Manager->meta->apply($lisa);
56
57       We could also use the "apply_all_roles" function from Moose::Util.
58
59         apply_all_roles( $person, MyApp::Role::Job::Manager->meta );
60
61       The main advantage of using "apply_all_roles" is that it can be used to
62       apply more than one role at a time.
63
64       We could also pass parameters to the role we're applying:
65
66         MyApp::Role::Job::Manager->meta->apply(
67             $lisa,
68             -alias => { assign_work => 'get_off_your_lazy_behind' },
69         );
70
71       We saw examples of how method exclusion and alias working in
72       Moose::Cookbook::Roles::Restartable_AdvancedComposition.
73

CONCLUSION

75       Applying a role to an object instance is a useful tool for adding
76       behavior to existing objects. In our example, it is effective used to
77       model a promotion.
78
79       It can also be useful as a sort of controlled monkey-patching for
80       existing code, particularly non-Moose code. For example, you could
81       create a debugging role and apply it to an object at runtime.
82

AUTHORS

84       ·   Stevan Little <stevan.little@iinteractive.com>
85
86       ·   Dave Rolsky <autarch@urth.org>
87
88       ·   Jesse Luehrs <doy@tozt.net>
89
90       ·   Shawn M Moore <code@sartak.org>
91
92       ·   יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
93
94       ·   Karen Etheridge <ether@cpan.org>
95
96       ·   Florian Ragwitz <rafl@debian.org>
97
98       ·   Hans Dieter Pearcey <hdp@weftsoar.net>
99
100       ·   Chris Prather <chris@prather.org>
101
102       ·   Matt S Trout <mst@shadowcat.co.uk>
103
105       This software is copyright (c) 2006 by Infinity Interactive, Inc.
106
107       This is free software; you can redistribute it and/or modify it under
108       the same terms as the Perl 5 programming language system itself.
109
110
111
112perl v5.30.0                  Moos2e0:1:9C-o0o7k-b2o6ok::Roles::ApplicationToInstance(3)
Impressum