This file is indexed.

/usr/lib/perl5/Moose/Manual/Roles.pod is in libmoose-perl 2.1005-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package Moose::Manual::Roles;

# ABSTRACT: Roles, an alternative to deep hierarchies and base classes

__END__

=pod

=head1 NAME

Moose::Manual::Roles - Roles, an alternative to deep hierarchies and base classes

=head1 VERSION

version 2.1005

=head1 WHAT IS A ROLE?

A role encapsulates some piece of behavior or state that can be shared between
classes. It is something that classes I<do>. It is important to understand that
I<roles are not classes>. You cannot inherit from a role, and a role cannot be
instantiated. We sometimes say that roles are I<consumed>, either by classes
or other roles.

Instead, a role is I<composed> into a class. In practical terms, this
means that all of the methods, method modifiers, and attributes defined in a role are
added directly to (we sometimes say "flattened into") the class that
consumes the role. These attributes and methods then appear as if they
were defined in the class itself. A subclass of the consuming class
will inherit all of these methods and attributes.

Moose roles are similar to mixins or interfaces in other languages.

Besides defining their own methods and attributes, roles can also
require that the consuming class define certain methods of its
own. You could have a role that consisted only of a list of required
methods, in which case the role would be very much like a Java
interface.

Note that attribute accessors also count as methods for the
purposes of satisfying the requirements of a role.

=head1 A SIMPLE ROLE

Creating a role looks a lot like creating a Moose class:

  package Breakable;

  use Moose::Role;

  has 'is_broken' => (
      is  => 'rw',
      isa => 'Bool',
  );

  sub break {
      my $self = shift;

      print "I broke\n";

      $self->is_broken(1);
  }

Except for our use of L<Moose::Role>, this looks just like a class
definition with Moose. However, this is not a class, and it cannot be
instantiated.

Instead, its attributes and methods will be composed into classes
which use the role:

  package Car;

  use Moose;

  with 'Breakable';

  has 'engine' => (
      is  => 'ro',
      isa => 'Engine',
  );

The C<with> function composes roles into a class. Once that is done,
the C<Car> class has an C<is_broken> attribute and a C<break>
method. The C<Car> class also C<does('Breakable')>:

  my $car = Car->new( engine => Engine->new );

  print $car->is_broken ? 'Busted' : 'Still working';
  $car->break;
  print $car->is_broken ? 'Busted' : 'Still working';

  $car->does('Breakable'); # true

This prints:

  Still working
  I broke
  Busted

We could use this same role in a C<Bone> class:

  package Bone;

  use Moose;

  with 'Breakable';

  has 'marrow' => (
      is  => 'ro',
      isa => 'Marrow',
  );

See also L<Moose::Cookbook::Roles::Comparable_CodeReuse> for an example.

=head1 REQUIRED METHODS

As mentioned previously, a role can require that consuming classes
provide one or more methods. Using our C<Breakable> example, let's
make it require that consuming classes implement their own C<break>
methods:

  package Breakable;

  use Moose::Role;

  requires 'break';

  has 'is_broken' => (
      is  => 'rw',
      isa => 'Bool',
  );

  after 'break' => sub {
      my $self = shift;

      $self->is_broken(1);
  };

If we try to consume this role in a class that does not have a
C<break> method, we will get an exception.

You can see that we added a method modifier on C<break>. We want
classes that consume this role to implement their own logic for
breaking, but we make sure that the C<is_broken> attribute is always
set to true when C<break> is called.

  package Car

  use Moose;

  with 'Breakable';

  has 'engine' => (
      is  => 'ro',
      isa => 'Engine',
  );

  sub break {
      my $self = shift;

      if ( $self->is_moving ) {
          $self->stop;
      }
  }

=head2 Roles Versus Abstract Base Classes

If you are familiar with the concept of abstract base classes in other
languages, you may be tempted to use roles in the same way.

You I<can> define an "interface-only" role, one that contains I<just>
a list of required methods.

However, any class which consumes this role must implement all of the
required methods, either directly or through inheritance from a
parent. You cannot delay the method requirement check so that they can
be implemented by future subclasses.

Because the role defines the required methods directly, adding a base
class to the mix would not achieve anything. We recommend that you
simply consume the interface role in each class which implements that
interface.

=head2 Required Attributes

As mentioned before, a role's required method may also be satisfied by an
attribute accessor. However, the call to C<has> which defines an attribute
happens at runtime. This means that you must define the attribute I<before>
consuming the role, or else the role will not see the generated accessor.

  package Breakable;

  use Moose::Role;

  requires 'stress';

  package Car;

  use Moose;

  has 'stress' => (
      is  => 'rw',
      isa => 'Int',
  );

  with 'Breakable';

=head1 USING METHOD MODIFIERS

Method modifiers and roles are a very powerful combination.  Often, a
role will combine method modifiers and required methods. We already
saw one example with our C<Breakable> example.

Method modifiers increase the complexity of roles, because they make
the role application order relevant. If a class uses multiple roles,
each of which modify the same method, those modifiers will be applied
in the same order as the roles are used:

  package MovieCar;

  use Moose;

  extends 'Car';

  with 'Breakable', 'ExplodesOnBreakage';

Assuming that the new C<ExplodesOnBreakage> role I<also> has an
C<after> modifier on C<break>, the C<after> modifiers will run one
after the other. The modifier from C<Breakable> will run first, then
the one from C<ExplodesOnBreakage>.

=head1 METHOD CONFLICTS

If a class composes multiple roles, and those roles have methods of
the same name, we will have a conflict. In that case, the composing
class is required to provide its I<own> method of the same name.

  package Breakdancer;

  use Moose::Role;

  sub break {

  }

If we compose both C<Breakable> and C<Breakdancer> in a class, we must
provide our own C<break> method:

  package FragileDancer;

  use Moose;

  with 'Breakable', 'Breakdancer';

  sub break { ... }

A role can be a collection of other roles:

  package Break::Bundle;

  use Moose::Role;

  with ('Breakable', 'Breakdancer');

=head1 METHOD EXCLUSION AND ALIASING

If we want our C<FragileDancer> class to be able to call the methods
from both its roles, we can alias the methods:

  package FragileDancer;

  use Moose;

  with 'Breakable'   => { -alias => { break => 'break_bone' } },
       'Breakdancer' => { -alias => { break => 'break_dance' } };

However, aliasing a method simply makes a I<copy> of the method with
the new name. We also need to exclude the original name:

  with 'Breakable' => {
      -alias    => { break => 'break_bone' },
      -excludes => 'break',
      },
      'Breakdancer' => {
      -alias    => { break => 'break_dance' },
      -excludes => 'break',
      };

The excludes parameter prevents the C<break> method from being composed
into the C<FragileDancer> class, so we don't have a conflict. This
means that C<FragileDancer> does not need to implement its own
C<break> method.

This is useful, but it's worth noting that this breaks the contract
implicit in consuming a role. Our C<FragileDancer> class does both the
C<Breakable> and C<BreakDancer>, but does not provide a C<break>
method. If some API expects an object that does one of those roles, it
probably expects it to implement that method.

In some use cases we might alias and exclude methods from roles, but
then provide a method of the same name in the class itself.

Also see L<Moose::Cookbook::Roles::Restartable_AdvancedComposition> for an example.

=head1 ROLE EXCLUSION

A role can say that it cannot be combined with some other role. This
should be used with great caution, since it limits the re-usability of
the role.

  package Breakable;

  use Moose::Role;

  excludes 'BreakDancer';

=head1 ADDING A ROLE TO AN OBJECT INSTANCE

You may want to add a role to an object instance, rather than to a class. For
example, you may want to add debug tracing to one instance of an object while
debugging a particular bug. Another use case might be to dynamically change
objects based on a user's configuration, as a plugin system.

The best way to do this is to use the C<apply_all_roles()> function from
L<Moose::Util>:

  use Moose::Util qw( apply_all_roles );

  my $car = Car->new;
  apply_all_roles( $car, 'Breakable' );

This function can apply more than one role at a time, and will do so using the
normal Moose role combination system. We recommend using this function to
apply roles to an object. This is what Moose uses internally when you call
C<with>.

=head1 AUTHOR

Moose is maintained by the Moose Cabal, along with the help of many contributors. See L<Moose/CABAL> and L<Moose/CONTRIBUTORS> for details.

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Infinity Interactive, Inc..

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut