Moved attribute management to CMOP::HasAttributes.
[gitmo/Class-MOP.git] / lib / Class / MOP / Class.pm
1
2 package Class::MOP::Class;
3
4 use strict;
5 use warnings;
6
7 use Class::MOP::Instance;
8 use Class::MOP::Method::Wrapped;
9 use Class::MOP::Method::Accessor;
10 use Class::MOP::Method::Constructor;
11
12 use Carp         'confess';
13 use Scalar::Util 'blessed', 'reftype', 'weaken';
14 use Sub::Name    'subname';
15 use Devel::GlobalDestruction 'in_global_destruction';
16
17 our $VERSION   = '0.95';
18 $VERSION = eval $VERSION;
19 our $AUTHORITY = 'cpan:STEVAN';
20
21 use base 'Class::MOP::Module', 'Class::MOP::HasAttributes';
22
23 # Creation
24
25 sub initialize {
26     my $class = shift;
27
28     my $package_name;
29     
30     if ( @_ % 2 ) {
31         $package_name = shift;
32     } else {
33         my %options = @_;
34         $package_name = $options{package};
35     }
36
37     ($package_name && !ref($package_name))
38         || confess "You must pass a package name and it cannot be blessed";
39
40     return Class::MOP::get_metaclass_by_name($package_name)
41         || $class->_construct_class_instance(package => $package_name, @_);
42 }
43
44 # NOTE: (meta-circularity)
45 # this is a special form of _construct_instance
46 # (see below), which is used to construct class
47 # meta-object instances for any Class::MOP::*
48 # class. All other classes will use the more
49 # normal &construct_instance.
50 sub _construct_class_instance {
51     my $class        = shift;
52     my $options      = @_ == 1 ? $_[0] : {@_};
53     my $package_name = $options->{package};
54     (defined $package_name && $package_name)
55         || confess "You must pass a package name";
56     # NOTE:
57     # return the metaclass if we have it cached,
58     # and it is still defined (it has not been
59     # reaped by DESTROY yet, which can happen
60     # annoyingly enough during global destruction)
61
62     if (defined(my $meta = Class::MOP::get_metaclass_by_name($package_name))) {
63         return $meta;
64     }
65
66     # NOTE:
67     # we need to deal with the possibility
68     # of class immutability here, and then
69     # get the name of the class appropriately
70     $class = (ref($class)
71                     ? ($class->is_immutable
72                         ? $class->_get_mutable_metaclass_name()
73                         : ref($class))
74                     : $class);
75
76     # now create the metaclass
77     my $meta;
78     if ($class eq 'Class::MOP::Class') {
79         $meta = $class->_new($options);
80     }
81     else {
82         # NOTE:
83         # it is safe to use meta here because
84         # class will always be a subclass of
85         # Class::MOP::Class, which defines meta
86         $meta = $class->meta->_construct_instance($options)
87     }
88
89     # and check the metaclass compatibility
90     $meta->_check_metaclass_compatibility();  
91
92     Class::MOP::store_metaclass_by_name($package_name, $meta);
93
94     # NOTE:
95     # we need to weaken any anon classes
96     # so that they can call DESTROY properly
97     Class::MOP::weaken_metaclass($package_name) if $meta->is_anon_class;
98
99     $meta;
100 }
101
102 sub _new {
103     my $class = shift;
104
105     return Class::MOP::Class->initialize($class)->new_object(@_)
106         if $class ne __PACKAGE__;
107
108     my $options = @_ == 1 ? $_[0] : {@_};
109
110     return bless {
111         # inherited from Class::MOP::Package
112         'package' => $options->{package},
113
114         # NOTE:
115         # since the following attributes will
116         # actually be loaded from the symbol
117         # table, and actually bypass the instance
118         # entirely, we can just leave these things
119         # listed here for reference, because they
120         # should not actually have a value associated
121         # with the slot.
122         'namespace' => \undef,
123         'methods'   => {},
124
125         # inherited from Class::MOP::Module
126         'version'   => \undef,
127         'authority' => \undef,
128
129         # defined in Class::MOP::Class
130         'superclasses' => \undef,
131
132         'attributes' => {},
133         'attribute_metaclass' =>
134             ( $options->{'attribute_metaclass'} || 'Class::MOP::Attribute' ),
135         'method_metaclass' =>
136             ( $options->{'method_metaclass'} || 'Class::MOP::Method' ),
137         'wrapped_method_metaclass' => (
138             $options->{'wrapped_method_metaclass'}
139                 || 'Class::MOP::Method::Wrapped'
140         ),
141         'instance_metaclass' =>
142             ( $options->{'instance_metaclass'} || 'Class::MOP::Instance' ),
143         'immutable_trait' => (
144             $options->{'immutable_trait'}
145                 || 'Class::MOP::Class::Immutable::Trait'
146         ),
147         'constructor_name' => ( $options->{constructor_name} || 'new' ),
148         'constructor_class' => (
149             $options->{constructor_class} || 'Class::MOP::Method::Constructor'
150         ),
151         'destructor_class' => $options->{destructor_class},
152     }, $class;
153 }
154
155 sub reset_package_cache_flag  { (shift)->{'_package_cache_flag'} = undef } 
156 sub update_package_cache_flag {
157     my $self = shift;
158     # NOTE:
159     # we can manually update the cache number 
160     # since we are actually adding the method
161     # to our cache as well. This avoids us 
162     # having to regenerate the method_map.
163     # - SL    
164     $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);    
165 }
166
167 sub _check_metaclass_compatibility {
168     my $self = shift;
169
170     # this is always okay ...
171     return if ref($self)                eq 'Class::MOP::Class'   &&
172               $self->instance_metaclass eq 'Class::MOP::Instance';
173
174     my @class_list = $self->linearized_isa;
175     shift @class_list; # shift off $self->name
176
177     foreach my $superclass_name (@class_list) {
178         my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name) || next;
179
180         # NOTE:
181         # we need to deal with the possibility
182         # of class immutability here, and then
183         # get the name of the class appropriately
184         my $super_meta_type
185             = $super_meta->is_immutable
186             ? $super_meta->_get_mutable_metaclass_name()
187             : ref($super_meta);
188
189         ($self->isa($super_meta_type))
190             || confess "The metaclass of " . $self->name . " ("
191                        . (ref($self)) . ")" .  " is not compatible with the " .
192                        "metaclass of its superclass, ".$superclass_name . " ("
193                        . ($super_meta_type) . ")";
194         # NOTE:
195         # we also need to check that instance metaclasses
196         # are compatibile in the same the class.
197         ($self->instance_metaclass->isa($super_meta->instance_metaclass))
198             || confess "The instance metaclass for " . $self->name . " (" . ($self->instance_metaclass) . ")" .
199                        " is not compatible with the " .
200                        "instance metaclass of its superclass, " . $superclass_name . " (" . ($super_meta->instance_metaclass) . ")";
201     }
202 }
203
204 ## ANON classes
205
206 {
207     # NOTE:
208     # this should be sufficient, if you have a
209     # use case where it is not, write a test and
210     # I will change it.
211     my $ANON_CLASS_SERIAL = 0;
212
213     # NOTE:
214     # we need a sufficiently annoying prefix
215     # this should suffice for now, this is
216     # used in a couple of places below, so
217     # need to put it up here for now.
218     my $ANON_CLASS_PREFIX = 'Class::MOP::Class::__ANON__::SERIAL::';
219
220     sub is_anon_class {
221         my $self = shift;
222         no warnings 'uninitialized';
223         $self->name =~ /^$ANON_CLASS_PREFIX/o;
224     }
225
226     sub create_anon_class {
227         my ($class, %options) = @_;
228         my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
229         return $class->create($package_name, %options);
230     }
231
232     # NOTE:
233     # this will only get called for
234     # anon-classes, all other calls
235     # are assumed to occur during
236     # global destruction and so don't
237     # really need to be handled explicitly
238     sub DESTROY {
239         my $self = shift;
240
241         return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated
242
243         no warnings 'uninitialized';
244         my $name = $self->name;
245         return unless $name =~ /^$ANON_CLASS_PREFIX/o;
246         # Moose does a weird thing where it replaces the metaclass for
247         # class when fixing metaclass incompatibility. In that case,
248         # we don't want to clean out the namespace now. We can detect
249         # that because Moose will explicitly update the singleton
250         # cache in Class::MOP.
251         my $current_meta = Class::MOP::get_metaclass_by_name($name);
252         return if $current_meta ne $self;
253
254         my ($serial_id) = ($name =~ /^$ANON_CLASS_PREFIX(\d+)/o);
255         no strict 'refs';
256         @{$name . '::ISA'} = ();
257         %{$name . '::'}    = ();
258         delete ${$ANON_CLASS_PREFIX}{$serial_id . '::'};
259
260         Class::MOP::remove_metaclass_by_name($name);
261     }
262
263 }
264
265 # creating classes with MOP ...
266
267 sub create {
268     my ( $class, @args ) = @_;
269
270     unshift @args, 'package' if @args % 2 == 1;
271
272     my (%options) = @args;
273     my $package_name = $options{package};
274
275     (ref $options{superclasses} eq 'ARRAY')
276         || confess "You must pass an ARRAY ref of superclasses"
277             if exists $options{superclasses};
278             
279     (ref $options{attributes} eq 'ARRAY')
280         || confess "You must pass an ARRAY ref of attributes"
281             if exists $options{attributes};      
282             
283     (ref $options{methods} eq 'HASH')
284         || confess "You must pass a HASH ref of methods"
285             if exists $options{methods};                  
286
287     my (%initialize_options) = @args;
288     delete @initialize_options{qw(
289         package
290         superclasses
291         attributes
292         methods
293         version
294         authority
295     )};
296     my $meta = $class->initialize( $package_name => %initialize_options );
297
298     $meta->_instantiate_module( $options{version}, $options{authority} );
299
300     # FIXME totally lame
301     $meta->add_method('meta' => sub {
302         $class->initialize(ref($_[0]) || $_[0]);
303     });
304
305     $meta->superclasses(@{$options{superclasses}})
306         if exists $options{superclasses};
307     # NOTE:
308     # process attributes first, so that they can
309     # install accessors, but locally defined methods
310     # can then overwrite them. It is maybe a little odd, but
311     # I think this should be the order of things.
312     if (exists $options{attributes}) {
313         foreach my $attr (@{$options{attributes}}) {
314             $meta->add_attribute($attr);
315         }
316     }
317     if (exists $options{methods}) {
318         foreach my $method_name (keys %{$options{methods}}) {
319             $meta->add_method($method_name, $options{methods}->{$method_name});
320         }
321     }
322     return $meta;
323 }
324
325 ## Attribute readers
326
327 # NOTE:
328 # all these attribute readers will be bootstrapped
329 # away in the Class::MOP bootstrap section
330
331 sub instance_metaclass       { $_[0]->{'instance_metaclass'}          }
332 sub immutable_trait          { $_[0]->{'immutable_trait'}             }
333 sub constructor_class        { $_[0]->{'constructor_class'}           }
334 sub constructor_name         { $_[0]->{'constructor_name'}            }
335 sub destructor_class         { $_[0]->{'destructor_class'}            }
336
337 # Instance Construction & Cloning
338
339 sub new_object {
340     my $class = shift;
341
342     # NOTE:
343     # we need to protect the integrity of the
344     # Class::MOP::Class singletons here, so we
345     # delegate this to &construct_class_instance
346     # which will deal with the singletons
347     return $class->_construct_class_instance(@_)
348         if $class->name->isa('Class::MOP::Class');
349     return $class->_construct_instance(@_);
350 }
351
352 sub _construct_instance {
353     my $class = shift;
354     my $params = @_ == 1 ? $_[0] : {@_};
355     my $meta_instance = $class->get_meta_instance();
356     # FIXME:
357     # the code below is almost certainly incorrect
358     # but this is foreign inheritance, so we might
359     # have to kludge it in the end.
360     my $instance = $params->{__INSTANCE__} || $meta_instance->create_instance();
361     foreach my $attr ($class->get_all_attributes()) {
362         $attr->initialize_instance_slot($meta_instance, $instance, $params);
363     }
364     # NOTE:
365     # this will only work for a HASH instance type
366     if ($class->is_anon_class) {
367         (reftype($instance) eq 'HASH')
368             || confess "Currently only HASH based instances are supported with instance of anon-classes";
369         # NOTE:
370         # At some point we should make this official
371         # as a reserved slot name, but right now I am
372         # going to keep it here.
373         # my $RESERVED_MOP_SLOT = '__MOP__';
374         $instance->{'__MOP__'} = $class;
375     }
376     return $instance;
377 }
378
379
380 sub get_meta_instance {
381     my $self = shift;
382     $self->{'_meta_instance'} ||= $self->_create_meta_instance();
383 }
384
385 sub _create_meta_instance {
386     my $self = shift;
387     
388     my $instance = $self->instance_metaclass->new(
389         associated_metaclass => $self,
390         attributes => [ $self->get_all_attributes() ],
391     );
392
393     $self->add_meta_instance_dependencies()
394         if $instance->is_dependent_on_superclasses();
395
396     return $instance;
397 }
398
399 sub clone_object {
400     my $class    = shift;
401     my $instance = shift;
402     (blessed($instance) && $instance->isa($class->name))
403         || confess "You must pass an instance of the metaclass (" . (ref $class ? $class->name : $class) . "), not ($instance)";
404
405     # NOTE:
406     # we need to protect the integrity of the
407     # Class::MOP::Class singletons here, they
408     # should not be cloned.
409     return $instance if $instance->isa('Class::MOP::Class');
410     $class->_clone_instance($instance, @_);
411 }
412
413 sub _clone_instance {
414     my ($class, $instance, %params) = @_;
415     (blessed($instance))
416         || confess "You can only clone instances, ($instance) is not a blessed instance";
417     my $meta_instance = $class->get_meta_instance();
418     my $clone = $meta_instance->clone_instance($instance);
419     foreach my $attr ($class->get_all_attributes()) {
420         if ( defined( my $init_arg = $attr->init_arg ) ) {
421             if (exists $params{$init_arg}) {
422                 $attr->set_value($clone, $params{$init_arg});
423             }
424         }
425     }
426     return $clone;
427 }
428
429 sub rebless_instance {
430     my ($self, $instance, %params) = @_;
431
432     my $old_metaclass = Class::MOP::class_of($instance);
433
434     my $old_class = $old_metaclass ? $old_metaclass->name : blessed($instance);
435     $self->name->isa($old_class)
436         || confess "You may rebless only into a subclass of ($old_class), of which (". $self->name .") isn't.";
437
438     $old_metaclass->rebless_instance_away($instance, $self, %params)
439         if $old_metaclass;
440
441     my $meta_instance = $self->get_meta_instance();
442
443     # rebless!
444     # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
445     $meta_instance->rebless_instance_structure($_[1], $self);
446
447     foreach my $attr ( $self->get_all_attributes ) {
448         if ( $attr->has_value($instance) ) {
449             if ( defined( my $init_arg = $attr->init_arg ) ) {
450                 $params{$init_arg} = $attr->get_value($instance)
451                     unless exists $params{$init_arg};
452             } 
453             else {
454                 $attr->set_value($instance, $attr->get_value($instance));
455             }
456         }
457     }
458
459     foreach my $attr ($self->get_all_attributes) {
460         $attr->initialize_instance_slot($meta_instance, $instance, \%params);
461     }
462     
463     $instance;
464 }
465
466 sub rebless_instance_away {
467     # this intentionally does nothing, it is just a hook
468 }
469
470 # Inheritance
471
472 sub superclasses {
473     my $self     = shift;
474     my $var_spec = { sigil => '@', type => 'ARRAY', name => 'ISA' };
475     if (@_) {
476         my @supers = @_;
477         @{$self->get_package_symbol($var_spec)} = @supers;
478
479         # NOTE:
480         # on 5.8 and below, we need to call
481         # a method to get Perl to detect
482         # a cycle in the class hierarchy
483         my $class = $self->name;
484         $class->isa($class);
485
486         # NOTE:
487         # we need to check the metaclass
488         # compatibility here so that we can
489         # be sure that the superclass is
490         # not potentially creating an issues
491         # we don't know about
492
493         $self->_check_metaclass_compatibility();
494         $self->_superclasses_updated();
495     }
496     @{$self->get_package_symbol($var_spec)};
497 }
498
499 sub _superclasses_updated {
500     my $self = shift;
501     $self->update_meta_instance_dependencies();
502 }
503
504 sub subclasses {
505     my $self = shift;
506     my $super_class = $self->name;
507
508     return @{ $super_class->mro::get_isarev() };
509 }
510
511 sub direct_subclasses {
512     my $self = shift;
513     my $super_class = $self->name;
514
515     return grep {
516         grep {
517             $_ eq $super_class
518         } Class::MOP::Class->initialize($_)->superclasses
519     } $self->subclasses;
520 }
521
522 sub linearized_isa {
523     return @{ mro::get_linear_isa( (shift)->name ) };
524 }
525
526 sub class_precedence_list {
527     my $self = shift;
528     my $name = $self->name;
529
530     unless (Class::MOP::IS_RUNNING_ON_5_10()) { 
531         # NOTE:
532         # We need to check for circular inheritance here
533         # if we are are not on 5.10, cause 5.8 detects it 
534         # late. This will do nothing if all is well, and 
535         # blow up otherwise. Yes, it's an ugly hack, better
536         # suggestions are welcome.        
537         # - SL
538         ($name || return)->isa('This is a test for circular inheritance') 
539     }
540
541     # if our mro is c3, we can 
542     # just grab the linear_isa
543     if (mro::get_mro($name) eq 'c3') {
544         return @{ mro::get_linear_isa($name) }
545     }
546     else {
547         # NOTE:
548         # we can't grab the linear_isa for dfs
549         # since it has all the duplicates 
550         # already removed.
551         return (
552             $name,
553             map {
554                 $self->initialize($_)->class_precedence_list()
555             } $self->superclasses()
556         );
557     }
558 }
559
560 ## Methods
561
562 {
563     my $fetch_and_prepare_method = sub {
564         my ($self, $method_name) = @_;
565         my $wrapped_metaclass = $self->wrapped_method_metaclass;
566         # fetch it locally
567         my $method = $self->get_method($method_name);
568         # if we dont have local ...
569         unless ($method) {
570             # try to find the next method
571             $method = $self->find_next_method_by_name($method_name);
572             # die if it does not exist
573             (defined $method)
574                 || confess "The method '$method_name' was not found in the inheritance hierarchy for " . $self->name;
575             # and now make sure to wrap it
576             # even if it is already wrapped
577             # because we need a new sub ref
578             $method = $wrapped_metaclass->wrap($method,
579                 package_name => $self->name,
580                 name         => $method_name,
581             );
582         }
583         else {
584             # now make sure we wrap it properly
585             $method = $wrapped_metaclass->wrap($method,
586                 package_name => $self->name,
587                 name         => $method_name,
588             ) unless $method->isa($wrapped_metaclass);
589         }
590         $self->add_method($method_name => $method);
591         return $method;
592     };
593
594     sub add_before_method_modifier {
595         my ($self, $method_name, $method_modifier) = @_;
596         (defined $method_name && length $method_name)
597             || confess "You must pass in a method name";
598         my $method = $fetch_and_prepare_method->($self, $method_name);
599         $method->add_before_modifier(
600             subname(':before' => $method_modifier)
601         );
602     }
603
604     sub add_after_method_modifier {
605         my ($self, $method_name, $method_modifier) = @_;
606         (defined $method_name && length $method_name)
607             || confess "You must pass in a method name";
608         my $method = $fetch_and_prepare_method->($self, $method_name);
609         $method->add_after_modifier(
610             subname(':after' => $method_modifier)
611         );
612     }
613
614     sub add_around_method_modifier {
615         my ($self, $method_name, $method_modifier) = @_;
616         (defined $method_name && length $method_name)
617             || confess "You must pass in a method name";
618         my $method = $fetch_and_prepare_method->($self, $method_name);
619         $method->add_around_modifier(
620             subname(':around' => $method_modifier)
621         );
622     }
623
624     # NOTE:
625     # the methods above used to be named like this:
626     #    ${pkg}::${method}:(before|after|around)
627     # but this proved problematic when using one modifier
628     # to wrap multiple methods (something which is likely
629     # to happen pretty regularly IMO). So instead of naming
630     # it like this, I have chosen to just name them purely
631     # with their modifier names, like so:
632     #    :(before|after|around)
633     # The fact is that in a stack trace, it will be fairly
634     # evident from the context what method they are attached
635     # to, and so don't need the fully qualified name.
636 }
637
638 sub find_method_by_name {
639     my ($self, $method_name) = @_;
640     (defined $method_name && length $method_name)
641         || confess "You must define a method name to find";
642     foreach my $class ($self->linearized_isa) {
643         my $method = $self->initialize($class)->get_method($method_name);
644         return $method if defined $method;
645     }
646     return;
647 }
648
649 sub get_all_methods {
650     my $self = shift;
651
652     my %methods;
653     for my $class ( reverse $self->linearized_isa ) {
654         my $meta = $self->initialize($class);
655
656         $methods{$_} = $meta->get_method($_)
657             for $meta->get_method_list;
658     }
659
660     return values %methods;
661 }
662
663 sub get_all_method_names {
664     my $self = shift;
665     my %uniq;
666     return grep { !$uniq{$_}++ } map { $self->initialize($_)->get_method_list } $self->linearized_isa;
667 }
668
669 sub find_all_methods_by_name {
670     my ($self, $method_name) = @_;
671     (defined $method_name && length $method_name)
672         || confess "You must define a method name to find";
673     my @methods;
674     foreach my $class ($self->linearized_isa) {
675         # fetch the meta-class ...
676         my $meta = $self->initialize($class);
677         push @methods => {
678             name  => $method_name,
679             class => $class,
680             code  => $meta->get_method($method_name)
681         } if $meta->has_method($method_name);
682     }
683     return @methods;
684 }
685
686 sub find_next_method_by_name {
687     my ($self, $method_name) = @_;
688     (defined $method_name && length $method_name)
689         || confess "You must define a method name to find";
690     my @cpl = $self->linearized_isa;
691     shift @cpl; # discard ourselves
692     foreach my $class (@cpl) {
693         my $method = $self->initialize($class)->get_method($method_name);
694         return $method if defined $method;
695     }
696     return;
697 }
698
699 sub update_meta_instance_dependencies {
700     my $self = shift;
701
702     if ( $self->{meta_instance_dependencies} ) {
703         return $self->add_meta_instance_dependencies;
704     }
705 }
706
707 sub add_meta_instance_dependencies {
708     my $self = shift;
709
710     $self->remove_meta_instance_dependencies;
711
712     my @attrs = $self->get_all_attributes();
713
714     my %seen;
715     my @classes = grep { not $seen{ $_->name }++ }
716         map { $_->associated_class } @attrs;
717
718     foreach my $class (@classes) {
719         $class->add_dependent_meta_instance($self);
720     }
721
722     $self->{meta_instance_dependencies} = \@classes;
723 }
724
725 sub remove_meta_instance_dependencies {
726     my $self = shift;
727
728     if ( my $classes = delete $self->{meta_instance_dependencies} ) {
729         foreach my $class (@$classes) {
730             $class->remove_dependent_meta_instance($self);
731         }
732
733         return $classes;
734     }
735
736     return;
737
738 }
739
740 sub add_dependent_meta_instance {
741     my ( $self, $metaclass ) = @_;
742     push @{ $self->{dependent_meta_instances} }, $metaclass;
743 }
744
745 sub remove_dependent_meta_instance {
746     my ( $self, $metaclass ) = @_;
747     my $name = $metaclass->name;
748     @$_ = grep { $_->name ne $name } @$_
749         for $self->{dependent_meta_instances};
750 }
751
752 sub invalidate_meta_instances {
753     my $self = shift;
754     $_->invalidate_meta_instance()
755         for $self, @{ $self->{dependent_meta_instances} };
756 }
757
758 sub invalidate_meta_instance {
759     my $self = shift;
760     undef $self->{_meta_instance};
761 }
762
763 # check if we can reinitialize
764 sub is_pristine {
765     my $self = shift;
766
767     # if any local attr is defined
768     return if $self->get_attribute_list;
769
770     # or any non-declared methods
771     for my $method ( map { $self->get_method($_) } $self->get_method_list ) {
772         return if $method->isa("Class::MOP::Method::Generated");
773         # FIXME do we need to enforce this too? return unless $method->isa( $self->method_metaclass );
774     }
775
776     return 1;
777 }
778
779 ## Class closing
780
781 sub is_mutable   { 1 }
782 sub is_immutable { 0 }
783
784 sub immutable_options { %{ $_[0]{__immutable}{options} || {} } }
785
786 sub _immutable_options {
787     my ( $self, @args ) = @_;
788
789     return (
790         inline_accessors   => 1,
791         inline_constructor => 1,
792         inline_destructor  => 0,
793         debug              => 0,
794         immutable_trait    => $self->immutable_trait,
795         constructor_name   => $self->constructor_name,
796         constructor_class  => $self->constructor_class,
797         destructor_class   => $self->destructor_class,
798         @args,
799     );
800 }
801
802 sub make_immutable {
803     my ( $self, @args ) = @_;
804
805     if ( $self->is_mutable ) {
806         $self->_initialize_immutable( $self->_immutable_options(@args) );
807         $self->_rebless_as_immutable(@args);
808         return $self;
809     }
810     else {
811         return;
812     }
813 }
814
815 sub make_mutable {
816     my $self = shift;
817
818     if ( $self->is_immutable ) {
819         my @args = $self->immutable_options;
820         $self->_rebless_as_mutable();
821         $self->_remove_inlined_code(@args);
822         delete $self->{__immutable};
823         return $self;
824     }
825     else {
826         return;
827     }
828 }
829
830 sub _rebless_as_immutable {
831     my ( $self, @args ) = @_;
832
833     $self->{__immutable}{original_class} = ref $self;
834
835     bless $self => $self->_immutable_metaclass(@args);
836 }
837
838 sub _immutable_metaclass {
839     my ( $self, %args ) = @_;
840
841     if ( my $class = $args{immutable_metaclass} ) {
842         return $class;
843     }
844
845     my $trait = $args{immutable_trait} = $self->immutable_trait
846         || confess "no immutable trait specified for $self";
847
848     my $meta      = $self->meta;
849     my $meta_attr = $meta->find_attribute_by_name("immutable_trait");
850
851     my $class_name;
852
853     if ( $meta_attr and $trait eq $meta_attr->default ) {
854         # if the trait is the same as the default we try and pick a
855         # predictable name for the immutable metaclass
856         $class_name = 'Class::MOP::Class::Immutable::' . ref($self);
857     }
858     else {
859         $class_name = join '::', 'Class::MOP::Class::Immutable::CustomTrait',
860             $trait, 'ForMetaClass', ref($self);
861     }
862
863     return $class_name
864         if Class::MOP::is_class_loaded($class_name);
865
866     # If the metaclass is a subclass of CMOP::Class which has had
867     # metaclass roles applied (via Moose), then we want to make sure
868     # that we preserve that anonymous class (see Fey::ORM for an
869     # example of where this matters).
870     my $meta_name
871         = $meta->is_immutable
872         ? $meta->_get_mutable_metaclass_name
873         : ref $meta;
874
875     my $immutable_meta = $meta_name->create(
876         $class_name,
877         superclasses => [ ref $self ],
878     );
879
880     Class::MOP::load_class($trait);
881     for my $meth ( Class::MOP::Class->initialize($trait)->get_all_methods ) {
882         my $meth_name = $meth->name;
883
884         if ( $immutable_meta->find_method_by_name( $meth_name ) ) {
885             $immutable_meta->add_around_method_modifier( $meth_name, $meth->body );
886         }
887         else {
888             $immutable_meta->add_method( $meth_name, $meth->clone );
889         }
890     }
891
892     $immutable_meta->make_immutable(
893         inline_constructor => 0,
894         inline_accessors   => 0,
895     );
896
897     return $class_name;
898 }
899
900 sub _remove_inlined_code {
901     my $self = shift;
902
903     $self->remove_method( $_->name ) for $self->_inlined_methods;
904
905     delete $self->{__immutable}{inlined_methods};
906 }
907
908 sub _inlined_methods { @{ $_[0]{__immutable}{inlined_methods} || [] } }
909
910 sub _add_inlined_method {
911     my ( $self, $method ) = @_;
912
913     push @{ $self->{__immutable}{inlined_methods} ||= [] }, $method;
914 }
915
916 sub _initialize_immutable {
917     my ( $self, %args ) = @_;
918
919     $self->{__immutable}{options} = \%args;
920     $self->_install_inlined_code(%args);
921 }
922
923 sub _install_inlined_code {
924     my ( $self, %args ) = @_;
925
926     # FIXME
927     $self->_inline_accessors(%args)   if $args{inline_accessors};
928     $self->_inline_constructor(%args) if $args{inline_constructor};
929     $self->_inline_destructor(%args)  if $args{inline_destructor};
930 }
931
932 sub _rebless_as_mutable {
933     my $self = shift;
934
935     bless $self, $self->_get_mutable_metaclass_name;
936
937     return $self;
938 }
939
940 sub _inline_accessors {
941     my $self = shift;
942
943     foreach my $attr_name ( $self->get_attribute_list ) {
944         $self->get_attribute($attr_name)->install_accessors(1);
945     }
946 }
947
948 sub _inline_constructor {
949     my ( $self, %args ) = @_;
950
951     my $name = $args{constructor_name};
952     # A class may not even have a constructor, and that's okay.
953     return unless defined $name;
954
955     if ( $self->has_method($name) && !$args{replace_constructor} ) {
956         my $class = $self->name;
957         warn "Not inlining a constructor for $class since it defines"
958             . " its own constructor.\n"
959             . "If you are certain you don't need to inline your"
960             . " constructor, specify inline_constructor => 0 in your"
961             . " call to $class->meta->make_immutable\n";
962         return;
963     }
964
965     my $constructor_class = $args{constructor_class};
966
967     Class::MOP::load_class($constructor_class);
968
969     my $constructor = $constructor_class->new(
970         options      => \%args,
971         metaclass    => $self,
972         is_inline    => 1,
973         package_name => $self->name,
974         name         => $name,
975     );
976
977     if ( $args{replace_constructor} or $constructor->can_be_inlined ) {
978         $self->add_method( $name => $constructor );
979         $self->_add_inlined_method($constructor);
980     }
981 }
982
983 sub _inline_destructor {
984     my ( $self, %args ) = @_;
985
986     ( exists $args{destructor_class} && defined $args{destructor_class} )
987         || confess "The 'inline_destructor' option is present, but "
988         . "no destructor class was specified";
989
990     if ( $self->has_method('DESTROY') && ! $args{replace_destructor} ) {
991         my $class = $self->name;
992         warn "Not inlining a destructor for $class since it defines"
993             . " its own destructor.\n";
994         return;
995     }
996
997     my $destructor_class = $args{destructor_class};
998
999     Class::MOP::load_class($destructor_class);
1000
1001     return unless $destructor_class->is_needed($self);
1002
1003     my $destructor = $destructor_class->new(
1004         options      => \%args,
1005         metaclass    => $self,
1006         package_name => $self->name,
1007         name         => 'DESTROY'
1008     );
1009
1010     if ( $args{replace_destructor} or $destructor->can_be_inlined ) {
1011         $self->add_method( 'DESTROY' => $destructor );
1012         $self->_add_inlined_method($destructor);
1013     }
1014 }
1015
1016 1;
1017
1018 __END__
1019
1020 =pod
1021
1022 =head1 NAME
1023
1024 Class::MOP::Class - Class Meta Object
1025
1026 =head1 SYNOPSIS
1027
1028   # assuming that class Foo
1029   # has been defined, you can
1030
1031   # use this for introspection ...
1032
1033   # add a method to Foo ...
1034   Foo->meta->add_method( 'bar' => sub {...} )
1035
1036   # get a list of all the classes searched
1037   # the method dispatcher in the correct order
1038   Foo->meta->class_precedence_list()
1039
1040   # remove a method from Foo
1041   Foo->meta->remove_method('bar');
1042
1043   # or use this to actually create classes ...
1044
1045   Class::MOP::Class->create(
1046       'Bar' => (
1047           version      => '0.01',
1048           superclasses => ['Foo'],
1049           attributes   => [
1050               Class::MOP::Attribute->new('$bar'),
1051               Class::MOP::Attribute->new('$baz'),
1052           ],
1053           methods => {
1054               calculate_bar => sub {...},
1055               construct_baz => sub {...}
1056           }
1057       )
1058   );
1059
1060 =head1 DESCRIPTION
1061
1062 The Class Protocol is the largest and most complex part of the
1063 Class::MOP meta-object protocol. It controls the introspection and
1064 manipulation of Perl 5 classes, and it can create them as well. The
1065 best way to understand what this module can do is to read the
1066 documentation for each of its methods.
1067
1068 =head1 INHERITANCE
1069
1070 C<Class::MOP::Class> is a subclass of L<Class::MOP::Module>.
1071
1072 =head1 METHODS
1073
1074 =head2 Class construction
1075
1076 These methods all create new C<Class::MOP::Class> objects. These
1077 objects can represent existing classes or they can be used to create
1078 new classes from scratch.
1079
1080 The metaclass object for a given class is a singleton. If you attempt
1081 to create a metaclass for the same class twice, you will just get the
1082 existing object.
1083
1084 =over 4
1085
1086 =item B<< Class::MOP::Class->create($package_name, %options) >>
1087
1088 This method creates a new C<Class::MOP::Class> object with the given
1089 package name. It accepts a number of options:
1090
1091 =over 8
1092
1093 =item * version
1094
1095 An optional version number for the newly created package.
1096
1097 =item * authority
1098
1099 An optional authority for the newly created package.
1100
1101 =item * superclasses
1102
1103 An optional array reference of superclass names.
1104
1105 =item * methods
1106
1107 An optional hash reference of methods for the class. The keys of the
1108 hash reference are method names and values are subroutine references.
1109
1110 =item * attributes
1111
1112 An optional array reference of L<Class::MOP::Attribute> objects.
1113
1114 =back
1115
1116 =item B<< Class::MOP::Class->create_anon_class(%options) >>
1117
1118 This method works just like C<< Class::MOP::Class->create >> but it
1119 creates an "anonymous" class. In fact, the class does have a name, but
1120 that name is a unique name generated internally by this module.
1121
1122 It accepts the same C<superclasses>, C<methods>, and C<attributes>
1123 parameters that C<create> accepts.
1124
1125 Anonymous classes are destroyed once the metaclass they are attached
1126 to goes out of scope, and will be removed from Perl's internal symbol
1127 table.
1128
1129 All instances of an anonymous class keep a special reference to the
1130 metaclass object, which prevents the metaclass from going out of scope
1131 while any instances exist.
1132
1133 This only works if the instance is based on a hash reference, however.
1134
1135 =item B<< Class::MOP::Class->initialize($package_name, %options) >>
1136
1137 This method will initialize a C<Class::MOP::Class> object for the
1138 named package. Unlike C<create>, this method I<will not> create a new
1139 class.
1140
1141 The purpose of this method is to retrieve a C<Class::MOP::Class>
1142 object for introspecting an existing class.
1143
1144 If an existing C<Class::MOP::Class> object exists for the named
1145 package, it will be returned, and any options provided will be
1146 ignored!
1147
1148 If the object does not yet exist, it will be created.
1149
1150 The valid options that can be passed to this method are
1151 C<attribute_metaclass>, C<method_metaclass>,
1152 C<wrapped_method_metaclass>, and C<instance_metaclass>. These are all
1153 optional, and default to the appropriate class in the C<Class::MOP>
1154 distribution.
1155
1156 =back
1157
1158 =head2 Object instance construction and cloning
1159
1160 These methods are all related to creating and/or cloning object
1161 instances.
1162
1163 =over 4
1164
1165 =item B<< $metaclass->clone_object($instance, %params) >>
1166
1167 This method clones an existing object instance. Any parameters you
1168 provide are will override existing attribute values in the object.
1169
1170 This is a convenience method for cloning an object instance, then
1171 blessing it into the appropriate package.
1172
1173 You could implement a clone method in your class, using this method:
1174
1175   sub clone {
1176       my ($self, %params) = @_;
1177       $self->meta->clone_object($self, %params);
1178   }
1179
1180 =item B<< $metaclass->rebless_instance($instance, %params) >>
1181
1182 This method changes the class of C<$instance> to the metaclass's class.
1183
1184 You can only rebless an instance into a subclass of its current
1185 class. If you pass any additional parameters, these will be treated
1186 like constructor parameters and used to initialize the object's
1187 attributes. Any existing attributes that are already set will be
1188 overwritten.
1189
1190 Before reblessing the instance, this method will call
1191 C<rebless_instance_away> on the instance's current metaclass. This method
1192 will be passed the instance, the new metaclass, and any parameters
1193 specified to C<rebless_instance>. By default, C<rebless_instance_away>
1194 does nothing; it is merely a hook.
1195
1196 =item B<< $metaclass->new_object(%params) >>
1197
1198 This method is used to create a new object of the metaclass's
1199 class. Any parameters you provide are used to initialize the
1200 instance's attributes. A special C<__INSTANCE__> key can be passed to
1201 provide an already generated instance, rather than having Class::MOP
1202 generate it for you. This is mostly useful for using Class::MOP with
1203 foreign classes which generate instances using their own constructors.
1204
1205 =item B<< $metaclass->instance_metaclass >>
1206
1207 Returns the class name of the instance metaclass. See
1208 L<Class::MOP::Instance> for more information on the instance
1209 metaclass.
1210
1211 =item B<< $metaclass->get_meta_instance >>
1212
1213 Returns an instance of the C<instance_metaclass> to be used in the
1214 construction of a new instance of the class.
1215
1216 =back
1217
1218 =head2 Informational predicates
1219
1220 These are a few predicate methods for asking information about the
1221 class itself.
1222
1223 =over 4
1224
1225 =item B<< $metaclass->is_anon_class >>
1226
1227 This returns true if the class was created by calling C<<
1228 Class::MOP::Class->create_anon_class >>.
1229
1230 =item B<< $metaclass->is_mutable >>
1231
1232 This returns true if the class is still mutable.
1233
1234 =item B<< $metaclass->is_immutable >>
1235
1236 This returns true if the class has been made immutable.
1237
1238 =item B<< $metaclass->is_pristine >>
1239
1240 A class is I<not> pristine if it has non-inherited attributes or if it
1241 has any generated methods.
1242
1243 =back
1244
1245 =head2 Inheritance Relationships
1246
1247 =over 4
1248
1249 =item B<< $metaclass->superclasses(@superclasses) >>
1250
1251 This is a read-write accessor which represents the superclass
1252 relationships of the metaclass's class.
1253
1254 This is basically sugar around getting and setting C<@ISA>.
1255
1256 =item B<< $metaclass->class_precedence_list >>
1257
1258 This returns a list of all of the class's ancestor classes. The
1259 classes are returned in method dispatch order.
1260
1261 =item B<< $metaclass->linearized_isa >>
1262
1263 This returns a list based on C<class_precedence_list> but with all
1264 duplicates removed.
1265
1266 =item B<< $metaclass->subclasses >>
1267
1268 This returns a list of all subclasses for this class, even indirect
1269 subclasses.
1270
1271 =item B<< $metaclass->direct_subclasses >>
1272
1273 This returns a list of immediate subclasses for this class, which does not
1274 include indirect subclasses.
1275
1276 =back
1277
1278 =head2 Method introspection
1279
1280 See L<Class::MOP::Package/Method introspection and creation> for
1281 methods that operate only on the current class.  Class::MOP::Class adds
1282 introspection capabilities that take inheritance into account.
1283
1284 =over 4
1285
1286 =item B<< $metaclass->get_all_methods >>
1287
1288 This will traverse the inheritance hierarchy and return a list of all
1289 the L<Class::MOP::Method> objects for this class and its parents.
1290
1291 =item B<< $metaclass->find_method_by_name($method_name) >>
1292
1293 This will return a L<Class::MOP::Method> for the specified
1294 C<$method_name>. If the class does not have the specified method, it
1295 returns C<undef>
1296
1297 Unlike C<get_method>, this method I<will> look for the named method in
1298 superclasses.
1299
1300 =item B<< $metaclass->get_all_method_names >>
1301
1302 This will return a list of method I<names> for all of this class's
1303 methods, including inherited methods.
1304
1305 =item B<< $metaclass->find_all_methods_by_name($method_name) >>
1306
1307 This method looks for the named method in the class and all of its
1308 parents. It returns every matching method it finds in the inheritance
1309 tree, so it returns a list of methods.
1310
1311 Each method is returned as a hash reference with three keys. The keys
1312 are C<name>, C<class>, and C<code>. The C<code> key has a
1313 L<Class::MOP::Method> object as its value.
1314
1315 The list of methods is distinct.
1316
1317 =item B<< $metaclass->find_next_method_by_name($method_name) >>
1318
1319 This method returns the first method in any superclass matching the
1320 given name. It is effectively the method that C<SUPER::$method_name>
1321 would dispatch to.
1322
1323 =back
1324
1325 =head2 Attribute introspection and creation
1326
1327 Because Perl 5 does not have a core concept of attributes in classes,
1328 we can only return information about attributes which have been added
1329 via this class's methods. We cannot discover information about
1330 attributes which are defined in terms of "regular" Perl 5 methods.
1331
1332 =over 4
1333
1334 =item B<< $metaclass->get_attribute($attribute_name) >>
1335
1336 This will return a L<Class::MOP::Attribute> for the specified
1337 C<$attribute_name>. If the class does not have the specified
1338 attribute, it returns C<undef>.
1339
1340 NOTE that get_attribute does not search superclasses, for that you
1341 need to use C<find_attribute_by_name>.
1342
1343 =item B<< $metaclass->has_attribute($attribute_name) >>
1344
1345 Returns a boolean indicating whether or not the class defines the
1346 named attribute. It does not include attributes inherited from parent
1347 classes.
1348
1349 =item B<< $metaclass->get_attribute_list >>
1350
1351 This will return a list of attributes I<names> for all attributes
1352 defined in this class.
1353
1354 =item B<< $metaclass->get_all_attributes >>
1355
1356 This will traverse the inheritance hierarchy and return a list of all
1357 the L<Class::MOP::Attribute> objects for this class and its parents.
1358
1359 =item B<< $metaclass->find_attribute_by_name($attribute_name) >>
1360
1361 This will return a L<Class::MOP::Attribute> for the specified
1362 C<$attribute_name>. If the class does not have the specified
1363 attribute, it returns C<undef>.
1364
1365 Unlike C<get_attribute>, this attribute I<will> look for the named
1366 attribute in superclasses.
1367
1368 =item B<< $metaclass->add_attribute(...) >>
1369
1370 This method accepts either an existing L<Class::MOP::Attribute>
1371 object or parameters suitable for passing to that class's C<new>
1372 method.
1373
1374 The attribute provided will be added to the class.
1375
1376 Any accessor methods defined by the attribute will be added to the
1377 class when the attribute is added.
1378
1379 If an attribute of the same name already exists, the old attribute
1380 will be removed first.
1381
1382 =item B<< $metaclass->remove_attribute($attribute_name) >>
1383
1384 This will remove the named attribute from the class, and
1385 L<Class::MOP::Attribute> object.
1386
1387 Removing an attribute also removes any accessor methods defined by the
1388 attribute.
1389
1390 However, note that removing an attribute will only affect I<future>
1391 object instances created for this class, not existing instances.
1392
1393 =item B<< $metaclass->attribute_metaclass >>
1394
1395 Returns the class name of the attribute metaclass for this class. By
1396 default, this is L<Class::MOP::Attribute>.
1397
1398 =back
1399
1400 =head2 Class Immutability
1401
1402 Making a class immutable "freezes" the class definition. You can no
1403 longer call methods which alter the class, such as adding or removing
1404 methods or attributes.
1405
1406 Making a class immutable lets us optimize the class by inlining some
1407 methods, and also allows us to optimize some methods on the metaclass
1408 object itself.
1409
1410 After immutabilization, the metaclass object will cache most informational
1411 methods that returns information about methods or attributes. Methods which
1412 would alter the class, such as C<add_attribute> and C<add_method>, will
1413 throw an error on an immutable metaclass object.
1414
1415 The immutabilization system in L<Moose> takes much greater advantage
1416 of the inlining features than Class::MOP itself does.
1417
1418 =over 4
1419
1420 =item B<< $metaclass->make_immutable(%options) >>
1421
1422 This method will create an immutable transformer and use it to make
1423 the class and its metaclass object immutable.
1424
1425 This method accepts the following options:
1426
1427 =over 8
1428
1429 =item * inline_accessors
1430
1431 =item * inline_constructor
1432
1433 =item * inline_destructor
1434
1435 These are all booleans indicating whether the specified method(s)
1436 should be inlined.
1437
1438 By default, accessors and the constructor are inlined, but not the
1439 destructor.
1440
1441 =item * immutable_trait
1442
1443 The name of a class which will be used as a parent class for the
1444 metaclass object being made immutable. This "trait" implements the
1445 post-immutability functionality of the metaclass (but not the
1446 transformation itself).
1447
1448 This defaults to L<Class::MOP::Class::Immutable::Trait>.
1449
1450 =item * constructor_name
1451
1452 This is the constructor method name. This defaults to "new".
1453
1454 =item * constructor_class
1455
1456 The name of the method metaclass for constructors. It will be used to
1457 generate the inlined constructor. This defaults to
1458 "Class::MOP::Method::Constructor".
1459
1460 =item * replace_constructor
1461
1462 This is a boolean indicating whether an existing constructor should be
1463 replaced when inlining a constructor. This defaults to false.
1464
1465 =item * destructor_class
1466
1467 The name of the method metaclass for destructors. It will be used to
1468 generate the inlined destructor. This defaults to
1469 "Class::MOP::Method::Denstructor".
1470
1471 =item * replace_destructor
1472
1473 This is a boolean indicating whether an existing destructor should be
1474 replaced when inlining a destructor. This defaults to false.
1475
1476 =back
1477
1478 =item B<< $metaclass->immutable_options >>
1479
1480 Returns a hash of the options used when making the class immutable, including
1481 both defaults and anything supplied by the user in the call to C<<
1482 $metaclass->make_immutable >>. This is useful if you need to temporarily make
1483 a class mutable and then restore immutability as it was before.
1484
1485 =item B<< $metaclass->make_mutable >>
1486
1487 Calling this method reverse the immutabilization transformation.
1488
1489 =back
1490
1491 =head2 Method Modifiers
1492
1493 Method modifiers are hooks which allow a method to be wrapped with
1494 I<before>, I<after> and I<around> method modifiers. Every time a
1495 method is called, its modifiers are also called.
1496
1497 A class can modify its own methods, as well as methods defined in
1498 parent classes.
1499
1500 =head3 How method modifiers work?
1501
1502 Method modifiers work by wrapping the original method and then
1503 replacing it in the class's symbol table. The wrappers will handle
1504 calling all the modifiers in the appropriate order and preserving the
1505 calling context for the original method.
1506
1507 The return values of C<before> and C<after> modifiers are
1508 ignored. This is because their purpose is B<not> to filter the input
1509 and output of the primary method (this is done with an I<around>
1510 modifier).
1511
1512 This may seem like an odd restriction to some, but doing this allows
1513 for simple code to be added at the beginning or end of a method call
1514 without altering the function of the wrapped method or placing any
1515 extra responsibility on the code of the modifier.
1516
1517 Of course if you have more complex needs, you can use the C<around>
1518 modifier which allows you to change both the parameters passed to the
1519 wrapped method, as well as its return value.
1520
1521 Before and around modifiers are called in last-defined-first-called
1522 order, while after modifiers are called in first-defined-first-called
1523 order. So the call tree might looks something like this:
1524
1525   before 2
1526    before 1
1527     around 2
1528      around 1
1529       primary
1530      around 1
1531     around 2
1532    after 1
1533   after 2
1534
1535 =head3 What is the performance impact?
1536
1537 Of course there is a performance cost associated with method
1538 modifiers, but we have made every effort to make that cost directly
1539 proportional to the number of modifier features you use.
1540
1541 The wrapping method does its best to B<only> do as much work as it
1542 absolutely needs to. In order to do this we have moved some of the
1543 performance costs to set-up time, where they are easier to amortize.
1544
1545 All this said, our benchmarks have indicated the following:
1546
1547   simple wrapper with no modifiers             100% slower
1548   simple wrapper with simple before modifier   400% slower
1549   simple wrapper with simple after modifier    450% slower
1550   simple wrapper with simple around modifier   500-550% slower
1551   simple wrapper with all 3 modifiers          1100% slower
1552
1553 These numbers may seem daunting, but you must remember, every feature
1554 comes with some cost. To put things in perspective, just doing a
1555 simple C<AUTOLOAD> which does nothing but extract the name of the
1556 method called and return it costs about 400% over a normal method
1557 call.
1558
1559 =over 4
1560
1561 =item B<< $metaclass->add_before_method_modifier($method_name, $code) >>
1562
1563 This wraps the specified method with the supplied subroutine
1564 reference. The modifier will be called as a method itself, and will
1565 receive the same arguments as are passed to the method.
1566
1567 When the modifier exits, the wrapped method will be called.
1568
1569 The return value of the modifier will be ignored.
1570
1571 =item B<< $metaclass->add_after_method_modifier($method_name, $code) >>
1572
1573 This wraps the specified method with the supplied subroutine
1574 reference. The modifier will be called as a method itself, and will
1575 receive the same arguments as are passed to the method.
1576
1577 When the wrapped methods exits, the modifier will be called.
1578
1579 The return value of the modifier will be ignored.
1580
1581 =item B<< $metaclass->add_around_method_modifier($method_name, $code) >>
1582
1583 This wraps the specified method with the supplied subroutine
1584 reference.
1585
1586 The first argument passed to the modifier will be a subroutine
1587 reference to the wrapped method. The second argument is the object,
1588 and after that come any arguments passed when the method is called.
1589
1590 The around modifier can choose to call the original method, as well as
1591 what arguments to pass if it does so.
1592
1593 The return value of the modifier is what will be seen by the caller.
1594
1595 =back
1596
1597 =head2 Introspection
1598
1599 =over 4
1600
1601 =item B<< Class::MOP::Class->meta >>
1602
1603 This will return a L<Class::MOP::Class> instance for this class.
1604
1605 It should also be noted that L<Class::MOP> will actually bootstrap
1606 this module by installing a number of attribute meta-objects into its
1607 metaclass.
1608
1609 =back
1610
1611 =head1 AUTHORS
1612
1613 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1614
1615 =head1 COPYRIGHT AND LICENSE
1616
1617 Copyright 2006-2009 by Infinity Interactive, Inc.
1618
1619 L<http://www.iinteractive.com>
1620
1621 This library is free software; you can redistribute it and/or modify
1622 it under the same terms as Perl itself.
1623
1624 =cut