Add built local::lib
[catagits/Gitalist.git] / local-lib5 / man / man3 / Moose::Manual::Roles.3pm
1 .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.3
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sh \" Subsection heading
6 .br
7 .if t .Sp
8 .ne 5
9 .PP
10 \fB\\$1\fR
11 .PP
12 ..
13 .de Sp \" Vertical space (when we can't use .PP)
14 .if t .sp .5v
15 .if n .sp
16 ..
17 .de Vb \" Begin verbatim text
18 .ft CW
19 .nf
20 .ne \\$1
21 ..
22 .de Ve \" End verbatim text
23 .ft R
24 .fi
25 ..
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<>.
32 .tr \(*W-|\(bv\*(Tr
33 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34 .ie n \{\
35 .    ds -- \(*W-
36 .    ds PI pi
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
39 .    ds L" ""
40 .    ds R" ""
41 .    ds C` ""
42 .    ds C' ""
43 'br\}
44 .el\{\
45 .    ds -- \|\(em\|
46 .    ds PI \(*p
47 .    ds L" ``
48 .    ds R" ''
49 'br\}
50 .\"
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.
55 .if \nF \{\
56 .    de IX
57 .    tm Index:\\$1\t\\n%\t"\\$2"
58 ..
59 .    nr % 0
60 .    rr F
61 .\}
62 .\"
63 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
64 .\" way too many mistakes in technical documents.
65 .hy 0
66 .if n .na
67 .\"
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
71 .if n \{\
72 .    ds #H 0
73 .    ds #V .8m
74 .    ds #F .3m
75 .    ds #[ \f1
76 .    ds #] \fP
77 .\}
78 .if t \{\
79 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 .    ds #V .6m
81 .    ds #F 0
82 .    ds #[ \&
83 .    ds #] \&
84 .\}
85 .    \" simple accents for nroff and troff
86 .if n \{\
87 .    ds ' \&
88 .    ds ` \&
89 .    ds ^ \&
90 .    ds , \&
91 .    ds ~ ~
92 .    ds /
93 .\}
94 .if t \{\
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'
101 .\}
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 \
117 \{\
118 .    ds : e
119 .    ds 8 ss
120 .    ds o a
121 .    ds d- d\h'-1'\(ga
122 .    ds D- D\h'-1'\(hy
123 .    ds th \o'bp'
124 .    ds Th \o'LP'
125 .    ds ae ae
126 .    ds Ae AE
127 .\}
128 .rm #[ #] #H #V #F C
129 .\" ========================================================================
130 .\"
131 .IX Title "Moose::Manual::Roles 3"
132 .TH Moose::Manual::Roles 3 "2009-09-15" "perl v5.8.7" "User Contributed Perl Documentation"
133 .SH "NAME"
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.
142 .PP
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.
149 .PP
150 Moose roles are similar to mixins or interfaces in other languages.
151 .PP
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
156 interface.
157 .PP
158 Note that attribute accessors also count as methods for the
159 purposes of satisfying the requirements of a role. 
160 .SH "A SIMPLE ROLE"
161 .IX Header "A SIMPLE ROLE"
162 Creating a role looks a lot like creating a Moose class:
163 .PP
164 .Vb 1
165 \&  package Breakable;
166 .Ve
167 .PP
168 .Vb 1
169 \&  use Moose::Role;
170 .Ve
171 .PP
172 .Vb 4
173 \&  has 'is_broken' => (
174 \&      is  => 'rw',
175 \&      isa => 'Bool',
176 \&  );
177 .Ve
178 .PP
179 .Vb 2
180 \&  sub break {
181 \&      my $self = shift;
182 .Ve
183 .PP
184 .Vb 1
185 \&      print "I broke\en";
186 .Ve
187 .PP
188 .Vb 2
189 \&      $self\->is_broken(1);
190 \&  }
191 .Ve
192 .PP
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
195 instantiated.
196 .PP
197 Instead, its attributes and methods will be composed into classes
198 which use the role:
199 .PP
200 .Vb 1
201 \&  package Car;
202 .Ve
203 .PP
204 .Vb 1
205 \&  use Moose;
206 .Ve
207 .PP
208 .Vb 1
209 \&  with 'Breakable';
210 .Ve
211 .PP
212 .Vb 4
213 \&  has 'engine' => (
214 \&      is  => 'ro',
215 \&      isa => 'Engine',
216 \&  );
217 .Ve
218 .PP
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:
222 .PP
223 .Vb 1
224 \&  my $car = Car\->new( engine => Engine\->new );
225 .Ve
226 .PP
227 .Vb 3
228 \&  print $car\->is_broken ? 'Busted' : 'Still working';
229 \&  $car\->break;
230 \&  print $car\->is_broken ? 'Busted' : 'Still working';
231 .Ve
232 .PP
233 .Vb 1
234 \&  $car\->does('Breakable'); # true
235 .Ve
236 .PP
237 This prints:
238 .PP
239 .Vb 3
240 \&  Still working
241 \&  I broke
242 \&  Busted
243 .Ve
244 .PP
245 We could use this same role in a \f(CW\*(C`Bone\*(C'\fR class:
246 .PP
247 .Vb 1
248 \&  package Bone;
249 .Ve
250 .PP
251 .Vb 1
252 \&  use Moose;
253 .Ve
254 .PP
255 .Vb 1
256 \&  with 'Breakable';
257 .Ve
258 .PP
259 .Vb 4
260 \&  has 'marrow' => (
261 \&      is  => 'ro',
262 \&      isa => 'Marrow',
263 \&  );
264 .Ve
265 .PP
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
272 methods:
273 .PP
274 .Vb 1
275 \&  package Breakable;
276 .Ve
277 .PP
278 .Vb 1
279 \&  use Moose::Role;
280 .Ve
281 .PP
282 .Vb 1
283 \&  requires 'break';
284 .Ve
285 .PP
286 .Vb 4
287 \&  has 'is_broken' => (
288 \&      is  => 'rw',
289 \&      isa => 'Bool',
290 \&  );
291 .Ve
292 .PP
293 .Vb 2
294 \&  after 'break' => sub {
295 \&      my $self = shift;
296 .Ve
297 .PP
298 .Vb 2
299 \&      $self\->is_broken(1);
300 \&  };
301 .Ve
302 .PP
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.
305 .PP
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.
310 .PP
311 .Vb 1
312 \&  package Car
313 .Ve
314 .PP
315 .Vb 1
316 \&  use Moose;
317 .Ve
318 .PP
319 .Vb 1
320 \&  with 'Breakable';
321 .Ve
322 .PP
323 .Vb 4
324 \&  has 'engine' => (
325 \&      is  => 'ro',
326 \&      isa => 'Engine',
327 \&  );
328 .Ve
329 .PP
330 .Vb 2
331 \&  sub break {
332 \&      my $self = shift;
333 .Ve
334 .PP
335 .Vb 4
336 \&      if ( $self\->is_moving ) {
337 \&          $self\->stop;
338 \&      }
339 \&  }
340 .Ve
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.
345 .PP
346 You \fIcan\fR define an \*(L"interface\-only\*(R" role, one that contains \fIjust\fR
347 a list of required methods.
348 .PP
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.
353 .PP
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
357 interface.
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.
364 .PP
365 .Vb 1
366 \&  package Breakable;
367 .Ve
368 .PP
369 .Vb 1
370 \&  use Moose::Role;
371 .Ve
372 .PP
373 .Vb 1
374 \&  requires 'stress';
375 .Ve
376 .PP
377 .Vb 1
378 \&  package Car;
379 .Ve
380 .PP
381 .Vb 1
382 \&  use Moose;
383 .Ve
384 .PP
385 .Vb 4
386 \&  has 'stress' => ( 
387 \&      is  => 'rw',
388 \&      isa => 'Int',
389 \&  );
390 .Ve
391 .PP
392 .Vb 1
393 \&  with 'Breakable';
394 .Ve
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.
400 .PP
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:
405 .PP
406 .Vb 1
407 \&  package MovieCar;
408 .Ve
409 .PP
410 .Vb 1
411 \&  use Moose;
412 .Ve
413 .PP
414 .Vb 1
415 \&  extends 'Car';
416 .Ve
417 .PP
418 .Vb 1
419 \&  with 'Breakable', 'ExplodesOnBreakage';
420 .Ve
421 .PP
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.
431 .PP
432 .Vb 1
433 \&  package Breakdancer;
434 .Ve
435 .PP
436 .Vb 1
437 \&  use Moose::Role
438 .Ve
439 .PP
440 .Vb 1
441 \&  sub break {
442 .Ve
443 .PP
444 .Vb 1
445 \&  }
446 .Ve
447 .PP
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:
450 .PP
451 .Vb 1
452 \&  package FragileDancer;
453 .Ve
454 .PP
455 .Vb 1
456 \&  use Moose;
457 .Ve
458 .PP
459 .Vb 1
460 \&  with 'Breakable', 'Breakdancer';
461 .Ve
462 .PP
463 .Vb 1
464 \&  sub break { ... }
465 .Ve
466 .PP
467 A role can be a collection of other roles:
468 .PP
469 .Vb 1
470 \&  package Break::Bundle;
471 .Ve
472 .PP
473 .Vb 1
474 \&  use Moose::Role;
475 .Ve
476 .PP
477 .Vb 1
478 \&  with ('Breakable', 'Breakdancer');
479 .Ve
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:
484 .PP
485 .Vb 1
486 \&  package FragileDancer;
487 .Ve
488 .PP
489 .Vb 1
490 \&  use Moose;
491 .Ve
492 .PP
493 .Vb 2
494 \&  with 'Breakable'   => { \-alias => { break => 'break_bone' } },
495 \&       'Breakdancer' => { \-alias => { break => 'break_dance' } };
496 .Ve
497 .PP
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:
500 .PP
501 .Vb 8
502 \&  with 'Breakable' => {
503 \&      \-alias    => { break => 'break_bone' },
504 \&      \-excludes => 'break',
505 \&      },
506 \&      'Breakdancer' => {
507 \&      \-alias    => { break => 'break_dance' },
508 \&      \-excludes => 'break',
509 \&      };
510 .Ve
511 .PP
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.
516 .PP
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.
522 .PP
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.
525 .PP
526 Also see Moose::Cookbook::Roles::Recipe2 for an example.
527 .SH "ROLE EXCLUSION"
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
531 the role.
532 .PP
533 .Vb 1
534 \&  package Breakable;
535 .Ve
536 .PP
537 .Vb 1
538 \&  use Moose::Role;
539 .Ve
540 .PP
541 .Vb 1
542 \&  excludes 'BreakDancer';
543 .Ve
544 .SH "APPLYING ROLES"
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.
548 .PP
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.
554 .PP
555 .Vb 6
556 \&   use Moose::Util;
557 \&   ...
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;
562 .Ve
563 .PP
564 Do not apply roles to classes that have immutable subclasses, since that
565 will invalidate the metadata of the subclasses.
566 .PP
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:
569 .PP
570 .Vb 1
571 \&   Moose::Util::apply_all_roles($instance, ('MyApp::SomeRole'));
572 .Ve
573 .PP
574 Or you can use the role's meta object:
575 .PP
576 .Vb 1
577 \&   MyApp::SomeRole\->meta\->apply($instance);
578 .Ve
579 .PP
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
589 system.
590 .PP
591 The best way to do this is to use the \f(CW\*(C`apply_all_roles\*(C'\fR function from
592 Moose::Util:
593 .PP
594 .Vb 1
595 \&  use Moose::Util qw( apply_all_roles );
596 .Ve
597 .PP
598 .Vb 2
599 \&  my $car = Car\->new;
600 \&  apply_all_roles( $car, 'Breakable' );
601 .Ve
602 .PP
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.
607 .SH "AUTHOR"
608 .IX Header "AUTHOR"
609 Dave Rolsky <autarch@urth.org>
610 .SH "COPYRIGHT AND LICENSE"
611 .IX Header "COPYRIGHT AND LICENSE"
612 Copyright 2009 by Infinity Interactive, Inc.
613 .PP
614 <http://www.iinteractive.com>
615 .PP
616 This library is free software; you can redistribute it and/or modify
617 it under the same terms as Perl itself.