1 package Moose::Cookbook::Roles::Recipe3;
3 # ABSTRACT: Applying a role to an object instance
7 package Moose::Cookbook::Roles::Recipe3;
14 # Not in the recipe, but needed for writing tests.
28 predicate => 'has_work',
36 package MyApp::Role::Job::Manager;
38 use List::Util qw( first );
44 isa => 'ArrayRef[Employee]',
51 my $employee = first { !$_->has_work } @{ $self->employees };
53 die 'All my employees have work to do!' unless $employee;
55 $employee->work($work);
60 my $lisa = Employee->new( name => 'Lisa' );
61 MyApp::Role::Job::Manager->meta->apply($lisa);
63 my $homer = Employee->new( name => 'Homer' );
64 my $bart = Employee->new( name => 'Bart' );
65 my $marge = Employee->new( name => 'Marge' );
67 $lisa->employees( [ $homer, $bart, $marge ] );
68 $lisa->assign_work('mow the lawn');
72 In this recipe, we show how a role can be applied to an object. In
73 this specific case, we are giving an employee managerial
76 Applying a role to an object is simple. The L<Moose::Meta::Role>
77 object provides an C<apply> method. This method will do the right
78 thing when given an object instance.
80 MyApp::Role::Job::Manager->meta->apply($lisa);
82 We could also use the C<apply_all_roles> function from L<Moose::Util>.
84 apply_all_roles( $person, MyApp::Role::Job::Manager->meta );
86 The main advantage of using C<apply_all_roles> is that it can be used
87 to apply more than one role at a time.
89 We could also pass parameters to the role we're applying:
91 MyApp::Role::Job::Manager->meta->apply(
93 -alias => { assign_work => 'get_off_your_lazy_behind' },
96 We saw examples of how method exclusion and alias working in L<roles
97 recipe 2|Moose::Cookbook::Roles::Recipe2>.
101 Applying a role to an object instance is a useful tool for adding
102 behavior to existing objects. In our example, it is effective used to
105 It can also be useful as a sort of controlled monkey-patching for
106 existing code, particularly non-Moose code. For example, you could
107 create a debugging role and apply it to an object at runtime.
112 my $lisa = Employee->new( name => 'Lisa' );
113 MyApp::Role::Job::Manager->meta->apply($lisa);
115 my $homer = Employee->new( name => 'Homer' );
116 my $bart = Employee->new( name => 'Bart' );
117 my $marge = Employee->new( name => 'Marge' );
119 $lisa->employees( [ $homer, $bart, $marge ] );
120 $lisa->assign_work('mow the lawn');
122 ok( $lisa->does('MyApp::Role::Job::Manager'),
123 'lisa now does the manager role' );
125 is( $homer->work, 'mow the lawn',
126 'homer was assigned a task by lisa' );