File Coverage

blib/lib/Moose.pm
Criterion Covered Total %
statement 146 149 97.9
branch 37 40 92.5
condition 11 12 91.6
subroutine 38 39 97.4
pod 10 12 83.3
total 242 252 96.0


line stmt bran cond sub pod time code
1 377     377   12866540 use strict;
  377         2882  
  377         12905  
2 377     377   2174 use warnings;
  377         810  
  377         20734  
3             package Moose; # git description: 2.2204-2-ged91341b0
4             our $VERSION = '2.2205';
5             our $AUTHORITY = 'cpan:STEVAN';
6              
7 377     377   8965 use 5.008003;
  377         1397  
8              
9 377     377   2526 use Scalar::Util ();
  377         929  
  377         9121  
10 377     377   2536 use Carp 'carp';
  377         983  
  377         22667  
11 377     377   103263 use Module::Runtime 'module_notional_filename';
  377         342555  
  377         2773  
12 377     377   111666 use Class::Load 'is_class_loaded';
  377         3098004  
  377         20672  
13              
14 377     377   150620 use Moose::Deprecated;
  377         1021  
  377         2157  
15 377     377   198517 use Moose::Exporter;
  377         1409  
  377         5442  
16              
17 377     377   5031 use Class::MOP;
  377         822  
  377         19407  
18              
19             die "Class::MOP version $Moose::VERSION required--this is version $Class::MOP::VERSION"
20             if $Class::MOP::VERSION ne $Moose::VERSION;
21              
22 377     377   215626 use Moose::Meta::Class;
  377         1392  
  377         15597  
23 377     377   186267 use Moose::Meta::TypeConstraint;
  377         2491  
  377         17770  
24 377     377   177550 use Moose::Meta::TypeCoercion;
  377         1435  
  377         14195  
25 377     377   2873 use Moose::Meta::Attribute;
  377         896  
  377         9035  
26 377     377   197816 use Moose::Meta::Instance;
  377         1426  
  377         14375  
27              
28 377     377   177831 use Moose::Object;
  377         1122  
  377         11516  
29              
30 377     377   207135 use Moose::Meta::Role;
  377         1518  
  377         17980  
31 377     377   196733 use Moose::Meta::Role::Composite;
  377         1495  
  377         18948  
32 377     377   183424 use Moose::Meta::Role::Application;
  377         1395  
  377         15144  
33 377     377   206717 use Moose::Meta::Role::Application::RoleSummation;
  377         1343  
  377         13312  
34 377     377   199433 use Moose::Meta::Role::Application::ToClass;
  377         1414  
  377         14322  
35 377     377   195785 use Moose::Meta::Role::Application::ToRole;
  377         1148  
  377         11972  
36 377     377   183365 use Moose::Meta::Role::Application::ToInstance;
  377         1282  
  377         12394  
37              
38 377     377   3132 use Moose::Util::TypeConstraints;
  377         1087  
  377         4606  
39 377     377   334760 use Moose::Util 'throw_exception';
  377         1206  
  377         2500  
40              
41 377     377   265582 use Moose::Meta::Attribute::Native;
  377         1506  
  377         488030  
42              
43             sub extends {
44 622     622 1 1855 my $meta = shift;
45              
46 622 100       2538 unless ( @_ )
47             {
48 1         25 throw_exception( ExtendsMissingArgs => class_name => $meta->name );
49             }
50             # this checks the metaclass to make sure
51             # it is correct, sometimes it can get out
52             # of sync when the classes are being built
53 621         3064 $meta->superclasses(@_);
54             }
55              
56             sub with {
57 551     551 1 3903 Moose::Util::apply_all_roles(shift, @_);
58             }
59              
60             sub throw_error {
61 2     2 0 354 shift;
62 2         14 Class::MOP::Object->throw_error(@_);
63             }
64              
65             sub has {
66 1499     1499 1 4103 my $meta = shift;
67 1499         3141 my $name = shift;
68              
69 1499         5772 my %context = Moose::Util::_caller_info;
70 1499         4805 $context{context} = 'has declaration';
71 1499         3718 $context{type} = 'class';
72 1499         6443 my @options = ( definition_context => \%context, @_ );
73 1499 100       5922 my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
74 1499         8054 $meta->add_attribute( $_, @options ) for @$attrs;
75             }
76              
77             sub before {
78 7     7 1 44 Moose::Util::add_method_modifier(shift, 'before', \@_);
79             }
80              
81             sub after {
82 6     6 1 37 Moose::Util::add_method_modifier(shift, 'after', \@_);
83             }
84              
85             sub around {
86 9     9 1 70 Moose::Util::add_method_modifier(shift, 'around', \@_);
87             }
88              
89             our $SUPER_PACKAGE;
90             our $SUPER_BODY;
91             our @SUPER_ARGS;
92              
93             sub super {
94 17 50   17 1 119 if (@_) {
95 0         0 carp 'Arguments passed to super() are ignored';
96             }
97              
98             # This check avoids a recursion loop - see
99             # t/bugs/super_recursion.t
100 17 100 100     89 return if defined $SUPER_PACKAGE && $SUPER_PACKAGE ne caller();
101 16 100       42 return unless $SUPER_BODY; $SUPER_BODY->(@SUPER_ARGS);
  15         46  
102             }
103              
104             sub override {
105 18     18 1 41 my $meta = shift;
106 18         52 my ( $name, $method ) = @_;
107 18         79 $meta->add_override_method_modifier( $name => $method );
108             }
109              
110             sub inner {
111 27     27 1 2532 my $pkg = caller();
112 27         43 our ( %INNER_BODY, %INNER_ARGS );
113              
114 27 100       79 if ( my $body = $INNER_BODY{$pkg} ) {
115 19         27 my @args = @{ $INNER_ARGS{$pkg} };
  19         48  
116 19         42 local $INNER_ARGS{$pkg};
117 19         33 local $INNER_BODY{$pkg};
118 19         61 return $body->(@args);
119             } else {
120 8         22 return;
121             }
122             }
123              
124             sub augment {
125 16     16 1 41 my $meta = shift;
126 16         46 my ( $name, $method ) = @_;
127 16         91 $meta->add_augment_method_modifier( $name => $method );
128             }
129              
130             Moose::Exporter->setup_import_methods(
131             with_meta => [
132             qw( extends with has before after around override augment )
133             ],
134             as_is => [
135             qw( super inner ),
136             'Carp::confess',
137             'Scalar::Util::blessed',
138             ],
139             );
140              
141             sub init_meta {
142 1518     1518 0 4282 shift;
143 1518         7194 my %args = @_;
144              
145             my $class = $args{for_class}
146 1518 100       6080 or throw_exception( InitMetaRequiresClass => params => \%args );
147              
148 1517   100     7695 my $base_class = $args{base_class} || 'Moose::Object';
149 1517   100     6257 my $metaclass = $args{metaclass} || 'Moose::Meta::Class';
150 1517 100       4815 my $meta_name = exists $args{meta_name} ? $args{meta_name} : 'meta';
151              
152 1517 100       10239 throw_exception( MetaclassNotLoaded => class_name => $metaclass )
153             unless is_class_loaded($metaclass);
154              
155 1516 100       9027 throw_exception( MetaclassMustBeASubclassOfMooseMetaClass => class_name => $metaclass )
156             unless $metaclass->isa('Moose::Meta::Class');
157              
158             # make a subtype for each Moose class
159 1515 100       6400 class_type($class)
160             unless find_type_constraint($class);
161              
162 1515         3655 my $meta;
163              
164 1515 100       5918 if ( $meta = Class::MOP::get_metaclass_by_name($class) ) {
165 68 100       375 unless ( $meta->isa("Moose::Meta::Class") ) {
166 3 100       22 if ( $meta->isa('Moose::Meta::Role') ) {
167 1         6 throw_exception( MetaclassIsARoleNotASubclassOfGivenMetaclass => role_name => $class,
168             metaclass => $metaclass,
169             role => $meta
170             );
171             } else {
172 2         13 throw_exception( MetaclassIsNotASubclassOfGivenMetaclass => class_name => $class,
173             metaclass => $metaclass,
174             class => $meta
175             );
176             }
177             }
178             } else {
179             # no metaclass
180              
181             # now we check whether our ancestors have metaclass, and if so borrow that
182 1447         2917 my ( undef, @isa ) = @{ mro::get_linear_isa($class) };
  1447         11338  
183              
184 1447         4757 foreach my $ancestor ( @isa ) {
185 9   100     33 my $ancestor_meta = Class::MOP::get_metaclass_by_name($ancestor) || next;
186              
187 5         30 my $ancestor_meta_class = $ancestor_meta->_real_ref_name;
188              
189             # if we have an ancestor metaclass that inherits $metaclass, we use
190             # that. This is like _fix_metaclass_incompatibility, but we can do it now.
191              
192             # the case of having an ancestry is not very common, but arises in
193             # e.g. Reaction
194 5 100       31 unless ( $metaclass->isa( $ancestor_meta_class ) ) {
195 1 50       11 if ( $ancestor_meta_class->isa($metaclass) ) {
196 1         2 $metaclass = $ancestor_meta_class;
197             }
198             }
199             }
200              
201 1447         7336 $meta = $metaclass->initialize($class);
202 1447         8171 my $filename = module_notional_filename($meta->name);
203             $INC{$filename} = '(set by Moose)'
204 1447 100       41521 unless exists $INC{$filename};
205             }
206              
207 1512 100       5407 if (defined $meta_name) {
208             # also check for inherited non moose 'meta' method?
209 1511         6715 my $existing = $meta->get_method($meta_name);
210 1511 50 66     6739 if ($existing && !$existing->isa('Class::MOP::Method::Meta')) {
211 0         0 Carp::cluck "Moose is overwriting an existing method named "
212             . "$meta_name in class $class with a method "
213             . "which returns the class's metaclass. If this is "
214             . "actually what you want, you should remove the "
215             . "existing method, otherwise, you should rename or "
216             . "disable this generated method using the "
217             . "'-meta_name' option to 'use Moose'.";
218             }
219 1511         6307 $meta->_add_meta_method($meta_name);
220             }
221              
222             # make sure they inherit from Moose::Object
223 1512 100       7088 $meta->superclasses($base_class)
224             unless $meta->superclasses();
225              
226 1512         6988 return $meta;
227             }
228              
229             # This may be used in some older MooseX extensions.
230             sub _get_caller {
231 0     0     goto &Moose::Exporter::_get_caller;
232             }
233              
234             ## make 'em all immutable
235              
236             $_->make_immutable(
237             inline_constructor => 1,
238             constructor_name => "_new",
239             # these are Class::MOP accessors, so they need inlining
240             inline_accessors => 1
241             ) for grep { $_->is_mutable }
242             map { $_->meta }
243             qw(
244             Moose::Meta::Attribute
245             Moose::Meta::Class
246             Moose::Meta::Instance
247              
248             Moose::Meta::TypeCoercion
249             Moose::Meta::TypeCoercion::Union
250              
251             Moose::Meta::Method
252             Moose::Meta::Method::Constructor
253             Moose::Meta::Method::Destructor
254             Moose::Meta::Method::Overridden
255             Moose::Meta::Method::Augmented
256              
257             Moose::Meta::Role
258             Moose::Meta::Role::Attribute
259             Moose::Meta::Role::Method
260             Moose::Meta::Role::Method::Required
261             Moose::Meta::Role::Method::Conflicting
262              
263             Moose::Meta::Role::Composite
264              
265             Moose::Meta::Role::Application
266             Moose::Meta::Role::Application::RoleSummation
267             Moose::Meta::Role::Application::ToClass
268             Moose::Meta::Role::Application::ToRole
269             Moose::Meta::Role::Application::ToInstance
270             );
271              
272             $_->make_immutable(
273             inline_constructor => 0,
274             constructor_name => undef,
275             # these are Class::MOP accessors, so they need inlining
276             inline_accessors => 1
277             ) for grep { $_->is_mutable }
278             map { $_->meta }
279             qw(
280             Moose::Meta::Method::Accessor
281             Moose::Meta::Method::Delegation
282             Moose::Meta::Mixin::AttributeCore
283             );
284              
285             1;
286              
287             # ABSTRACT: A postmodern object system for Perl 5
288              
289             __END__
290              
291             =pod
292              
293             =encoding UTF-8
294              
295             =head1 NAME
296              
297             Moose - A postmodern object system for Perl 5
298              
299             =head1 VERSION
300              
301             version 2.2205
302              
303             =head1 SYNOPSIS
304              
305             package Point;
306             use Moose; # automatically turns on strict and warnings
307              
308             has 'x' => (is => 'rw', isa => 'Int');
309             has 'y' => (is => 'rw', isa => 'Int');
310              
311             sub clear {
312             my $self = shift;
313             $self->x(0);
314             $self->y(0);
315             }
316              
317             package Point3D;
318             use Moose;
319              
320             extends 'Point';
321              
322             has 'z' => (is => 'rw', isa => 'Int');
323              
324             after 'clear' => sub {
325             my $self = shift;
326             $self->z(0);
327             };
328              
329             =head1 DESCRIPTION
330              
331             Moose is an extension of the Perl 5 object system.
332              
333             The main goal of Moose is to make Perl 5 Object Oriented programming
334             easier, more consistent, and less tedious. With Moose you can think
335             more about what you want to do and less about the mechanics of OOP.
336              
337             Additionally, Moose is built on top of L<Class::MOP>, which is a
338             metaclass system for Perl 5. This means that Moose not only makes
339             building normal Perl 5 objects better, but it provides the power of
340             metaclass programming as well.
341              
342             =head2 New to Moose?
343              
344             If you're new to Moose, the best place to start is the
345             L<Moose::Manual> docs, followed by the L<Moose::Cookbook>. The intro
346             will show you what Moose is, and how it makes Perl 5 OO better.
347              
348             The cookbook recipes on Moose basics will get you up to speed with
349             many of Moose's features quickly. Once you have an idea of what Moose
350             can do, you can use the API documentation to get more detail on
351             features which interest you.
352              
353             =head2 Moose Extensions
354              
355             The C<MooseX::> namespace is the official place to find Moose extensions.
356             These extensions can be found on the CPAN. The easiest way to find them
357             is to search for them (L<https://metacpan.org/search?q=MooseX::>),
358             or to examine L<Task::Moose> which aims to keep an up-to-date, easily
359             installable list of Moose extensions.
360              
361             =head1 TRANSLATIONS
362              
363             Much of the Moose documentation has been translated into other languages.
364              
365             =over 4
366              
367             =item Japanese
368              
369             Japanese docs can be found at
370             L<http://perldoc.perlassociation.org/pod/Moose-Doc-JA/index.html>. The
371             source POD files can be found in GitHub:
372             L<http://github.com/jpa/Moose-Doc-JA>
373              
374             =back
375              
376             =head1 BUILDING CLASSES WITH MOOSE
377              
378             Moose makes every attempt to provide as much convenience as possible during
379             class construction/definition, but still stay out of your way if you want it
380             to. Here are a few items to note when building classes with Moose.
381              
382             When you C<use Moose>, Moose will set the class's parent class to
383             L<Moose::Object>, I<unless> the class using Moose already has a parent
384             class. In addition, specifying a parent with C<extends> will change the parent
385             class.
386              
387             Moose will also manage all attributes (including inherited ones) that are
388             defined with C<has>. And (assuming you call C<new>, which is inherited from
389             L<Moose::Object>) this includes properly initializing all instance slots,
390             setting defaults where appropriate, and performing any type constraint checking
391             or coercion.
392              
393             =head1 PROVIDED METHODS
394              
395             Moose provides a number of methods to all your classes, mostly through the
396             inheritance of L<Moose::Object>. There is however, one exception. By default,
397             Moose will install a method named C<meta> in any class which uses
398             C<Moose>. This method returns the current class's metaclass.
399              
400             If you'd like to rename this method, you can do so by passing the
401             C<-meta_name> option when using Moose:
402              
403             use Moose -meta_name => 'my_meta';
404              
405             However, the L<Moose::Object> class I<also> provides a method named C<meta>
406             which does the same thing. If your class inherits from L<Moose::Object> (which
407             is the default), then you will still have a C<meta> method. However, if your
408             class inherits from a parent which provides a C<meta> method of its own, your
409             class will inherit that instead.
410              
411             If you'd like for Moose to not install a meta method at all, you can pass
412             C<undef> as the C<-meta_name> option:
413              
414             use Moose -meta_name => undef;
415              
416             Again, you will still inherit C<meta> from L<Moose::Object> in this case.
417              
418             =head1 EXPORTED FUNCTIONS
419              
420             Moose will export a number of functions into the class's namespace which
421             may then be used to set up the class. These functions all work directly
422             on the current class.
423              
424             =head2 extends (@superclasses)
425              
426             This function will set the superclass(es) for the current class. If the parent
427             classes are not yet loaded, then C<extends> tries to load them.
428              
429             This approach is recommended instead of C<use L<base>>/C<use L<parent>>, because
430             C<use base> actually C<push>es onto the class's C<@ISA>, whereas C<extends> will
431             replace it. This is important to ensure that classes which do not have
432             superclasses still properly inherit from L<Moose::Object>.
433              
434             Each superclass can be followed by a hash reference with options. Currently,
435             only L<-version|Class::MOP/Class Loading Options> is recognized:
436              
437             extends 'My::Parent' => { -version => 0.01 },
438             'My::OtherParent' => { -version => 0.03 };
439              
440             An exception will be thrown if the version requirements are not
441             satisfied.
442              
443             =head2 with (@roles)
444              
445             This will apply a given set of C<@roles> to the local class.
446              
447             Like with C<extends>, each specified role can be followed by a hash
448             reference with a L<-version|Class::MOP/Class Loading Options> option:
449              
450             with 'My::Role' => { -version => 0.32 },
451             'My::Otherrole' => { -version => 0.23 };
452              
453             The specified version requirements must be satisfied, otherwise an
454             exception will be thrown.
455              
456             If your role takes options or arguments, they can be passed along in the
457             hash reference as well.
458              
459             You should only use one C<with>, even if you are consuming multiple roles. If
460             you consume roles using multiple C<with> statements Moose cannot detect method
461             conflicts between those roles.
462              
463             =head2 has $name|@$names =E<gt> %options
464              
465             This will install an attribute of a given C<$name> into the current class. If
466             the first parameter is an array reference, it will create an attribute for
467             every C<$name> in the list. The C<%options> will be passed to the constructor
468             for L<Moose::Meta::Attribute> (which inherits from L<Class::MOP::Attribute>),
469             so the full documentation for the valid options can be found there. These are
470             the most commonly used options:
471              
472             =over 4
473              
474             =item I<is =E<gt> 'rw'|'ro'>
475              
476             The I<is> option accepts either I<rw> (for read/write) or I<ro> (for read
477             only). These will create either a read/write accessor or a read-only
478             accessor respectively, using the same name as the C<$name> of the attribute.
479              
480             If you need more control over how your accessors are named, you can
481             use the L<reader|Class::MOP::Attribute/reader>,
482             L<writer|Class::MOP::Attribute/writer> and
483             L<accessor|Class::MOP::Attribute/accessor> options inherited from
484             L<Class::MOP::Attribute>, however if you use those, you won't need the
485             I<is> option.
486              
487             =item I<isa =E<gt> $type_name>
488              
489             The I<isa> option uses Moose's type constraint facilities to set up runtime
490             type checking for this attribute. Moose will perform the checks during class
491             construction, and within any accessors. The C<$type_name> argument must be a
492             string. The string may be either a class name or a type defined using
493             Moose's type definition features. (Refer to L<Moose::Util::TypeConstraints>
494             for information on how to define a new type, and how to retrieve type meta-data).
495              
496             =item I<coerce =E<gt> (1|0)>
497              
498             This will attempt to use coercion with the supplied type constraint to change
499             the value passed into any accessors or constructors. You B<must> supply a type
500             constraint, and that type constraint B<must> define a coercion. See
501             L<Moose::Cookbook::Basics::HTTP_SubtypesAndCoercion> for an example.
502              
503             =item I<does =E<gt> $role_name>
504              
505             This will accept the name of a role which the value stored in this attribute
506             is expected to have consumed.
507              
508             =item I<required =E<gt> (1|0)>
509              
510             This marks the attribute as being required. This means a value must be
511             supplied during class construction, I<or> the attribute must be lazy
512             and have either a default or a builder. Note that C<required> does not
513             say anything about the attribute's value, which can be C<undef>.
514              
515             =item I<weak_ref =E<gt> (1|0)>
516              
517             This will tell the class to store the value of this attribute as a weakened
518             reference. If an attribute is a weakened reference, it B<cannot> also be
519             coerced. Note that when a weak ref expires, the attribute's value becomes
520             undefined, and is still considered to be set for purposes of predicate,
521             default, etc.
522              
523             =item I<lazy =E<gt> (1|0)>
524              
525             This will tell the class to not create this slot until absolutely necessary.
526             If an attribute is marked as lazy it B<must> have a default or builder
527             supplied.
528              
529             =item I<trigger =E<gt> $code>
530              
531             The I<trigger> option is a CODE reference which will be called after
532             the value of the attribute is set. The CODE ref is passed the
533             instance itself, the updated value, and the original value if the
534             attribute was already set.
535              
536             You B<can> have a trigger on a read-only attribute.
537              
538             B<NOTE:> Triggers will only fire when you B<assign> to the attribute,
539             either in the constructor, or using the writer. Default and built values will
540             B<not> cause the trigger to be fired.
541              
542             =item I<handles =E<gt> ARRAY | HASH | REGEXP | ROLE | ROLETYPE | DUCKTYPE | CODE>
543              
544             The I<handles> option provides Moose classes with automated delegation features.
545             This is a pretty complex and powerful option. It accepts many different option
546             formats, each with its own benefits and drawbacks.
547              
548             B<NOTE:> The class being delegated to does not need to be a Moose based class,
549             which is why this feature is especially useful when wrapping non-Moose classes.
550              
551             All I<handles> option formats share the following traits:
552              
553             You cannot override a locally defined method with a delegated method; an
554             exception will be thrown if you try. That is to say, if you define C<foo> in
555             your class, you cannot override it with a delegated C<foo>. This is almost never
556             something you would want to do, and if it is, you should do it by hand and not
557             use Moose.
558              
559             You cannot override any of the methods found in Moose::Object, or the C<BUILD>
560             and C<DEMOLISH> methods. These will not throw an exception, but will silently
561             move on to the next method in the list. My reasoning for this is that you would
562             almost never want to do this, since it usually breaks your class. As with
563             overriding locally defined methods, if you do want to do this, you should do it
564             manually, not with Moose.
565              
566             You do not I<need> to have a reader (or accessor) for the attribute in order
567             to delegate to it. Moose will create a means of accessing the value for you,
568             however this will be several times B<less> efficient then if you had given
569             the attribute a reader (or accessor) to use.
570              
571             Below is the documentation for each option format:
572              
573             =over 4
574              
575             =item C<ARRAY>
576              
577             This is the most common usage for I<handles>. You basically pass a list of
578             method names to be delegated, and Moose will install a delegation method
579             for each one.
580              
581             =item C<HASH>
582              
583             This is the second most common usage for I<handles>. Instead of a list of
584             method names, you pass a HASH ref where each key is the method name you
585             want installed locally, and its value is the name of the original method
586             in the class being delegated to.
587              
588             This can be very useful for recursive classes like trees. Here is a
589             quick example (soon to be expanded into a Moose::Cookbook recipe):
590              
591             package Tree;
592             use Moose;
593              
594             has 'node' => (is => 'rw', isa => 'Any');
595              
596             has 'children' => (
597             is => 'ro',
598             isa => 'ArrayRef',
599             default => sub { [] }
600             );
601              
602             has 'parent' => (
603             is => 'rw',
604             isa => 'Tree',
605             weak_ref => 1,
606             handles => {
607             parent_node => 'node',
608             siblings => 'children',
609             }
610             );
611              
612             In this example, the Tree package gets C<parent_node> and C<siblings> methods,
613             which delegate to the C<node> and C<children> methods (respectively) of the Tree
614             instance stored in the C<parent> slot.
615              
616             You may also use an array reference to curry arguments to the original method.
617              
618             has 'thing' => (
619             ...
620             handles => { set_foo => [ set => 'foo' ] },
621             );
622              
623             # $self->set_foo(...) calls $self->thing->set('foo', ...)
624              
625             The first element of the array reference is the original method name, and the
626             rest is a list of curried arguments.
627              
628             =item C<REGEXP>
629              
630             The regexp option works very similar to the ARRAY option, except that it builds
631             the list of methods for you. It starts by collecting all possible methods of the
632             class being delegated to, then filters that list using the regexp supplied here.
633              
634             B<NOTE:> An I<isa> option is required when using the regexp option format. This
635             is so that we can determine (at compile time) the method list from the class.
636             Without an I<isa> this is just not possible.
637              
638             =item C<ROLE> or C<ROLETYPE>
639              
640             With the role option, you specify the name of a role or a
641             L<role type|Moose::Meta::TypeConstraint::Role> whose "interface" then becomes
642             the list of methods to handle. The "interface" can be defined as; the methods
643             of the role and any required methods of the role. It should be noted that this
644             does B<not> include any method modifiers or generated attribute methods (which
645             is consistent with role composition).
646              
647             =item C<DUCKTYPE>
648              
649             With the duck type option, you pass a duck type object whose "interface" then
650             becomes the list of methods to handle. The "interface" can be defined as the
651             list of methods passed to C<duck_type> to create a duck type object. For more
652             information on C<duck_type> please check
653             L<Moose::Util::TypeConstraints>.
654              
655             =item C<CODE>
656              
657             This is the option to use when you really want to do something funky. You should
658             only use it if you really know what you are doing, as it involves manual
659             metaclass twiddling.
660              
661             This takes a code reference, which should expect two arguments. The first is the
662             attribute meta-object this I<handles> is attached to. The second is the
663             metaclass of the class being delegated to. It expects you to return a hash (not
664             a HASH ref) of the methods you want mapped.
665              
666             =back
667              
668             =item I<traits =E<gt> [ @role_names ]>
669              
670             This tells Moose to take the list of C<@role_names> and apply them to the
671             attribute meta-object. Custom attribute metaclass traits are useful for
672             extending the capabilities of the I<has> keyword: they are the simplest way to
673             extend the MOP, but they are still a fairly advanced topic and too much to
674             cover here.
675              
676             See L<Metaclass and Trait Name Resolution> for details on how a trait name is
677             resolved to a role name.
678              
679             Also see L<Moose::Cookbook::Meta::Labeled_AttributeTrait> for a metaclass
680             trait example.
681              
682             =item I<builder> => Str
683              
684             The value of this key is the name of the method that will be called to obtain
685             the value used to initialize the attribute. See the L<builder option docs in
686             Class::MOP::Attribute|Class::MOP::Attribute/builder> and/or
687             L<Moose::Cookbook::Basics::BinaryTree_BuilderAndLazyBuild> for more
688             information.
689              
690             =item I<default> => SCALAR | CODE
691              
692             The value of this key is the default value which will initialize the attribute.
693              
694             NOTE: If the value is a simple scalar (string or number), then it can
695             be just passed as is. However, if you wish to initialize it with a
696             HASH or ARRAY ref, then you need to wrap that inside a CODE reference.
697             See the L<default option docs in
698             Class::MOP::Attribute|Class::MOP::Attribute/default> for more
699             information.
700              
701             =item I<clearer> => Str
702              
703             Creates a method allowing you to clear the value. See the L<clearer option
704             docs in Class::MOP::Attribute|Class::MOP::Attribute/clearer> for more
705             information.
706              
707             =item I<predicate> => Str
708              
709             Creates a method to perform a basic test to see if a value has been set in the
710             attribute. See the L<predicate option docs in
711             Class::MOP::Attribute|Class::MOP::Attribute/predicate> for more information.
712              
713             Note that the predicate will return true even for a C<weak_ref> attribute
714             whose value has expired.
715              
716             =item I<documentation> => $string
717              
718             An arbitrary string that can be retrieved later by calling C<<
719             $attr->documentation >>.
720              
721             =back
722              
723             =head2 has +$name =E<gt> %options
724              
725             This is variation on the normal attribute creator C<has> which allows you to
726             clone and extend an attribute from a superclass or from a role. Here is an
727             example of the superclass usage:
728              
729             package Foo;
730             use Moose;
731              
732             has 'message' => (
733             is => 'rw',
734             isa => 'Str',
735             default => 'Hello, I am a Foo'
736             );
737              
738             package My::Foo;
739             use Moose;
740              
741             extends 'Foo';
742              
743             has '+message' => (default => 'Hello I am My::Foo');
744              
745             What is happening here is that B<My::Foo> is cloning the C<message> attribute
746             from its parent class B<Foo>, retaining the C<is =E<gt> 'rw'> and C<isa =E<gt>
747             'Str'> characteristics, but changing the value in C<default>.
748              
749             Here is another example, but within the context of a role:
750              
751             package Foo::Role;
752             use Moose::Role;
753              
754             has 'message' => (
755             is => 'rw',
756             isa => 'Str',
757             default => 'Hello, I am a Foo'
758             );
759              
760             package My::Foo;
761             use Moose;
762              
763             with 'Foo::Role';
764              
765             has '+message' => (default => 'Hello I am My::Foo');
766              
767             In this case, we are basically taking the attribute which the role supplied
768             and altering it within the bounds of this feature.
769              
770             Note that you can only extend an attribute from either a superclass or a role,
771             you cannot extend an attribute in a role that composes over an attribute from
772             another role.
773              
774             Aside from where the attributes come from (one from superclass, the other
775             from a role), this feature works exactly the same. This feature is restricted
776             somewhat, so as to try and force at least I<some> sanity into it. Most options work the same, but there are some exceptions:
777              
778             =over 4
779              
780             =item I<reader>
781              
782             =item I<writer>
783              
784             =item I<accessor>
785              
786             =item I<clearer>
787              
788             =item I<predicate>
789              
790             These options can be added, but cannot override a superclass definition.
791              
792             =item I<traits>
793              
794             You are allowed to B<add> additional traits to the C<traits> definition.
795             These traits will be composed into the attribute, but preexisting traits
796             B<are not> overridden, or removed.
797              
798             =back
799              
800             =head2 before $name|@names|\@names|qr/.../ =E<gt> sub { ... }
801              
802             =head2 after $name|@names|\@names|qr/.../ =E<gt> sub { ... }
803              
804             =head2 around $name|@names|\@names|qr/.../ =E<gt> sub { ... }
805              
806             These three items are syntactic sugar for the before, after, and around method
807             modifier features that L<Class::MOP> provides. More information on these may be
808             found in L<Moose::Manual::MethodModifiers> and the
809             L<Class::MOP::Class documentation|Class::MOP::Class/"Method Modifiers">.
810              
811             =head2 override ($name, &sub)
812              
813             An C<override> method is a way of explicitly saying "I am overriding this
814             method from my superclass". You can call C<super> within this method, and
815             it will work as expected. The same thing I<can> be accomplished with a normal
816             method call and the C<SUPER::> pseudo-package; it is really your choice.
817              
818             =head2 super
819              
820             The keyword C<super> is a no-op when called outside of an C<override> method. In
821             the context of an C<override> method, it will call the next most appropriate
822             superclass method with the same arguments as the original method.
823              
824             =head2 augment ($name, &sub)
825              
826             An C<augment> method, is a way of explicitly saying "I am augmenting this
827             method from my superclass". Once again, the details of how C<inner> and
828             C<augment> work is best described in the
829             L<Moose::Cookbook::Basics::Document_AugmentAndInner>.
830              
831             =head2 inner
832              
833             The keyword C<inner>, much like C<super>, is a no-op outside of the context of
834             an C<augment> method. You can think of C<inner> as being the inverse of
835             C<super>; the details of how C<inner> and C<augment> work is best described in
836             the L<Moose::Cookbook::Basics::Document_AugmentAndInner>.
837              
838             =head2 blessed
839              
840             This is the C<Scalar::Util::blessed> function. It is highly recommended that
841             this is used instead of C<ref> anywhere you need to test for an object's class
842             name.
843              
844             =head2 confess
845              
846             This is the C<Carp::confess> function, and exported here for historical
847             reasons.
848              
849             =head1 METACLASS
850              
851             When you use Moose, you can specify traits which will be applied to your
852             metaclass:
853              
854             use Moose -traits => 'My::Trait';
855              
856             This is very similar to the attribute traits feature. When you do
857             this, your class's C<meta> object will have the specified traits
858             applied to it.
859              
860             =head2 Metaclass and Trait Name Resolution
861              
862             By default, when given a trait name, Moose simply tries to load a
863             class of the same name. If such a class does not exist, it then looks
864             for a class matching
865             B<Moose::Meta::$type::Custom::Trait::$trait_name>. The C<$type>
866             variable here will be one of B<Attribute> or B<Class>, depending on
867             what the trait is being applied to.
868              
869             If a class with this long name exists, Moose checks to see if it has
870             the method C<register_implementation>. This method is expected to
871             return the I<real> class name of the trait. If there is no
872             C<register_implementation> method, it will fall back to using
873             B<Moose::Meta::$type::Custom::Trait::$trait> as the trait name.
874              
875             The lookup method for metaclasses is the same, except that it looks
876             for a class matching B<Moose::Meta::$type::Custom::$metaclass_name>.
877              
878             If all this is confusing, take a look at
879             L<Moose::Cookbook::Meta::Labeled_AttributeTrait>, which demonstrates how to
880             create an attribute trait.
881              
882             =head1 UNIMPORTING FUNCTIONS
883              
884             =head2 B<unimport>
885              
886             Moose offers a way to remove the keywords it exports, through the C<unimport>
887             method. You simply have to say C<no Moose> at the bottom of your code for this
888             to work. Here is an example:
889              
890             package Person;
891             use Moose;
892              
893             has 'first_name' => (is => 'rw', isa => 'Str');
894             has 'last_name' => (is => 'rw', isa => 'Str');
895              
896             sub full_name {
897             my $self = shift;
898             $self->first_name . ' ' . $self->last_name
899             }
900              
901             no Moose; # keywords are removed from the Person package
902              
903             =head1 EXTENDING AND EMBEDDING MOOSE
904              
905             To learn more about extending Moose, we recommend checking out the
906             "Extending" recipes in the L<Moose::Cookbook>, starting with
907             L<Moose::Cookbook::Extending::ExtensionOverview>, which provides an overview of
908             all the different ways you might extend Moose. L<Moose::Exporter> and
909             L<Moose::Util::MetaRole> are the modules which provide the majority of the
910             extension functionality, so reading their documentation should also be helpful.
911              
912             =head2 The MooseX:: namespace
913              
914             Generally if you're writing an extension I<for> Moose itself you'll want
915             to put your extension in the C<MooseX::> namespace. This namespace is
916             specifically for extensions that make Moose better or different in some
917             fundamental way. It is traditionally B<not> for a package that just happens
918             to use Moose. This namespace follows from the examples of the C<LWPx::>
919             and C<DBIx::> namespaces that perform the same function for C<LWP> and C<DBI>
920             respectively.
921              
922             =head1 METACLASS COMPATIBILITY AND MOOSE
923              
924             Metaclass compatibility is a thorny subject. You should start by
925             reading the "About Metaclass compatibility" section in the
926             L<Class::MOP> docs.
927              
928             Moose will attempt to resolve a few cases of metaclass incompatibility
929             when you set the superclasses for a class, in addition to the cases that
930             L<Class::MOP> handles.
931              
932             Moose tries to determine if the metaclasses only "differ by roles". This
933             means that the parent and child's metaclass share a common ancestor in
934             their respective hierarchies, and that the subclasses under the common
935             ancestor are only different because of role applications. This case is
936             actually fairly common when you mix and match various C<MooseX::*>
937             modules, many of which apply roles to the metaclass.
938              
939             If the parent and child do differ by roles, Moose replaces the
940             metaclass in the child with a newly created metaclass. This metaclass
941             is a subclass of the parent's metaclass which does all of the roles that
942             the child's metaclass did before being replaced. Effectively, this
943             means the new metaclass does all of the roles done by both the
944             parent's and child's original metaclasses.
945              
946             Ultimately, this is all transparent to you except in the case of an
947             unresolvable conflict.
948              
949             =head1 CAVEATS
950              
951             It should be noted that C<super> and C<inner> B<cannot> be used in the same
952             method. However, they may be combined within the same class hierarchy; see
953             F<t/basics/override_augment_inner_super.t> for an example.
954              
955             The reason for this is that C<super> is only valid within a method
956             with the C<override> modifier, and C<inner> will never be valid within an
957             C<override> method. In fact, C<augment> will skip over any C<override> methods
958             when searching for its appropriate C<inner>.
959              
960             This might seem like a restriction, but I am of the opinion that keeping these
961             two features separate (yet interoperable) actually makes them easy to use, since
962             their behavior is then easier to predict. Time will tell whether I am right or
963             not (UPDATE: so far so good).
964              
965             =head1 GETTING HELP
966              
967             We offer both a mailing list and a very active IRC channel.
968              
969             The mailing list is L<mailto:moose@perl.org>. You must be subscribed to send
970             a message. To subscribe, send an empty message to
971             L<mailto:moose-subscribe@perl.org>
972              
973             You can also visit us at C<#moose> on L<irc://irc.perl.org/#moose>
974             This channel is quite active, and questions at all levels (on Moose-related
975             topics ;) are welcome.
976              
977             =head1 WHAT DOES MOOSE STAND FOR?
978              
979             Moose doesn't stand for one thing in particular, however, if you want, here
980             are a few of our favorites. Feel free to contribute more!
981              
982             =over 4
983              
984             =item * Make Other Object Systems Envious
985              
986             =item * Makes Object Orientation So Easy
987              
988             =item * Makes Object Orientation Spiffy- Er (sorry ingy)
989              
990             =item * Most Other Object Systems Emasculate
991              
992             =item * Moose Often Ovulate Sorta Early
993              
994             =item * Moose Offers Often Super Extensions
995              
996             =item * Meta Object Obligates Salivary Excitation
997              
998             =item * Meta Object Orientation Syntax Extensions
999              
1000             =item * Moo, Only Overengineered, Slow, and Execrable (blame rjbs!)
1001              
1002             =item * Massive Object-Oriented Stacktrace Emitter
1003              
1004             =back
1005              
1006             =head1 ACKNOWLEDGEMENTS
1007              
1008             =over 4
1009              
1010             =item I blame Sam Vilain for introducing me to the insanity that is meta-models.
1011              
1012             =item I blame Audrey Tang for then encouraging my meta-model habit in #perl6.
1013              
1014             =item Without Yuval "nothingmuch" Kogman this module would not be possible,
1015             and it certainly wouldn't have this name ;P
1016              
1017             =item The basis of the TypeContraints module was Rob Kinyon's idea
1018             originally, I just ran with it.
1019              
1020             =item Thanks to mst & chansen and the whole #moose posse for all the
1021             early ideas/feature-requests/encouragement/bug-finding.
1022              
1023             =item Thanks to David "Theory" Wheeler for meta-discussions and spelling fixes.
1024              
1025             =back
1026              
1027             =head1 SEE ALSO
1028              
1029             =over 4
1030              
1031             =item L<http://moose.perl.org/>
1032              
1033             This is the official web home of Moose. It contains links to our public git
1034             repository, as well as links to a number of talks and articles on Moose and
1035             Moose related technologies.
1036              
1037             =item the L<Moose manual|Moose::Manual>
1038              
1039             This is an introduction to Moose which covers most of the basics.
1040              
1041             =item Modern Perl, by chromatic
1042              
1043             This is an introduction to modern Perl programming, which includes a section on
1044             Moose. It is available in print and as a free download from
1045             L<http://onyxneon.com/books/modern_perl/>.
1046              
1047             =item The Moose is flying, a tutorial by Randal Schwartz
1048              
1049             Part 1 - L<http://www.stonehenge.com/merlyn/LinuxMag/col94.html>
1050              
1051             Part 2 - L<http://www.stonehenge.com/merlyn/LinuxMag/col95.html>
1052              
1053             =item Several Moose extension modules in the C<MooseX::> namespace.
1054              
1055             See L<https://metacpan.org/search?q=MooseX::> for extensions.
1056              
1057             =back
1058              
1059             =head2 Books
1060              
1061             =over 4
1062              
1063             =item The Art of the MetaObject Protocol
1064              
1065             I mention this in the L<Class::MOP> docs too, as this book was critical in
1066             the development of both modules and is highly recommended.
1067              
1068             =back
1069              
1070             =head2 Papers
1071              
1072             =over 4
1073              
1074             =item L<http://www.cs.utah.edu/plt/publications/oopsla04-gff.pdf>
1075              
1076             This paper (suggested by lbr on #moose) was what lead to the implementation
1077             of the C<super>/C<override> and C<inner>/C<augment> features. If you really
1078             want to understand them, I suggest you read this.
1079              
1080             =back
1081              
1082             =head1 BUGS
1083              
1084             All complex software has bugs lurking in it, and this module is no
1085             exception.
1086              
1087             Please report any bugs to C<bug-moose@rt.cpan.org>, or through the web
1088             interface at L<http://rt.cpan.org>. You can also submit a C<TODO> test as a
1089             pull request at L<https://github.com/moose/Moose>.
1090              
1091             You can also discuss feature requests or possible bugs on the Moose mailing
1092             list (moose@perl.org) or on IRC at L<irc://irc.perl.org/#moose>.
1093              
1094             =head1 FEATURE REQUESTS
1095              
1096             We are very strict about what features we add to the Moose core, especially
1097             the user-visible features. Instead we have made sure that the underlying
1098             meta-system of Moose is as extensible as possible so that you can add your
1099             own features easily.
1100              
1101             That said, occasionally there is a feature needed in the meta-system
1102             to support your planned extension, in which case you should either
1103             email the mailing list (moose@perl.org) or join us on IRC at
1104             L<irc://irc.perl.org/#moose> to discuss. The
1105             L<Moose::Manual::Contributing> has more detail about how and when you
1106             can contribute.
1107              
1108             =head1 CABAL
1109              
1110             There are only a few people with the rights to release a new version
1111             of Moose. The Moose Cabal are the people to go to with questions regarding
1112             the wider purview of Moose. They help maintain not just the code
1113             but the community as well. See the list below under L</AUTHORS>.
1114              
1115             =head1 CONTRIBUTORS
1116              
1117             Moose is a community project, and as such, involves the work of many, many
1118             members of the community beyond just the members in the cabal. In particular:
1119              
1120             Dave (autarch) Rolsky wrote most of the documentation in L<Moose::Manual>.
1121              
1122             John (jgoulah) Goulah wrote L<Moose::Cookbook::Snack::Keywords>.
1123              
1124             Jess (castaway) Robinson wrote L<Moose::Cookbook::Snack::Types>.
1125              
1126             Aran (bluefeet) Clary Deltac wrote
1127             L<Moose::Cookbook::Basics::Genome_OverloadingSubtypesAndCoercion>.
1128              
1129             Anders (Debolaz) Nor Berle contributed L<Test::Moose> and L<Moose::Util>.
1130              
1131             Also, the code in L<Moose::Meta::Attribute::Native> is based on code from the
1132             L<MooseX::AttributeHelpers> distribution, which had contributions from:
1133              
1134             Chris (perigrin) Prather
1135              
1136             Cory (gphat) Watson
1137              
1138             Evan Carroll
1139              
1140             Florian (rafl) Ragwitz
1141              
1142             Jason May
1143              
1144             Jay Hannah
1145              
1146             Jesse (doy) Luehrs
1147              
1148             Paul (frodwith) Driver
1149              
1150             Robert (rlb3) Boone
1151              
1152             Robert Buels
1153              
1154             Robert (phaylon) Sedlacek
1155              
1156             Shawn (Sartak) Moore
1157              
1158             Stevan Little
1159              
1160             Tom (dec) Lanyon
1161              
1162             Yuval Kogman
1163              
1164             Finally, these people also contributed various tests, bug fixes,
1165             documentation, and features to the Moose codebase:
1166              
1167             Aankhen
1168              
1169             Adam (Alias) Kennedy
1170              
1171             Christian (chansen) Hansen
1172              
1173             Cory (gphat) Watson
1174              
1175             Dylan Hardison (doc fixes)
1176              
1177             Eric (ewilhelm) Wilhelm
1178              
1179             Evan Carroll
1180              
1181             Guillermo (groditi) Roditi
1182              
1183             Jason May
1184              
1185             Jay Hannah
1186              
1187             Jonathan (jrockway) Rockway
1188              
1189             Matt (mst) Trout
1190              
1191             Nathan (kolibrie) Gray
1192              
1193             Paul (frodwith) Driver
1194              
1195             Piotr (dexter) Roszatycki
1196              
1197             Robert Buels
1198              
1199             Robert (phaylon) Sedlacek
1200              
1201             Robert (rlb3) Boone
1202              
1203             Sam (mugwump) Vilain
1204              
1205             Scott (konobi) McWhirter
1206              
1207             Shlomi (rindolf) Fish
1208              
1209             Tom (dec) Lanyon
1210              
1211             Wallace (wreis) Reis
1212              
1213             ... and many other #moose folks
1214              
1215             =head1 AUTHORS
1216              
1217             =over 4
1218              
1219             =item *
1220              
1221             Stevan Little <stevan@cpan.org>
1222              
1223             =item *
1224              
1225             Dave Rolsky <autarch@urth.org>
1226              
1227             =item *
1228              
1229             Jesse Luehrs <doy@cpan.org>
1230              
1231             =item *
1232              
1233             Shawn M Moore <sartak@cpan.org>
1234              
1235             =item *
1236              
1237             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
1238              
1239             =item *
1240              
1241             Karen Etheridge <ether@cpan.org>
1242              
1243             =item *
1244              
1245             Florian Ragwitz <rafl@debian.org>
1246              
1247             =item *
1248              
1249             Hans Dieter Pearcey <hdp@cpan.org>
1250              
1251             =item *
1252              
1253             Chris Prather <chris@prather.org>
1254              
1255             =item *
1256              
1257             Matt S Trout <mstrout@cpan.org>
1258              
1259             =back
1260              
1261             =head1 COPYRIGHT AND LICENSE
1262              
1263             This software is copyright (c) 2006 by Infinity Interactive, Inc.
1264              
1265             This is free software; you can redistribute it and/or modify it under
1266             the same terms as the Perl 5 programming language system itself.
1267              
1268             =cut