Be more consistent in styling and wording of definitions
[gitmo/Moose.git] / lib / Moose.pm
CommitLineData
fcd84ca9 1
2package Moose;
3
4use strict;
5use warnings;
6
ecb1297a 7use 5.008;
8
fb4fcfee 9our $VERSION = '0.57';
75b95414 10$VERSION = eval $VERSION;
d44714be 11our $AUTHORITY = 'cpan:STEVAN';
fcd84ca9 12
21f1e231 13use Scalar::Util 'blessed';
c0b37457 14use Carp 'confess', 'croak', 'cluck';
fcd84ca9 15
5bd4db9b 16use Moose::Exporter;
7f18097c 17
ecfea998 18use Class::MOP 0.65;
ef1d5f4b 19
c0e30cf5 20use Moose::Meta::Class;
7415b2cb 21use Moose::Meta::TypeConstraint;
7c13858b 22use Moose::Meta::TypeCoercion;
78cd1d3b 23use Moose::Meta::Attribute;
ddd0ec20 24use Moose::Meta::Instance;
c0e30cf5 25
0779da92 26use Moose::Object;
27
d67145ed 28use Moose::Meta::Role;
0779da92 29use Moose::Meta::Role::Composite;
30use Moose::Meta::Role::Application;
31use Moose::Meta::Role::Application::RoleSummation;
32use Moose::Meta::Role::Application::ToClass;
33use Moose::Meta::Role::Application::ToRole;
34use Moose::Meta::Role::Application::ToInstance;
d67145ed 35
7415b2cb 36use Moose::Util::TypeConstraints;
d7d8a8c7 37use Moose::Util ();
a15dff8d 38
5bd4db9b 39sub extends {
97a93056 40 my $class = shift;
3d544ed5 41
5bd4db9b 42 croak "Must derive at least one class" unless @_;
9bcfbab1 43
5bd4db9b 44 my @supers = @_;
45 foreach my $super (@supers) {
46 Class::MOP::load_class($super);
47 croak "You cannot inherit from a Moose Role ($super)"
48 if $super->can('meta') &&
49 blessed $super->meta &&
50 $super->meta->isa('Moose::Meta::Role')
c92c1205 51 }
5bee491d 52
26fbace8 53
86dd5d11 54
5bd4db9b 55 # this checks the metaclass to make sure
56 # it is correct, sometimes it can get out
57 # of sync when the classes are being built
50d5df60 58 my $meta = Moose::Meta::Class->initialize($class)->_fix_metaclass_incompatability(@supers);
5bd4db9b 59 $meta->superclasses(@supers);
60}
a3c7e2fe 61
5bd4db9b 62sub with {
97a93056 63 my $class = shift;
aedcb7d9 64 Moose::Util::apply_all_roles(Class::MOP::Class->initialize($class), @_);
5bd4db9b 65}
9bcfbab1 66
5bd4db9b 67sub has {
97a93056 68 my $class = shift;
5bd4db9b 69 my $name = shift;
70 croak 'Usage: has \'name\' => ( key => value, ... )' if @_ == 1;
71 my %options = @_;
72 my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
aedcb7d9 73 Class::MOP::Class->initialize($class)->add_attribute( $_, %options ) for @$attrs;
5bd4db9b 74}
9bcfbab1 75
5bd4db9b 76sub before {
97a93056 77 my $class = shift;
5bd4db9b 78 Moose::Util::add_method_modifier($class, 'before', \@_);
79}
80
81sub after {
97a93056 82 my $class = shift;
5bd4db9b 83 Moose::Util::add_method_modifier($class, 'after', \@_);
84}
85
86sub around {
97a93056 87 my $class = shift;
5bd4db9b 88 Moose::Util::add_method_modifier($class, 'around', \@_);
89}
90
91sub super {
92 return unless our $SUPER_BODY; $SUPER_BODY->(our @SUPER_ARGS);
93}
9bcfbab1 94
5bd4db9b 95sub override {
97a93056 96 my $class = shift;
5bd4db9b 97 my ( $name, $method ) = @_;
aedcb7d9 98 Class::MOP::Class->initialize($class)->add_override_method_modifier( $name => $method );
5bd4db9b 99}
9bcfbab1 100
5bd4db9b 101sub inner {
102 my $pkg = caller();
103 our ( %INNER_BODY, %INNER_ARGS );
104
105 if ( my $body = $INNER_BODY{$pkg} ) {
106 my @args = @{ $INNER_ARGS{$pkg} };
107 local $INNER_ARGS{$pkg};
108 local $INNER_BODY{$pkg};
109 return $body->(@args);
110 } else {
111 return;
ce265cc3 112 }
5bd4db9b 113}
9bcfbab1 114
5bd4db9b 115sub augment {
97a93056 116 my $class = shift;
5bd4db9b 117 my ( $name, $method ) = @_;
aedcb7d9 118 Class::MOP::Class->initialize($class)->add_augment_method_modifier( $name => $method );
ce265cc3 119}
9bcfbab1 120
5bd4db9b 121sub make_immutable {
97a93056 122 my $class = shift;
5bd4db9b 123 cluck "The make_immutable keyword has been deprecated, " .
124 "please go back to __PACKAGE__->meta->make_immutable\n";
aedcb7d9 125 Class::MOP::Class->initialize($class)->make_immutable(@_);
5bd4db9b 126}
9bcfbab1 127
aedcb7d9 128Moose::Exporter->setup_import_methods(
97a93056 129 with_caller => [
130 qw( extends with has before after around override augment make_immutable )
131 ],
132 as_is => [
133 qw( super inner ),
5bd4db9b 134 \&Carp::confess,
135 \&Scalar::Util::blessed,
136 ],
137);
138
cc841c0e 139sub init_meta {
085fba61 140 # This used to be called as a function. This hack preserves
141 # backwards compatibility.
142 if ( $_[0] ne __PACKAGE__ ) {
143 return __PACKAGE__->init_meta(
144 for_class => $_[0],
145 base_class => $_[1],
146 metaclass => $_[2],
147 );
148 }
7c4676ef 149
0338a411 150 shift;
151 my %args = @_;
152
153 my $class = $args{for_class}
085fba61 154 or confess "Cannot call init_meta without specifying a for_class";
155 my $base_class = $args{base_class} || 'Moose::Object';
156 my $metaclass = $args{metaclass} || 'Moose::Meta::Class';
cc841c0e 157
158 confess
159 "The Metaclass $metaclass must be a subclass of Moose::Meta::Class."
160 unless $metaclass->isa('Moose::Meta::Class');
161
162 # make a subtype for each Moose class
163 class_type($class)
164 unless find_type_constraint($class);
165
166 my $meta;
50d5df60 167
168 if ( $meta = Class::MOP::get_metaclass_by_name($class) ) {
169 unless ( $meta->isa("Moose::Meta::Class") ) {
170 confess "$class already has a metaclass, but it does not inherit $metaclass ($meta)";
171 }
172 } else {
173 # no metaclass, no 'meta' method
174
175 # now we check whether our ancestors have metaclass, and if so borrow that
176 my ( undef, @isa ) = @{ $class->mro::get_linear_isa };
177
178 foreach my $ancestor ( @isa ) {
179 my $ancestor_meta = Class::MOP::get_metaclass_by_name($ancestor) || next;
180
181 my $ancestor_meta_class = ($ancestor_meta->is_immutable
182 ? $ancestor_meta->get_mutable_metaclass_name
183 : ref($ancestor_meta));
184
185 # if we have an ancestor metaclass that inherits $metaclass, we use
186 # that. This is like _fix_metaclass_incompatability, but we can do it now.
187
188 # the case of having an ancestry is not very common, but arises in
189 # e.g. Reaction
190 unless ( $metaclass->isa( $ancestor_meta_class ) ) {
191 if ( $ancestor_meta_class->isa($metaclass) ) {
192 $metaclass = $ancestor_meta_class;
193 }
194 }
195 }
196
197 $meta = $metaclass->initialize($class);
198 }
199
cc841c0e 200 if ( $class->can('meta') ) {
50d5df60 201 # check 'meta' method
202
203 # it may be inherited
204
cc841c0e 205 # NOTE:
206 # this is the case where the metaclass pragma
207 # was used before the 'use Moose' statement to
208 # override a specific class
50d5df60 209 my $method_meta = $class->meta;
210
211 ( blessed($method_meta) && $method_meta->isa('Moose::Meta::Class') )
212 || confess "$class already has a &meta function, but it does not return a Moose::Meta::Class ($meta)";
213
214 $meta = $method_meta;
cc841c0e 215 }
50d5df60 216
217 unless ( $meta->has_method("meta") ) { # don't overwrite
218 # also check for inherited non moose 'meta' method?
219 # FIXME also skip this if the user requested by passing an option
cc841c0e 220 $meta->add_method(
221 'meta' => sub {
222 # re-initialize so it inherits properly
50d5df60 223 $metaclass->initialize( ref($_[0]) || $_[0] );
cc841c0e 224 }
225 );
226 }
227
228 # make sure they inherit from Moose::Object
229 $meta->superclasses($base_class)
230 unless $meta->superclasses();
231
232 return $meta;
233}
234
085fba61 235# This may be used in some older MooseX extensions.
236sub _get_caller {
237 goto &Moose::Exporter::_get_caller;
238}
239
8ecb1fa0 240## make 'em all immutable
241
242$_->meta->make_immutable(
0779da92 243 inline_constructor => 1,
244 constructor_name => "_new",
77a18c28 245 inline_accessors => 1, # these are Class::MOP accessors, so they need inlining
9bcfbab1 246 )
0779da92 247 for (qw(
248 Moose::Meta::Attribute
249 Moose::Meta::Class
250 Moose::Meta::Instance
251
252 Moose::Meta::TypeConstraint
253 Moose::Meta::TypeConstraint::Union
254 Moose::Meta::TypeConstraint::Parameterized
9ad786af 255 Moose::Meta::TypeConstraint::Parameterizable
0779da92 256 Moose::Meta::TypeConstraint::Enum
257 Moose::Meta::TypeConstraint::Class
258 Moose::Meta::TypeConstraint::Role
259 Moose::Meta::TypeConstraint::Registry
260 Moose::Meta::TypeCoercion
261 Moose::Meta::TypeCoercion::Union
262
263 Moose::Meta::Method
264 Moose::Meta::Method::Accessor
265 Moose::Meta::Method::Constructor
266 Moose::Meta::Method::Destructor
267 Moose::Meta::Method::Overriden
268 Moose::Meta::Method::Augmented
269
270 Moose::Meta::Role
271 Moose::Meta::Role::Method
272 Moose::Meta::Role::Method::Required
273
274 Moose::Meta::Role::Composite
275
276 Moose::Meta::Role::Application
277 Moose::Meta::Role::Application::RoleSummation
278 Moose::Meta::Role::Application::ToClass
279 Moose::Meta::Role::Application::ToRole
280 Moose::Meta::Role::Application::ToInstance
0779da92 281));
8ecb1fa0 282
fcd84ca9 2831;
284
285__END__
286
287=pod
288
289=head1 NAME
290
8bdc7f13 291Moose - A postmodern object system for Perl 5
fcd84ca9 292
293=head1 SYNOPSIS
e522431d 294
295 package Point;
1cd45431 296 use Moose; # automatically turns on strict and warnings
26fbace8 297
43d599e5 298 has 'x' => (is => 'rw', isa => 'Int');
299 has 'y' => (is => 'rw', isa => 'Int');
26fbace8 300
e522431d 301 sub clear {
302 my $self = shift;
303 $self->x(0);
26fbace8 304 $self->y(0);
e522431d 305 }
26fbace8 306
e522431d 307 package Point3D;
308 use Moose;
26fbace8 309
e522431d 310 extends 'Point';
26fbace8 311
43d599e5 312 has 'z' => (is => 'rw', isa => 'Int');
26fbace8 313
e522431d 314 after 'clear' => sub {
315 my $self = shift;
43d599e5 316 $self->z(0);
26fbace8 317 };
2c0cbef7 318
fcd84ca9 319=head1 DESCRIPTION
320
26fbace8 321Moose is an extension of the Perl 5 object system.
e522431d 322
9b9da6f1 323The main goal of Moose is to make Perl 5 Object Oriented programming
324easier, more consistent and less tedious. With Moose you can to think
6f894f30 325more about what you want to do and less about the mechanics of OOP.
fcd84ca9 326
6f894f30 327Additionally, Moose is built on top of L<Class::MOP>, which is a
328metaclass system for Perl 5. This means that Moose not only makes
329building normal Perl 5 objects better, but it provides the power of
330metaclass programming as well.
8bdc7f13 331
f5909dca 332=head2 New to Moose?
333
6f894f30 334If you're new to Moose, the best place to start is the L<Moose::Intro>
335docs, followed by the L<Moose::Cookbook>. The intro will show you what
336Moose is, and how it makes Perl 5 OO better.
337
338The cookbook recipes on Moose basics will get you up to speed with
339many of Moose's features quickly. Once you have an idea of what Moose
340can do, you can use the API documentation to get more detail on
341features which interest you.
f5909dca 342
28669f89 343=head2 Moose Extensions
344
12aed9a0 345The C<MooseX::> namespace is the official place to find Moose extensions.
346These extensions can be found on the CPAN. The easiest way to find them
347is to search for them (L<http://search.cpan.org/search?query=MooseX::>),
348or to examine L<Task::Moose> which aims to keep an up-to-date, easily
349installable list of Moose extensions.
28669f89 350
6ba6d68c 351=head1 BUILDING CLASSES WITH MOOSE
352
68efb014 353Moose makes every attempt to provide as much convenience as possible during
354class construction/definition, but still stay out of your way if you want it
355to. Here are a few items to note when building classes with Moose.
6ba6d68c 356
26fbace8 357Unless specified with C<extends>, any class which uses Moose will
6ba6d68c 358inherit from L<Moose::Object>.
359
1cd45431 360Moose will also manage all attributes (including inherited ones) that are
361defined with C<has>. And (assuming you call C<new>, which is inherited from
362L<Moose::Object>) this includes properly initializing all instance slots,
363setting defaults where appropriate, and performing any type constraint checking
364or coercion.
6ba6d68c 365
004222dc 366=head1 PROVIDED METHODS
6ba6d68c 367
004222dc 368Moose provides a number of methods to all your classes, mostly through the
369inheritance of L<Moose::Object>. There is however, one exception.
6ba6d68c 370
371=over 4
372
373=item B<meta>
374
375This is a method which provides access to the current class's metaclass.
376
004222dc 377=back
378
379=head1 EXPORTED FUNCTIONS
380
381Moose will export a number of functions into the class's namespace which
382may then be used to set up the class. These functions all work directly
383on the current class.
384
385=over 4
386
6ba6d68c 387=item B<extends (@superclasses)>
388
389This function will set the superclass(es) for the current class.
390
26fbace8 391This approach is recommended instead of C<use base>, because C<use base>
392actually C<push>es onto the class's C<@ISA>, whereas C<extends> will
393replace it. This is important to ensure that classes which do not have
68efb014 394superclasses still properly inherit from L<Moose::Object>.
6ba6d68c 395
43d599e5 396=item B<with (@roles)>
e9ec68d6 397
004222dc 398This will apply a given set of C<@roles> to the local class.
e9ec68d6 399
cd7eeaf5 400=item B<has $name =E<gt> %options>
6ba6d68c 401
26fbace8 402This will install an attribute of a given C<$name> into the current class.
403The C<%options> are the same as those provided by
404L<Class::MOP::Attribute>, in addition to the list below which are provided
43d599e5 405by Moose (L<Moose::Meta::Attribute> to be more specific):
6ba6d68c 406
407=over 4
408
076c81ed 409=item I<is =E<gt> 'rw'|'ro'>
6ba6d68c 410
26fbace8 411The I<is> option accepts either I<rw> (for read/write) or I<ro> (for read
412only). These will create either a read/write accessor or a read-only
6ba6d68c 413accessor respectively, using the same name as the C<$name> of the attribute.
414
1cd45431 415If you need more control over how your accessors are named, you can use the
416I<reader>, I<writer> and I<accessor> options inherited from
004222dc 417L<Class::MOP::Attribute>, however if you use those, you won't need the I<is>
418option.
6ba6d68c 419
076c81ed 420=item I<isa =E<gt> $type_name>
6ba6d68c 421
26fbace8 422The I<isa> option uses Moose's type constraint facilities to set up runtime
423type checking for this attribute. Moose will perform the checks during class
424construction, and within any accessors. The C<$type_name> argument must be a
425string. The string may be either a class name or a type defined using
9cca2e9e 426Moose's type definition features. (Refer to L<Moose::Util::TypeConstraints>
c2a69ef1 427for information on how to define a new type, and how to retrieve type meta-data).
6ba6d68c 428
daea75c9 429=item I<coerce =E<gt> (1|0)>
430
26fbace8 431This will attempt to use coercion with the supplied type constraint to change
432the value passed into any accessors or constructors. You B<must> have supplied
5cfe3805 433a type constraint in order for this to work. See L<Moose::Cookbook::Basics::Recipe5>
1cd45431 434for an example.
daea75c9 435
436=item I<does =E<gt> $role_name>
437
26fbace8 438This will accept the name of a role which the value stored in this attribute
daea75c9 439is expected to have consumed.
440
441=item I<required =E<gt> (1|0)>
442
26fbace8 443This marks the attribute as being required. This means a I<defined> value must be
444supplied during class construction, and the attribute may never be set to
445C<undef> with an accessor.
daea75c9 446
447=item I<weak_ref =E<gt> (1|0)>
448
68efb014 449This will tell the class to store the value of this attribute as a weakened
450reference. If an attribute is a weakened reference, it B<cannot> also be
451coerced.
daea75c9 452
453=item I<lazy =E<gt> (1|0)>
454
26fbace8 455This will tell the class to not create this slot until absolutely necessary.
daea75c9 456If an attribute is marked as lazy it B<must> have a default supplied.
457
9e93dd19 458=item I<auto_deref =E<gt> (1|0)>
459
26fbace8 460This tells the accessor whether to automatically dereference the value returned.
1cd45431 461This is only legal if your C<isa> option is either C<ArrayRef> or C<HashRef>.
9e93dd19 462
65e14c86 463=item I<trigger =E<gt> $code>
464
465The I<trigger> option is a CODE reference which will be called after the value of
466the attribute is set. The CODE ref will be passed the instance itself, the
467updated value and the attribute meta-object (this is for more advanced fiddling
468and can typically be ignored). You B<cannot> have a trigger on a read-only
469attribute.
daea75c9 470
c84f324f 471=item I<handles =E<gt> ARRAY | HASH | REGEXP | ROLE | CODE>
2c0cbef7 472
26fbace8 473The I<handles> option provides Moose classes with automated delegation features.
474This is a pretty complex and powerful option. It accepts many different option
475formats, each with its own benefits and drawbacks.
38e3283b 476
1cd45431 477B<NOTE:> The class being delegated to does not need to be a Moose based class,
478which is why this feature is especially useful when wrapping non-Moose classes.
38e3283b 479
1cd45431 480All I<handles> option formats share the following traits:
38e3283b 481
1cd45431 482You cannot override a locally defined method with a delegated method; an
483exception will be thrown if you try. That is to say, if you define C<foo> in
484your class, you cannot override it with a delegated C<foo>. This is almost never
485something you would want to do, and if it is, you should do it by hand and not
486use Moose.
38e3283b 487
1cd45431 488You cannot override any of the methods found in Moose::Object, or the C<BUILD>
489and C<DEMOLISH> methods. These will not throw an exception, but will silently
490move on to the next method in the list. My reasoning for this is that you would
491almost never want to do this, since it usually breaks your class. As with
492overriding locally defined methods, if you do want to do this, you should do it
493manually, not with Moose.
38e3283b 494
f3c4e20e 495You do not I<need> to have a reader (or accessor) for the attribute in order
496to delegate to it. Moose will create a means of accessing the value for you,
497however this will be several times B<less> efficient then if you had given
498the attribute a reader (or accessor) to use.
499
38e3283b 500Below is the documentation for each option format:
501
502=over 4
503
504=item C<ARRAY>
505
26fbace8 506This is the most common usage for I<handles>. You basically pass a list of
507method names to be delegated, and Moose will install a delegation method
1cd45431 508for each one.
38e3283b 509
510=item C<HASH>
511
26fbace8 512This is the second most common usage for I<handles>. Instead of a list of
513method names, you pass a HASH ref where each key is the method name you
514want installed locally, and its value is the name of the original method
515in the class being delegated to.
fd595040 516
26fbace8 517This can be very useful for recursive classes like trees. Here is a
5cfe3805 518quick example (soon to be expanded into a Moose::Cookbook recipe):
38e3283b 519
1cd45431 520 package Tree;
38e3283b 521 use Moose;
26fbace8 522
38e3283b 523 has 'node' => (is => 'rw', isa => 'Any');
26fbace8 524
38e3283b 525 has 'children' => (
526 is => 'ro',
527 isa => 'ArrayRef',
528 default => sub { [] }
529 );
26fbace8 530
38e3283b 531 has 'parent' => (
532 is => 'rw',
533 isa => 'Tree',
a4e516f6 534 weak_ref => 1,
38e3283b 535 handles => {
536 parent_node => 'node',
26fbace8 537 siblings => 'children',
38e3283b 538 }
539 );
540
1cd45431 541In this example, the Tree package gets C<parent_node> and C<siblings> methods,
542which delegate to the C<node> and C<children> methods (respectively) of the Tree
26fbace8 543instance stored in the C<parent> slot.
38e3283b 544
545=item C<REGEXP>
546
26fbace8 547The regexp option works very similar to the ARRAY option, except that it builds
548the list of methods for you. It starts by collecting all possible methods of the
549class being delegated to, then filters that list using the regexp supplied here.
38e3283b 550
26fbace8 551B<NOTE:> An I<isa> option is required when using the regexp option format. This
552is so that we can determine (at compile time) the method list from the class.
38e3283b 553Without an I<isa> this is just not possible.
554
c84f324f 555=item C<ROLE>
556
26fbace8 557With the role option, you specify the name of a role whose "interface" then
558becomes the list of methods to handle. The "interface" can be defined as; the
559methods of the role and any required methods of the role. It should be noted
560that this does B<not> include any method modifiers or generated attribute
c84f324f 561methods (which is consistent with role composition).
562
38e3283b 563=item C<CODE>
564
1cd45431 565This is the option to use when you really want to do something funky. You should
566only use it if you really know what you are doing, as it involves manual
567metaclass twiddling.
38e3283b 568
1cd45431 569This takes a code reference, which should expect two arguments. The first is the
570attribute meta-object this I<handles> is attached to. The second is the
571metaclass of the class being delegated to. It expects you to return a hash (not
26fbace8 572a HASH ref) of the methods you want mapped.
38e3283b 573
574=back
2c0cbef7 575
004222dc 576=item I<metaclass =E<gt> $metaclass_name>
577
578This tells the class to use a custom attribute metaclass for this particular
579attribute. Custom attribute metaclasses are useful for extending the
580capabilities of the I<has> keyword: they are the simplest way to extend the MOP,
581but they are still a fairly advanced topic and too much to cover here, see
5cfe3805 582L<Moose::Cookbook::Meta::Recipe1> for more information.
004222dc 583
584The default behavior here is to just load C<$metaclass_name>; however, we also
585have a way to alias to a shorter name. This will first look to see if
586B<Moose::Meta::Attribute::Custom::$metaclass_name> exists. If it does, Moose
587will then check to see if that has the method C<register_implementation>, which
588should return the actual name of the custom attribute metaclass. If there is no
589C<register_implementation> method, it will fall back to using
590B<Moose::Meta::Attribute::Custom::$metaclass_name> as the metaclass name.
591
592=item I<traits =E<gt> [ @role_names ]>
593
594This tells Moose to take the list of C<@role_names> and apply them to the
595attribute meta-object. This is very similar to the I<metaclass> option, but
54f2996d 596allows you to use more than one extension at a time.
004222dc 597
54f2996d 598See L<TRAIT NAME RESOLUTION> for details on how a trait name is
599resolved to a class name.
600
601Also see L<Moose::Cookbook::Meta::Recipe3> for a metaclass trait
602example.
004222dc 603
6ba6d68c 604=back
605
cd7eeaf5 606=item B<has +$name =E<gt> %options>
607
26fbace8 608This is variation on the normal attibute creator C<has> which allows you to
8d62bf6d 609clone and extend an attribute from a superclass or from a role. Here is an
610example of the superclass usage:
cd7eeaf5 611
612 package Foo;
613 use Moose;
26fbace8 614
cd7eeaf5 615 has 'message' => (
26fbace8 616 is => 'rw',
cd7eeaf5 617 isa => 'Str',
618 default => 'Hello, I am a Foo'
619 );
26fbace8 620
cd7eeaf5 621 package My::Foo;
622 use Moose;
26fbace8 623
cd7eeaf5 624 extends 'Foo';
26fbace8 625
cd7eeaf5 626 has '+message' => (default => 'Hello I am My::Foo');
627
1cd45431 628What is happening here is that B<My::Foo> is cloning the C<message> attribute
629from its parent class B<Foo>, retaining the C<is =E<gt> 'rw'> and C<isa =E<gt>
630'Str'> characteristics, but changing the value in C<default>.
cd7eeaf5 631
8d62bf6d 632Here is another example, but within the context of a role:
633
634 package Foo::Role;
635 use Moose::Role;
986d175a 636
8d62bf6d 637 has 'message' => (
638 is => 'rw',
639 isa => 'Str',
640 default => 'Hello, I am a Foo'
641 );
986d175a 642
8d62bf6d 643 package My::Foo;
644 use Moose;
986d175a 645
8d62bf6d 646 with 'Foo::Role';
986d175a 647
8d62bf6d 648 has '+message' => (default => 'Hello I am My::Foo');
649
650In this case, we are basically taking the attribute which the role supplied
4032c9bb 651and altering it within the bounds of this feature.
8d62bf6d 652
4032c9bb 653Aside from where the attributes come from (one from superclass, the other
654from a role), this feature works exactly the same. This feature is restricted
655somewhat, so as to try and force at least I<some> sanity into it. You are only
656allowed to change the following attributes:
cd7eeaf5 657
658=over 4
659
26fbace8 660=item I<default>
cd7eeaf5 661
662Change the default value of an attribute.
663
26fbace8 664=item I<coerce>
cd7eeaf5 665
666Change whether the attribute attempts to coerce a value passed to it.
667
26fbace8 668=item I<required>
cd7eeaf5 669
670Change if the attribute is required to have a value.
671
672=item I<documentation>
673
674Change the documentation string associated with the attribute.
675
83cc9094 676=item I<lazy>
677
678Change if the attribute lazily initializes the slot.
679
cd7eeaf5 680=item I<isa>
681
aed87761 682You I<are> allowed to change the type without restriction.
683
684It is recommended that you use this freedom with caution. We used to
685only allow for extension only if the type was a subtype of the parent's
686type, but we felt that was too restrictive and is better left as a
687policy descision.
cd7eeaf5 688
83cc9094 689=item I<handles>
690
26fbace8 691You are allowed to B<add> a new C<handles> definition, but you are B<not>
692allowed to I<change> one.
83cc9094 693
8d62bf6d 694=item I<builder>
695
696You are allowed to B<add> a new C<builder> definition, but you are B<not>
697allowed to I<change> one.
698
13284479 699=item I<metaclass>
700
701You are allowed to B<add> a new C<metaclass> definition, but you are
702B<not> allowed to I<change> one.
703
704=item I<traits>
705
706You are allowed to B<add> additional traits to the C<traits> definition.
707These traits will be composed into the attribute, but pre-existing traits
708B<are not> overridden, or removed.
709
cd7eeaf5 710=back
711
076c81ed 712=item B<before $name|@names =E<gt> sub { ... }>
6ba6d68c 713
076c81ed 714=item B<after $name|@names =E<gt> sub { ... }>
6ba6d68c 715
076c81ed 716=item B<around $name|@names =E<gt> sub { ... }>
6ba6d68c 717
d8af92ae 718This three items are syntactic sugar for the before, after, and around method
719modifier features that L<Class::MOP> provides. More information on these may be
720found in the L<Class::MOP::Class documentation|Class::MOP::Class/"Method
721Modifiers"> for now.
6ba6d68c 722
159da176 723=item B<super>
724
26fbace8 725The keyword C<super> is a no-op when called outside of an C<override> method. In
726the context of an C<override> method, it will call the next most appropriate
159da176 727superclass method with the same arguments as the original method.
728
729=item B<override ($name, &sub)>
730
26fbace8 731An C<override> method is a way of explicitly saying "I am overriding this
732method from my superclass". You can call C<super> within this method, and
733it will work as expected. The same thing I<can> be accomplished with a normal
734method call and the C<SUPER::> pseudo-package; it is really your choice.
159da176 735
736=item B<inner>
737
26fbace8 738The keyword C<inner>, much like C<super>, is a no-op outside of the context of
739an C<augment> method. You can think of C<inner> as being the inverse of
68efb014 740C<super>; the details of how C<inner> and C<augment> work is best described in
5cfe3805 741the L<Moose::Cookbook::Basics::Recipe6>.
159da176 742
743=item B<augment ($name, &sub)>
744
26fbace8 745An C<augment> method, is a way of explicitly saying "I am augmenting this
746method from my superclass". Once again, the details of how C<inner> and
5cfe3805 747C<augment> work is best described in the L<Moose::Cookbook::Basics::Recipe6>.
159da176 748
6ba6d68c 749=item B<confess>
750
68efb014 751This is the C<Carp::confess> function, and exported here because I use it
004222dc 752all the time.
6ba6d68c 753
754=item B<blessed>
755
1cd45431 756This is the C<Scalar::Util::blessed> function, it is exported here because I
26fbace8 757use it all the time. It is highly recommended that this is used instead of
6ba6d68c 758C<ref> anywhere you need to test for an object's class name.
759
760=back
761
54f2996d 762=head1 METACLASS TRAITS
763
764When you use Moose, you can also specify traits which will be applied
765to your metaclass:
766
767 use Moose -traits => 'My::Trait';
768
769This is very similar to the attribute traits feature. When you do
770this, your class's C<meta> object will have the specified traits
771applied to it. See L<TRAIT NAME RESOLUTION> for more details.
772
773=head1 TRAIT NAME RESOLUTION
774
775By default, when given a trait name, Moose simply tries to load a
776class of the same name. If such a class does not exist, it then looks
777for for a class matching
778B<Moose::Meta::$type::Custom::Trait::$trait_name>. The C<$type>
779variable here will be one of B<Attribute> or B<Class>, depending on
780what the trait is being applied to.
781
782If a class with this long name exists, Moose checks to see if it has
783the method C<register_implementation>. This method is expected to
784return the I<real> class name of the trait. If there is no
785C<register_implementation> method, it will fall back to using
786B<Moose::Meta::$type::Custom::Trait::$trait> as the trait name.
787
788If all this is confusing, take a look at
789L<Moose::Cookbook::Meta::Recipe3>, which demonstrates how to create an
790attribute trait.
791
1cd45431 792=head1 UNIMPORTING FUNCTIONS
31f8ec72 793
794=head2 B<unimport>
795
1cd45431 796Moose offers a way to remove the keywords it exports, through the C<unimport>
31f8ec72 797method. You simply have to say C<no Moose> at the bottom of your code for this
798to work. Here is an example:
799
800 package Person;
801 use Moose;
802
803 has 'first_name' => (is => 'rw', isa => 'Str');
804 has 'last_name' => (is => 'rw', isa => 'Str');
26fbace8 805
806 sub full_name {
31f8ec72 807 my $self = shift;
26fbace8 808 $self->first_name . ' ' . $self->last_name
31f8ec72 809 }
26fbace8 810
811 no Moose; # keywords are removed from the Person package
31f8ec72 812
9bcfbab1 813=head1 EXTENDING AND EMBEDDING MOOSE
814
554b7648 815Moose also offers some options for extending or embedding it into your
6f894f30 816own framework. To learn more about extending Moose, we recommend
817checking out the "Extending" recipes in the L<Moose::Cookbook>,
818starting with L<Moose::Cookbook::Extending::Recipe1>, which provides
819an overview of all the different ways you might extend Moose.
554b7648 820
821=head2 B<< Moose->init_meta(for_class => $class, base_class => $baseclass, metaclass => $metaclass) >>
9bcfbab1 822
554b7648 823The C<init_meta> method sets up the metaclass object for the class
b143539e 824specified by C<for_class>. This method injects a a C<meta> accessor
825into the class so you can get at this object. It also sets the class's
554b7648 826superclass to C<base_class>, with L<Moose::Object> as the default.
9bcfbab1 827
554b7648 828You can specify an alternate metaclass with the C<metaclass> parameter.
26fbace8 829
80837fe1 830For more detail on this topic, see L<Moose::Cookbook::Extending::Recipe2>.
831
554b7648 832This method used to be documented as a function which accepted
833positional parameters. This calling style will still work for
4a66a4b3 834backwards compatibility, but is deprecated.
554b7648 835
836=head2 B<import>
837
838Moose's C<import> method supports the L<Sub::Exporter> form of C<{into =E<gt> $pkg}>
839and C<{into_level =E<gt> 1}>.
840
841B<NOTE>: Doing this is more or less deprecated. Use L<Moose::Exporter>
842instead, which lets you stack multiple C<Moose.pm>-alike modules
843sanely. It handles getting the exported functions into the right place
844for you.
845
05d9eaf6 846=head1 CAVEATS
847
848=over 4
849
850=item *
851
1cd45431 852It should be noted that C<super> and C<inner> B<cannot> be used in the same
853method. However, they may be combined within the same class hierarchy; see
854F<t/014_override_augment_inner_super.t> for an example.
05d9eaf6 855
26fbace8 856The reason for this is that C<super> is only valid within a method
857with the C<override> modifier, and C<inner> will never be valid within an
858C<override> method. In fact, C<augment> will skip over any C<override> methods
68efb014 859when searching for its appropriate C<inner>.
05d9eaf6 860
1cd45431 861This might seem like a restriction, but I am of the opinion that keeping these
862two features separate (yet interoperable) actually makes them easy to use, since
863their behavior is then easier to predict. Time will tell whether I am right or
c84f324f 864not (UPDATE: so far so good).
05d9eaf6 865
9b9da6f1 866=back
867
5569c072 868=head1 ACKNOWLEDGEMENTS
869
870=over 4
871
54c189df 872=item I blame Sam Vilain for introducing me to the insanity that is meta-models.
5569c072 873
54c189df 874=item I blame Audrey Tang for then encouraging my meta-model habit in #perl6.
5569c072 875
26fbace8 876=item Without Yuval "nothingmuch" Kogman this module would not be possible,
54c189df 877and it certainly wouldn't have this name ;P
5569c072 878
26fbace8 879=item The basis of the TypeContraints module was Rob Kinyon's idea
5569c072 880originally, I just ran with it.
881
638585e1 882=item Thanks to mst & chansen and the whole #moose posse for all the
c84f324f 883early ideas/feature-requests/encouragement/bug-finding.
d46a48f3 884
68efb014 885=item Thanks to David "Theory" Wheeler for meta-discussions and spelling fixes.
886
5569c072 887=back
888
e90c03d0 889=head1 SEE ALSO
890
891=over 4
892
c84f324f 893=item L<http://www.iinteractive.com/moose>
894
895This is the official web home of Moose, it contains links to our public SVN repo
26fbace8 896as well as links to a number of talks and articles on Moose and Moose related
897technologies.
c84f324f 898
196064ab 899=item L<Moose::Cookbook> - How to cook a Moose
900
901=item The Moose is flying, a tutorial by Randal Schwartz
902
903Part 1 - L<http://www.stonehenge.com/merlyn/LinuxMag/col94.html>
904
905Part 2 - L<http://www.stonehenge.com/merlyn/LinuxMag/col95.html>
906
6ba6d68c 907=item L<Class::MOP> documentation
908
909=item The #moose channel on irc.perl.org
910
e67a0fca 911=item The Moose mailing list - moose@perl.org
912
9e0361e1 913=item Moose stats on ohloh.net - L<http://www.ohloh.net/projects/moose>
c84f324f 914
12aed9a0 915=item Several Moose extension modules in the C<MooseX::> namespace.
916
917See L<http://search.cpan.org/search?query=MooseX::> for extensions.
28669f89 918
c84f324f 919=back
920
004222dc 921=head2 Books
922
923=over 4
924
925=item The Art of the MetaObject Protocol
926
927I mention this in the L<Class::MOP> docs too, this book was critical in
928the development of both modules and is highly recommended.
929
930=back
931
26fbace8 932=head2 Papers
c84f324f 933
934=over 4
e90c03d0 935
159da176 936=item L<http://www.cs.utah.edu/plt/publications/oopsla04-gff.pdf>
937
26fbace8 938This paper (suggested by lbr on #moose) was what lead to the implementation
939of the C<super>/C<override> and C<inner>/C<augment> features. If you really
1cd45431 940want to understand them, I suggest you read this.
159da176 941
e90c03d0 942=back
943
fcd84ca9 944=head1 BUGS
945
26fbace8 946All complex software has bugs lurking in it, and this module is no
fcd84ca9 947exception. If you find a bug please either email me, or add the bug
948to cpan-RT.
949
47b19570 950=head1 FEATURE REQUESTS
951
952We are very strict about what features we add to the Moose core, especially
953the user-visible features. Instead we have made sure that the underlying
954meta-system of Moose is as extensible as possible so that you can add your
955own features easily. That said, occasionally there is a feature needed in the
956meta-system to support your planned extension, in which case you should
957either email the mailing list or join us on irc at #moose to discuss.
958
fcd84ca9 959=head1 AUTHOR
960
961Stevan Little E<lt>stevan@iinteractive.comE<gt>
962
9af1d28b 963B<with contributions from:>
db1ab48d 964
9af1d28b 965Aankhen
966
967Adam (Alias) Kennedy
968
969Anders (Debolaz) Nor Berle
970
5868294f 971Nathan (kolibre) Gray
972
9af1d28b 973Christian (chansen) Hansen
974
e7f8d0c2 975Hans Dieter (confound) Pearcey
976
9af1d28b 977Eric (ewilhelm) Wilhelm
978
979Guillermo (groditi) Roditi
980
981Jess (castaway) Robinson
982
983Matt (mst) Trout
984
985Robert (phaylon) Sedlacek
986
987Robert (rlb3) Boone
988
989Scott (konobi) McWhirter
990
f44ae52f 991Shlomi (rindolf) Fish
992
9af1d28b 993Yuval (nothingmuch) Kogman
994
cbe25729 995Chris (perigrin) Prather
996
68b6146c 997Wallace (wreis) Reis
998
e46f5cc2 999Jonathan (jrockway) Rockway
1000
abfdc2b4 1001Dave (autarch) Rolsky
1002
3ccdc84a 1003Piotr (dexter) Roszatycki
1004
26fbace8 1005Sam (mugwump) Vilain
f1917f58 1006
ac211120 1007Shawn (sartak) Moore
1008
9af1d28b 1009... and many other #moose folks
98aae381 1010
fcd84ca9 1011=head1 COPYRIGHT AND LICENSE
1012
778db3ac 1013Copyright 2006-2008 by Infinity Interactive, Inc.
fcd84ca9 1014
1015L<http://www.iinteractive.com>
1016
1017This library is free software; you can redistribute it and/or modify
26fbace8 1018it under the same terms as Perl itself.
fcd84ca9 1019
ddd0ec20 1020=cut