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