1 .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.3
4 .\" ========================================================================
5 .de Sh \" Subsection heading
13 .de Sp \" Vertical space (when we can't use .PP)
17 .de Vb \" Begin verbatim text
22 .de Ve \" End verbatim text
26 .\" Set up some character translations and predefined strings. \*(-- will
27 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28 .\" double quote, and \*(R" will give a right double quote. | will give a
29 .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30 .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31 .\" expand to `' in nroff, nothing in troff, for use with C<>.
33 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
37 . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38 . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
51 .\" If the F register is turned on, we'll generate index entries on stderr for
52 .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53 .\" entries marked with X<> in POD. Of course, you'll have to process the
54 .\" output yourself in some meaningful fashion.
57 . tm Index:\\$1\t\\n%\t"\\$2"
63 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
64 .\" way too many mistakes in technical documents.
68 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69 .\" Fear. Run. Save yourself. No user-serviceable parts.
70 . \" fudge factors for nroff and troff
79 . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
85 . \" simple accents for nroff and troff
95 . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96 . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97 . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98 . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99 . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100 . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
102 . \" troff and (daisy-wheel) nroff accents
103 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110 .ds ae a\h'-(\w'a'u*4/10)'e
111 .ds Ae A\h'-(\w'A'u*4/10)'E
112 . \" corrections for vroff
113 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115 . \" for low resolution devices (crt and lpr)
116 .if \n(.H>23 .if \n(.V>19 \
129 .\" ========================================================================
131 .IX Title "Moose::Manual::Roles 3"
132 .TH Moose::Manual::Roles 3 "2009-09-15" "perl v5.8.7" "User Contributed Perl Documentation"
134 Moose::Manual::Roles \- Roles, an alternative to deep hierarchies and base classes
135 .SH "WHAT IS A ROLE?"
136 .IX Header "WHAT IS A ROLE?"
137 A role is something that classes do. Usually, a role encapsulates some
138 piece of behavior or state that can be shared between classes. It is
139 important to understand that \fIroles are not classes\fR. You cannot
140 inherit from a role, and a role cannot be instantiated. We sometimes
141 say that roles are \fIconsumed\fR, either by classes or other roles.
143 Instead, a role is \fIcomposed\fR into a class. In practical terms, this
144 means that all of the methods and attributes defined in a role are
145 added directly to (we sometimes say \*(L"flattened into\*(R") the class that
146 consumes the role. These attributes and methods then appear as if they
147 were defined in the class itself. A subclass of the consuming class
148 will inherit all of these methods and attributes.
150 Moose roles are similar to mixins or interfaces in other languages.
152 Besides defining their own methods and attributes, roles can also
153 require that the consuming class define certain methods of its
154 own. You could have a role that consisted only of a list of required
155 methods, in which case the role would be very much like a Java
158 Note that attribute accessors also count as methods for the
159 purposes of satisfying the requirements of a role.
161 .IX Header "A SIMPLE ROLE"
162 Creating a role looks a lot like creating a Moose class:
165 \& package Breakable;
173 \& has 'is_broken' => (
185 \& print "I broke\en";
189 \& $self\->is_broken(1);
193 Except for our use of Moose::Role, this looks just like a class
194 definition with Moose. However, this is not a class, and it cannot be
197 Instead, its attributes and methods will be composed into classes
219 The \f(CW\*(C`with\*(C'\fR function composes roles into a class. Once that is done,
220 the \f(CW\*(C`Car\*(C'\fR class has an \f(CW\*(C`is_broken\*(C'\fR attribute and a \f(CW\*(C`break\*(C'\fR
221 method. The \f(CW\*(C`Car\*(C'\fR class also \f(CW\*(C`does('Breakable')\*(C'\fR:
224 \& my $car = Car\->new( engine => Engine\->new );
228 \& print $car\->is_broken ? 'Busted' : 'Still working';
230 \& print $car\->is_broken ? 'Busted' : 'Still working';
234 \& $car\->does('Breakable'); # true
245 We could use this same role in a \f(CW\*(C`Bone\*(C'\fR class:
266 See also Moose::Cookbook::Roles::Recipe1 for an example.
267 .SH "REQUIRED METHODS"
268 .IX Header "REQUIRED METHODS"
269 As mentioned previously, a role can require that consuming classes
270 provide one or more methods. Using our \f(CW\*(C`Breakable\*(C'\fR example, let's
271 make it require that consuming classes implement their own \f(CW\*(C`break\*(C'\fR
275 \& package Breakable;
287 \& has 'is_broken' => (
294 \& after 'break' => sub {
299 \& $self\->is_broken(1);
303 If we try to consume this role in a class that does not have a
304 \&\f(CW\*(C`break\*(C'\fR method, we will get an exception.
306 You can see that we added a method modifier on \f(CW\*(C`break\*(C'\fR. We want
307 classes that consume this role to implement their own logic for
308 breaking, but we make sure that the \f(CW\*(C`is_broken\*(C'\fR attribute is always
309 set to true when \f(CW\*(C`break\*(C'\fR is called.
336 \& if ( $self\->is_moving ) {
341 .Sh "Roles Versus Abstract Base Classes"
342 .IX Subsection "Roles Versus Abstract Base Classes"
343 If you are familiar with the concept of abstract base classes in other
344 languages, you may be tempted to use roles in the same way.
346 You \fIcan\fR define an \*(L"interface\-only\*(R" role, one that contains \fIjust\fR
347 a list of required methods.
349 However, any class which consumes this role must implement all of the
350 required methods, either directly or through inheritance from a
351 parent. You cannot delay the method requirement check so that they can
352 be implemented by future subclasses.
354 Because the role defines the required methods directly, adding a base
355 class to the mix would not achieve anything. We recommend that you
356 simply consume the interface role in each class which implements that
358 .Sh "Required Attributes"
359 .IX Subsection "Required Attributes"
360 As mentioned before, a role requirement may also be satisfied by an
361 attribute accessor. But any \f(CW\*(C`has\*(C'\fR functions, which will generate
362 accessors that satisfy the role requirement, must be placed
363 \&\fIbefore\fR the \f(CW\*(C`with\*(C'\fR function that composes the role.
366 \& package Breakable;
374 \& requires 'stress';
395 .SH "USING METHOD MODIFIERS"
396 .IX Header "USING METHOD MODIFIERS"
397 Method modifiers and roles are a very powerful combination. Often, a
398 role will combine method modifiers and required methods. We already
399 saw one example with our \f(CW\*(C`Breakable\*(C'\fR example.
401 Method modifiers increase the complexity of roles, because they make
402 the role application order relevant. If a class uses multiple roles,
403 each of which modify the same method, those modifiers will be applied
404 in the same order as the roles are used:
419 \& with 'Breakable', 'ExplodesOnBreakage';
422 Assuming that the new \f(CW\*(C`ExplodesOnBreakage\*(C'\fR method \fIalso\fR has an
423 \&\f(CW\*(C`after\*(C'\fR modifier on \f(CW\*(C`break\*(C'\fR, the \f(CW\*(C`after\*(C'\fR modifiers will run one
424 after the other. The modifier from \f(CW\*(C`Breakable\*(C'\fR will run first, then
425 the one from \f(CW\*(C`ExplodesOnBreakage\*(C'\fR.
426 .SH "METHOD CONFLICTS"
427 .IX Header "METHOD CONFLICTS"
428 If a class composes multiple roles, and those roles have methods of
429 the same name, we will have a conflict. In that case, the composing
430 class is required to provide its \fIown\fR method of the same name.
433 \& package Breakdancer;
448 If we compose both \f(CW\*(C`Breakable\*(C'\fR and \f(CW\*(C`Breakdancer\*(C'\fR in a class, we must
449 provide our own \f(CW\*(C`break\*(C'\fR method:
452 \& package FragileDancer;
460 \& with 'Breakable', 'Breakdancer';
467 A role can be a collection of other roles:
470 \& package Break::Bundle;
478 \& with ('Breakable', 'Breakdancer');
480 .SH "METHOD EXCLUSION AND ALIASING"
481 .IX Header "METHOD EXCLUSION AND ALIASING"
482 If we want our \f(CW\*(C`FragileDancer\*(C'\fR class to be able to call the methods
483 from both its roles, we can alias the methods:
486 \& package FragileDancer;
494 \& with 'Breakable' => { \-alias => { break => 'break_bone' } },
495 \& 'Breakdancer' => { \-alias => { break => 'break_dance' } };
498 However, aliasing a method simply makes a \fIcopy\fR of the method with
499 the new name. We also need to exclude the original name:
502 \& with 'Breakable' => {
503 \& \-alias => { break => 'break_bone' },
504 \& \-excludes => 'break',
506 \& 'Breakdancer' => {
507 \& \-alias => { break => 'break_dance' },
508 \& \-excludes => 'break',
512 The excludes parameter prevents the \f(CW\*(C`break\*(C'\fR method from being composed
513 into the \f(CW\*(C`FragileDancer\*(C'\fR class, so we don't have a conflict. This
514 means that \f(CW\*(C`FragileDancer\*(C'\fR does not need to implement its own
515 \&\f(CW\*(C`break\*(C'\fR method.
517 This is useful, but it's worth noting that this breaks the contract
518 implicit in consuming a role. Our \f(CW\*(C`FragileDancer\*(C'\fR class does both the
519 \&\f(CW\*(C`Breakable\*(C'\fR and \f(CW\*(C`BreakDancer\*(C'\fR, but does not provide a \f(CW\*(C`break\*(C'\fR
520 method. If some \s-1API\s0 expects an object that does one of those roles, it
521 probably expects it to implement that method.
523 In some use cases we might alias and exclude methods from roles, but
524 then provide a method of the same name in the class itself.
526 Also see Moose::Cookbook::Roles::Recipe2 for an example.
528 .IX Header "ROLE EXCLUSION"
529 A role can say that it cannot be combined with some other role. This
530 should be used with great caution, since it limits the re-usability of
534 \& package Breakable;
542 \& excludes 'BreakDancer';
545 .IX Header "APPLYING ROLES"
546 A role can be applied to a class or an instance in other ways besides
547 using the 'with' syntax.
549 To apply a role to a class, use Moose::Util and the 'apply_all_roles'
550 function. If you apply the role to a class, it will affect all objects of that
551 class. You can't apply a role to a class if it has been made immutable. In
552 some circumstances it may make sense to make the class mutable, apply the role,
553 then make the class immutable again.
558 \& my $class = 'MyApp::Test';
559 \& $class\->meta\->make_mutable;
560 \& Moose::Util::apply_all_roles($class\->meta, ('MyApp::SomeRole'));
561 \& $class\->meta\->make_immutable;
564 Do not apply roles to classes that have immutable subclasses, since that
565 will invalidate the metadata of the subclasses.
567 If you want the role to be applied only to a particular instance and not to the
568 class, you can apply the roles to the instance instead of the class's meta:
571 \& Moose::Util::apply_all_roles($instance, ('MyApp::SomeRole'));
574 Or you can use the role's meta object:
577 \& MyApp::SomeRole\->meta\->apply($instance);
580 The mutable/immutable state is not relevant to roles applied to instances.
581 See Moose::Role and Moose::Util for more details and
582 Moose::Cookbook::Roles::Recipe3 for a more developed example.
583 .SH "ADDING A ROLE TO AN OBJECT INSTANCE"
584 .IX Header "ADDING A ROLE TO AN OBJECT INSTANCE"
585 Sometimes you may want to add a role to an object instance, rather than to a
586 class. For example, you may want to add debug tracing to one instance of an
587 object while debugging a particular bug. Another use case might be to
588 dynamically change objects based on a user's configuration, as a plugin
591 The best way to do this is to use the \f(CW\*(C`apply_all_roles\*(C'\fR function from
595 \& use Moose::Util qw( apply_all_roles );
599 \& my $car = Car\->new;
600 \& apply_all_roles( $car, 'Breakable' );
603 This function can apply more than one role at a time, and will do so using the
604 normal Moose role combination system. We recommend using this function to
605 apply roles to an object. This is what Moose uses internally when you call
606 \&\f(CW\*(C`with\*(C'\fR.
609 Dave Rolsky <autarch@urth.org>
610 .SH "COPYRIGHT AND LICENSE"
611 .IX Header "COPYRIGHT AND LICENSE"
612 Copyright 2009 by Infinity Interactive, Inc.
614 <http://www.iinteractive.com>
616 This library is free software; you can redistribute it and/or modify
617 it under the same terms as Perl itself.