2 package Class::MOP::Class;
7 use Class::MOP::Instance;
8 use Class::MOP::Method::Wrapped;
9 use Class::MOP::Method::Accessor;
10 use Class::MOP::Method::Constructor;
13 use Scalar::Util 'blessed', 'reftype', 'weaken';
14 use Sub::Name 'subname';
15 use Devel::GlobalDestruction 'in_global_destruction';
17 use List::MoreUtils 'all';
19 our $VERSION = '1.01';
20 $VERSION = eval $VERSION;
21 our $AUTHORITY = 'cpan:STEVAN';
23 use base 'Class::MOP::Module',
24 'Class::MOP::Mixin::HasAttributes',
25 'Class::MOP::Mixin::HasMethods';
35 $package_name = shift;
38 $package_name = $options{package};
41 ($package_name && !ref($package_name))
42 || confess "You must pass a package name and it cannot be blessed";
44 return Class::MOP::get_metaclass_by_name($package_name)
45 || $class->_construct_class_instance(package => $package_name, @_);
48 # NOTE: (meta-circularity)
49 # this is a special form of _construct_instance
50 # (see below), which is used to construct class
51 # meta-object instances for any Class::MOP::*
52 # class. All other classes will use the more
53 # normal &construct_instance.
54 sub _construct_class_instance {
56 my $options = @_ == 1 ? $_[0] : {@_};
57 my $package_name = $options->{package};
58 (defined $package_name && $package_name)
59 || confess "You must pass a package name";
61 # return the metaclass if we have it cached,
62 # and it is still defined (it has not been
63 # reaped by DESTROY yet, which can happen
64 # annoyingly enough during global destruction)
66 if (defined(my $meta = Class::MOP::get_metaclass_by_name($package_name))) {
72 ? $class->_real_ref_name
75 # now create the metaclass
77 if ($class eq 'Class::MOP::Class') {
78 $meta = $class->_new($options);
82 # it is safe to use meta here because
83 # class will always be a subclass of
84 # Class::MOP::Class, which defines meta
85 $meta = $class->meta->_construct_instance($options)
88 # and check the metaclass compatibility
89 $meta->_check_metaclass_compatibility();
91 Class::MOP::store_metaclass_by_name($package_name, $meta);
94 # we need to weaken any anon classes
95 # so that they can call DESTROY properly
96 Class::MOP::weaken_metaclass($package_name) if $meta->is_anon_class;
104 # NOTE: we need to deal with the possibility of class immutability here,
105 # and then get the name of the class appropriately
106 return $self->is_immutable
107 ? $self->_get_mutable_metaclass_name()
114 return Class::MOP::Class->initialize($class)->new_object(@_)
115 if $class ne __PACKAGE__;
117 my $options = @_ == 1 ? $_[0] : {@_};
120 # inherited from Class::MOP::Package
121 'package' => $options->{package},
124 # since the following attributes will
125 # actually be loaded from the symbol
126 # table, and actually bypass the instance
127 # entirely, we can just leave these things
128 # listed here for reference, because they
129 # should not actually have a value associated
131 'namespace' => \undef,
134 # inherited from Class::MOP::Module
136 'authority' => \undef,
138 # defined in Class::MOP::Class
139 'superclasses' => \undef,
142 'attribute_metaclass' =>
143 ( $options->{'attribute_metaclass'} || 'Class::MOP::Attribute' ),
144 'method_metaclass' =>
145 ( $options->{'method_metaclass'} || 'Class::MOP::Method' ),
146 'wrapped_method_metaclass' => (
147 $options->{'wrapped_method_metaclass'}
148 || 'Class::MOP::Method::Wrapped'
150 'instance_metaclass' =>
151 ( $options->{'instance_metaclass'} || 'Class::MOP::Instance' ),
152 'immutable_trait' => (
153 $options->{'immutable_trait'}
154 || 'Class::MOP::Class::Immutable::Trait'
156 'constructor_name' => ( $options->{constructor_name} || 'new' ),
157 'constructor_class' => (
158 $options->{constructor_class} || 'Class::MOP::Method::Constructor'
160 'destructor_class' => $options->{destructor_class},
164 sub reset_package_cache_flag { (shift)->{'_package_cache_flag'} = undef }
165 sub update_package_cache_flag {
168 # we can manually update the cache number
169 # since we are actually adding the method
170 # to our cache as well. This avoids us
171 # having to regenerate the method_map.
173 $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);
176 ## Metaclass compatibility
178 my %base_metaclass = (
179 attribute_metaclass => 'Class::MOP::Attribute',
180 method_metaclass => 'Class::MOP::Method',
181 wrapped_method_metaclass => 'Class::MOP::Method::Wrapped',
182 instance_metaclass => 'Class::MOP::Instance',
183 constructor_class => 'Class::MOP::Method::Constructor',
184 destructor_class => 'Class::MOP::Method::Destructor',
187 sub _base_metaclasses { %base_metaclass }
190 sub _check_metaclass_compatibility {
193 if (my @superclasses = $self->superclasses) {
194 $self->_fix_metaclass_incompatibility(@superclasses);
196 my %base_metaclass = $self->_base_metaclasses;
198 # this is always okay ...
199 return if ref($self) eq 'Class::MOP::Class'
201 my $meta = $self->$_;
202 !defined($meta) || $meta eq $base_metaclass{$_}
203 } keys %base_metaclass;
205 for my $superclass (@superclasses) {
206 $self->_check_class_metaclass_compatibility($superclass);
209 for my $metaclass_type (keys %base_metaclass) {
210 next unless defined $self->$metaclass_type;
211 for my $superclass (@superclasses) {
212 $self->_check_single_metaclass_compatibility(
213 $metaclass_type, $superclass
220 sub _class_metaclass_is_compatible {
222 my ( $superclass_name ) = @_;
224 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
227 my $super_meta_type = $super_meta->_real_ref_name;
229 return $self->isa($super_meta_type);
232 sub _check_class_metaclass_compatibility {
234 my ( $superclass_name ) = @_;
236 if (!$self->_class_metaclass_is_compatible($superclass_name)) {
237 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
239 my $super_meta_type = $super_meta->_real_ref_name;
241 confess "The metaclass of " . $self->name . " ("
242 . (ref($self)) . ")" . " is not compatible with "
243 . "the metaclass of its superclass, "
244 . $superclass_name . " (" . ($super_meta_type) . ")";
248 sub _single_metaclass_is_compatible {
250 my ( $metaclass_type, $superclass_name ) = @_;
252 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
255 # for instance, Moose::Meta::Class has a error_class attribute, but
256 # Class::MOP::Class doesn't - this shouldn't be an error
257 return 1 unless $super_meta->can($metaclass_type);
258 # for instance, Moose::Meta::Class has a destructor_class, but
259 # Class::MOP::Class doesn't - this shouldn't be an error
260 return 1 if defined $self->$metaclass_type
261 && !defined $super_meta->$metaclass_type;
263 return $self->$metaclass_type->isa($super_meta->$metaclass_type);
266 sub _check_single_metaclass_compatibility {
268 my ( $metaclass_type, $superclass_name ) = @_;
270 if (!$self->_single_metaclass_is_compatible($metaclass_type, $superclass_name)) {
271 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
272 my $metaclass_type_name = $metaclass_type;
273 $metaclass_type_name =~ s/_(?:meta)?class$//;
274 $metaclass_type_name =~ s/_/ /g;
275 confess "The $metaclass_type_name metaclass for "
276 . $self->name . " (" . ($self->$metaclass_type)
277 . ")" . " is not compatible with the "
278 . "$metaclass_type_name metaclass of its "
279 . "superclass, " . $superclass_name . " ("
280 . ($super_meta->$metaclass_type) . ")";
284 sub _can_fix_class_metaclass_incompatibility_by_subclassing {
286 my ($super_meta) = @_;
288 my $super_meta_type = $super_meta->_real_ref_name;
290 return $super_meta_type ne blessed($self)
291 && $super_meta->isa(blessed($self));
294 sub _can_fix_single_metaclass_incompatibility_by_subclassing {
296 my ($metaclass_type, $super_meta) = @_;
298 my $specific_meta = $self->$metaclass_type;
299 return unless $super_meta->can($metaclass_type);
300 my $super_specific_meta = $super_meta->$metaclass_type;
302 # for instance, Moose::Meta::Class has a destructor_class, but
303 # Class::MOP::Class doesn't - this shouldn't be an error
304 return if defined $specific_meta
305 && !defined $super_specific_meta;
307 return $specific_meta ne $super_specific_meta
308 && $super_specific_meta->isa($specific_meta);
311 sub _can_fix_metaclass_incompatibility_by_subclassing {
313 my ($super_meta) = @_;
315 return 1 if $self->_can_fix_class_metaclass_incompatibility_by_subclassing($super_meta);
317 my %base_metaclass = $self->_base_metaclasses;
318 for my $metaclass_type (keys %base_metaclass) {
319 next unless defined $self->$metaclass_type;
320 return 1 if $self->_can_fix_single_metaclass_incompatibility_by_subclassing($metaclass_type, $super_meta);
326 sub _can_fix_metaclass_incompatibility {
328 return $self->_can_fix_metaclass_incompatibility_by_subclassing(@_);
331 sub _fix_metaclass_incompatibility {
333 my @supers = map { Class::MOP::Class->initialize($_) } @_;
336 for my $super (@supers) {
338 if $self->_can_fix_metaclass_incompatibility($super);
340 return unless $necessary;
342 for my $super (@supers) {
343 if (!$self->_class_metaclass_is_compatible($super->name)) {
344 $self->_fix_class_metaclass_incompatibility($super);
348 my %base_metaclass = $self->_base_metaclasses;
349 for my $metaclass_type (keys %base_metaclass) {
350 next unless defined $self->$metaclass_type;
351 for my $super (@supers) {
352 if (!$self->_single_metaclass_is_compatible($metaclass_type, $super->name)) {
353 $self->_fix_single_metaclass_incompatibility(
354 $metaclass_type, $super
361 sub _fix_class_metaclass_incompatibility {
363 my ( $super_meta ) = @_;
365 if ($self->_can_fix_class_metaclass_incompatibility_by_subclassing($super_meta)) {
367 || confess "Can't fix metaclass incompatibility for "
369 . " because it is not pristine.";
371 my $super_meta_name = $super_meta->_real_ref_name;
373 $super_meta_name->meta->rebless_instance($self);
377 sub _fix_single_metaclass_incompatibility {
379 my ( $metaclass_type, $super_meta ) = @_;
381 if ($self->_can_fix_single_metaclass_incompatibility_by_subclassing($metaclass_type, $super_meta)) {
383 || confess "Can't fix metaclass incompatibility for "
385 . " because it is not pristine.";
387 $self->{$metaclass_type} = $super_meta->$metaclass_type;
395 # this should be sufficient, if you have a
396 # use case where it is not, write a test and
398 my $ANON_CLASS_SERIAL = 0;
401 # we need a sufficiently annoying prefix
402 # this should suffice for now, this is
403 # used in a couple of places below, so
404 # need to put it up here for now.
405 my $ANON_CLASS_PREFIX = 'Class::MOP::Class::__ANON__::SERIAL::';
409 no warnings 'uninitialized';
410 $self->name =~ /^$ANON_CLASS_PREFIX/o;
413 sub create_anon_class {
414 my ($class, %options) = @_;
415 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
416 return $class->create($package_name, %options);
420 # this will only get called for
421 # anon-classes, all other calls
422 # are assumed to occur during
423 # global destruction and so don't
424 # really need to be handled explicitly
428 return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated
430 no warnings 'uninitialized';
431 my $name = $self->name;
432 return unless $name =~ /^$ANON_CLASS_PREFIX/o;
434 # Moose does a weird thing where it replaces the metaclass for
435 # class when fixing metaclass incompatibility. In that case,
436 # we don't want to clean out the namespace now. We can detect
437 # that because Moose will explicitly update the singleton
438 # cache in Class::MOP.
439 my $current_meta = Class::MOP::get_metaclass_by_name($name);
440 return if $current_meta ne $self;
442 my ($serial_id) = ($name =~ /^$ANON_CLASS_PREFIX(\d+)/o);
444 @{$name . '::ISA'} = ();
445 %{$name . '::'} = ();
446 delete ${$ANON_CLASS_PREFIX}{$serial_id . '::'};
448 Class::MOP::remove_metaclass_by_name($name);
453 # creating classes with MOP ...
456 my ( $class, @args ) = @_;
458 unshift @args, 'package' if @args % 2 == 1;
460 my (%options) = @args;
461 my $package_name = $options{package};
463 (ref $options{superclasses} eq 'ARRAY')
464 || confess "You must pass an ARRAY ref of superclasses"
465 if exists $options{superclasses};
467 (ref $options{attributes} eq 'ARRAY')
468 || confess "You must pass an ARRAY ref of attributes"
469 if exists $options{attributes};
471 (ref $options{methods} eq 'HASH')
472 || confess "You must pass a HASH ref of methods"
473 if exists $options{methods};
475 my (%initialize_options) = @args;
476 delete @initialize_options{qw(
484 my $meta = $class->initialize( $package_name => %initialize_options );
486 $meta->_instantiate_module( $options{version}, $options{authority} );
489 $meta->add_method('meta' => sub {
490 $class->initialize(ref($_[0]) || $_[0]);
493 $meta->superclasses(@{$options{superclasses}})
494 if exists $options{superclasses};
496 # process attributes first, so that they can
497 # install accessors, but locally defined methods
498 # can then overwrite them. It is maybe a little odd, but
499 # I think this should be the order of things.
500 if (exists $options{attributes}) {
501 foreach my $attr (@{$options{attributes}}) {
502 $meta->add_attribute($attr);
505 if (exists $options{methods}) {
506 foreach my $method_name (keys %{$options{methods}}) {
507 $meta->add_method($method_name, $options{methods}->{$method_name});
516 # all these attribute readers will be bootstrapped
517 # away in the Class::MOP bootstrap section
519 sub instance_metaclass { $_[0]->{'instance_metaclass'} }
520 sub immutable_trait { $_[0]->{'immutable_trait'} }
521 sub constructor_class { $_[0]->{'constructor_class'} }
522 sub constructor_name { $_[0]->{'constructor_name'} }
523 sub destructor_class { $_[0]->{'destructor_class'} }
525 # Instance Construction & Cloning
531 # we need to protect the integrity of the
532 # Class::MOP::Class singletons here, so we
533 # delegate this to &construct_class_instance
534 # which will deal with the singletons
535 return $class->_construct_class_instance(@_)
536 if $class->name->isa('Class::MOP::Class');
537 return $class->_construct_instance(@_);
540 sub _construct_instance {
542 my $params = @_ == 1 ? $_[0] : {@_};
543 my $meta_instance = $class->get_meta_instance();
545 # the code below is almost certainly incorrect
546 # but this is foreign inheritance, so we might
547 # have to kludge it in the end.
549 if (my $instance_class = blessed($params->{__INSTANCE__})) {
550 ($instance_class eq $class->name)
551 || confess "Objects passed as the __INSTANCE__ parameter must "
552 . "already be blessed into the correct class, but "
553 . "$params->{__INSTANCE__} is not a " . $class->name;
554 $instance = $params->{__INSTANCE__};
556 elsif (exists $params->{__INSTANCE__}) {
557 confess "The __INSTANCE__ parameter must be a blessed reference, not "
558 . $params->{__INSTANCE__};
561 $instance = $meta_instance->create_instance();
563 foreach my $attr ($class->get_all_attributes()) {
564 $attr->initialize_instance_slot($meta_instance, $instance, $params);
567 # this will only work for a HASH instance type
568 if ($class->is_anon_class) {
569 (reftype($instance) eq 'HASH')
570 || confess "Currently only HASH based instances are supported with instance of anon-classes";
572 # At some point we should make this official
573 # as a reserved slot name, but right now I am
574 # going to keep it here.
575 # my $RESERVED_MOP_SLOT = '__MOP__';
576 $instance->{'__MOP__'} = $class;
582 sub get_meta_instance {
584 $self->{'_meta_instance'} ||= $self->_create_meta_instance();
587 sub _create_meta_instance {
590 my $instance = $self->instance_metaclass->new(
591 associated_metaclass => $self,
592 attributes => [ $self->get_all_attributes() ],
595 $self->add_meta_instance_dependencies()
596 if $instance->is_dependent_on_superclasses();
603 my $instance = shift;
604 (blessed($instance) && $instance->isa($class->name))
605 || confess "You must pass an instance of the metaclass (" . (ref $class ? $class->name : $class) . "), not ($instance)";
608 # we need to protect the integrity of the
609 # Class::MOP::Class singletons here, they
610 # should not be cloned.
611 return $instance if $instance->isa('Class::MOP::Class');
612 $class->_clone_instance($instance, @_);
615 sub _clone_instance {
616 my ($class, $instance, %params) = @_;
618 || confess "You can only clone instances, ($instance) is not a blessed instance";
619 my $meta_instance = $class->get_meta_instance();
620 my $clone = $meta_instance->clone_instance($instance);
621 foreach my $attr ($class->get_all_attributes()) {
622 if ( defined( my $init_arg = $attr->init_arg ) ) {
623 if (exists $params{$init_arg}) {
624 $attr->set_value($clone, $params{$init_arg});
631 sub rebless_instance {
632 my ($self, $instance, %params) = @_;
634 my $old_metaclass = Class::MOP::class_of($instance);
636 my $old_class = $old_metaclass ? $old_metaclass->name : blessed($instance);
637 $self->name->isa($old_class)
638 || confess "You may rebless only into a subclass of ($old_class), of which (". $self->name .") isn't.";
640 $old_metaclass->rebless_instance_away($instance, $self, %params)
643 my $meta_instance = $self->get_meta_instance();
646 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
647 $meta_instance->rebless_instance_structure($_[1], $self);
649 foreach my $attr ( $self->get_all_attributes ) {
650 if ( $attr->has_value($instance) ) {
651 if ( defined( my $init_arg = $attr->init_arg ) ) {
652 $params{$init_arg} = $attr->get_value($instance)
653 unless exists $params{$init_arg};
656 $attr->set_value($instance, $attr->get_value($instance));
661 foreach my $attr ($self->get_all_attributes) {
662 $attr->initialize_instance_slot($meta_instance, $instance, \%params);
668 sub rebless_instance_back {
669 my ( $self, $instance ) = @_;
671 my $old_metaclass = Class::MOP::class_of($instance);
674 = $old_metaclass ? $old_metaclass->name : blessed($instance);
675 $old_class->isa( $self->name )
677 "You may rebless only into a superclass of ($old_class), of which ("
681 $old_metaclass->rebless_instance_away( $instance, $self )
684 my $meta_instance = $self->get_meta_instance;
686 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
687 $meta_instance->rebless_instance_structure( $_[1], $self );
689 for my $attr ( $old_metaclass->get_all_attributes ) {
690 next if $self->has_attribute( $attr->name );
691 $meta_instance->deinitialize_slot( $instance, $_ ) for $attr->slots;
697 sub rebless_instance_away {
698 # this intentionally does nothing, it is just a hook
701 sub _attach_attribute {
702 my ($self, $attribute) = @_;
703 $attribute->attach_to_class($self);
706 sub _post_add_attribute {
707 my ( $self, $attribute ) = @_;
709 $self->invalidate_meta_instances;
711 # invalidate package flag here
714 $attribute->install_accessors;
717 $self->remove_attribute( $attribute->name );
722 sub remove_attribute {
725 my $removed_attribute = $self->SUPER::remove_attribute(@_)
728 $self->invalidate_meta_instances;
730 $removed_attribute->remove_accessors;
731 $removed_attribute->detach_from_class;
733 return$removed_attribute;
736 sub find_attribute_by_name {
737 my ( $self, $attr_name ) = @_;
739 foreach my $class ( $self->linearized_isa ) {
740 # fetch the meta-class ...
741 my $meta = Class::MOP::Class->initialize($class);
742 return $meta->get_attribute($attr_name)
743 if $meta->has_attribute($attr_name);
749 sub get_all_attributes {
751 my %attrs = map { %{ Class::MOP::Class->initialize($_)->_attribute_map } }
752 reverse $self->linearized_isa;
753 return values %attrs;
760 my $var_spec = { sigil => '@', type => 'ARRAY', name => 'ISA' };
763 @{$self->get_package_symbol($var_spec)} = @supers;
766 # on 5.8 and below, we need to call
767 # a method to get Perl to detect
768 # a cycle in the class hierarchy
769 my $class = $self->name;
773 # we need to check the metaclass
774 # compatibility here so that we can
775 # be sure that the superclass is
776 # not potentially creating an issues
777 # we don't know about
779 $self->_check_metaclass_compatibility();
780 $self->_superclasses_updated();
782 @{$self->get_package_symbol($var_spec)};
785 sub _superclasses_updated {
787 $self->update_meta_instance_dependencies();
792 my $super_class = $self->name;
794 return @{ $super_class->mro::get_isarev() };
797 sub direct_subclasses {
799 my $super_class = $self->name;
804 } Class::MOP::Class->initialize($_)->superclasses
809 return @{ mro::get_linear_isa( (shift)->name ) };
812 sub class_precedence_list {
814 my $name = $self->name;
816 unless (Class::MOP::IS_RUNNING_ON_5_10()) {
818 # We need to check for circular inheritance here
819 # if we are are not on 5.10, cause 5.8 detects it
820 # late. This will do nothing if all is well, and
821 # blow up otherwise. Yes, it's an ugly hack, better
822 # suggestions are welcome.
824 ($name || return)->isa('This is a test for circular inheritance')
827 # if our mro is c3, we can
828 # just grab the linear_isa
829 if (mro::get_mro($name) eq 'c3') {
830 return @{ mro::get_linear_isa($name) }
834 # we can't grab the linear_isa for dfs
835 # since it has all the duplicates
840 Class::MOP::Class->initialize($_)->class_precedence_list()
841 } $self->superclasses()
849 my $fetch_and_prepare_method = sub {
850 my ($self, $method_name) = @_;
851 my $wrapped_metaclass = $self->wrapped_method_metaclass;
853 my $method = $self->get_method($method_name);
854 # if we dont have local ...
856 # try to find the next method
857 $method = $self->find_next_method_by_name($method_name);
858 # die if it does not exist
860 || confess "The method '$method_name' was not found in the inheritance hierarchy for " . $self->name;
861 # and now make sure to wrap it
862 # even if it is already wrapped
863 # because we need a new sub ref
864 $method = $wrapped_metaclass->wrap($method,
865 package_name => $self->name,
866 name => $method_name,
870 # now make sure we wrap it properly
871 $method = $wrapped_metaclass->wrap($method,
872 package_name => $self->name,
873 name => $method_name,
874 ) unless $method->isa($wrapped_metaclass);
876 $self->add_method($method_name => $method);
880 sub add_before_method_modifier {
881 my ($self, $method_name, $method_modifier) = @_;
882 (defined $method_name && length $method_name)
883 || confess "You must pass in a method name";
884 my $method = $fetch_and_prepare_method->($self, $method_name);
885 $method->add_before_modifier(
886 subname(':before' => $method_modifier)
890 sub add_after_method_modifier {
891 my ($self, $method_name, $method_modifier) = @_;
892 (defined $method_name && length $method_name)
893 || confess "You must pass in a method name";
894 my $method = $fetch_and_prepare_method->($self, $method_name);
895 $method->add_after_modifier(
896 subname(':after' => $method_modifier)
900 sub add_around_method_modifier {
901 my ($self, $method_name, $method_modifier) = @_;
902 (defined $method_name && length $method_name)
903 || confess "You must pass in a method name";
904 my $method = $fetch_and_prepare_method->($self, $method_name);
905 $method->add_around_modifier(
906 subname(':around' => $method_modifier)
911 # the methods above used to be named like this:
912 # ${pkg}::${method}:(before|after|around)
913 # but this proved problematic when using one modifier
914 # to wrap multiple methods (something which is likely
915 # to happen pretty regularly IMO). So instead of naming
916 # it like this, I have chosen to just name them purely
917 # with their modifier names, like so:
918 # :(before|after|around)
919 # The fact is that in a stack trace, it will be fairly
920 # evident from the context what method they are attached
921 # to, and so don't need the fully qualified name.
924 sub find_method_by_name {
925 my ($self, $method_name) = @_;
926 (defined $method_name && length $method_name)
927 || confess "You must define a method name to find";
928 foreach my $class ($self->linearized_isa) {
929 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
930 return $method if defined $method;
935 sub get_all_methods {
939 for my $class ( reverse $self->linearized_isa ) {
940 my $meta = Class::MOP::Class->initialize($class);
942 $methods{$_} = $meta->get_method($_)
943 for $meta->get_method_list;
946 return values %methods;
949 sub get_all_method_names {
952 return grep { !$uniq{$_}++ } map { Class::MOP::Class->initialize($_)->get_method_list } $self->linearized_isa;
955 sub find_all_methods_by_name {
956 my ($self, $method_name) = @_;
957 (defined $method_name && length $method_name)
958 || confess "You must define a method name to find";
960 foreach my $class ($self->linearized_isa) {
961 # fetch the meta-class ...
962 my $meta = Class::MOP::Class->initialize($class);
964 name => $method_name,
966 code => $meta->get_method($method_name)
967 } if $meta->has_method($method_name);
972 sub find_next_method_by_name {
973 my ($self, $method_name) = @_;
974 (defined $method_name && length $method_name)
975 || confess "You must define a method name to find";
976 my @cpl = $self->linearized_isa;
977 shift @cpl; # discard ourselves
978 foreach my $class (@cpl) {
979 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
980 return $method if defined $method;
985 sub update_meta_instance_dependencies {
988 if ( $self->{meta_instance_dependencies} ) {
989 return $self->add_meta_instance_dependencies;
993 sub add_meta_instance_dependencies {
996 $self->remove_meta_instance_dependencies;
998 my @attrs = $self->get_all_attributes();
1001 my @classes = grep { not $seen{ $_->name }++ }
1002 map { $_->associated_class } @attrs;
1004 foreach my $class (@classes) {
1005 $class->add_dependent_meta_instance($self);
1008 $self->{meta_instance_dependencies} = \@classes;
1011 sub remove_meta_instance_dependencies {
1014 if ( my $classes = delete $self->{meta_instance_dependencies} ) {
1015 foreach my $class (@$classes) {
1016 $class->remove_dependent_meta_instance($self);
1026 sub add_dependent_meta_instance {
1027 my ( $self, $metaclass ) = @_;
1028 push @{ $self->{dependent_meta_instances} }, $metaclass;
1031 sub remove_dependent_meta_instance {
1032 my ( $self, $metaclass ) = @_;
1033 my $name = $metaclass->name;
1034 @$_ = grep { $_->name ne $name } @$_
1035 for $self->{dependent_meta_instances};
1038 sub invalidate_meta_instances {
1040 $_->invalidate_meta_instance()
1041 for $self, @{ $self->{dependent_meta_instances} };
1044 sub invalidate_meta_instance {
1046 undef $self->{_meta_instance};
1049 # check if we can reinitialize
1053 # if any local attr is defined
1054 return if $self->get_attribute_list;
1056 # or any non-declared methods
1057 for my $method ( map { $self->get_method($_) } $self->get_method_list ) {
1058 return if $method->isa("Class::MOP::Method::Generated");
1059 # FIXME do we need to enforce this too? return unless $method->isa( $self->method_metaclass );
1067 sub is_mutable { 1 }
1068 sub is_immutable { 0 }
1070 sub immutable_options { %{ $_[0]{__immutable}{options} || {} } }
1072 sub _immutable_options {
1073 my ( $self, @args ) = @_;
1076 inline_accessors => 1,
1077 inline_constructor => 1,
1078 inline_destructor => 0,
1080 immutable_trait => $self->immutable_trait,
1081 constructor_name => $self->constructor_name,
1082 constructor_class => $self->constructor_class,
1083 destructor_class => $self->destructor_class,
1088 sub make_immutable {
1089 my ( $self, @args ) = @_;
1091 if ( $self->is_mutable ) {
1092 $self->_initialize_immutable( $self->_immutable_options(@args) );
1093 $self->_rebless_as_immutable(@args);
1104 if ( $self->is_immutable ) {
1105 my @args = $self->immutable_options;
1106 $self->_rebless_as_mutable();
1107 $self->_remove_inlined_code(@args);
1108 delete $self->{__immutable};
1116 sub _rebless_as_immutable {
1117 my ( $self, @args ) = @_;
1119 $self->{__immutable}{original_class} = ref $self;
1121 bless $self => $self->_immutable_metaclass(@args);
1124 sub _immutable_metaclass {
1125 my ( $self, %args ) = @_;
1127 if ( my $class = $args{immutable_metaclass} ) {
1131 my $trait = $args{immutable_trait} = $self->immutable_trait
1132 || confess "no immutable trait specified for $self";
1134 my $meta = $self->meta;
1135 my $meta_attr = $meta->find_attribute_by_name("immutable_trait");
1139 if ( $meta_attr and $trait eq $meta_attr->default ) {
1140 # if the trait is the same as the default we try and pick a
1141 # predictable name for the immutable metaclass
1142 $class_name = 'Class::MOP::Class::Immutable::' . ref($self);
1145 $class_name = join '::', 'Class::MOP::Class::Immutable::CustomTrait',
1146 $trait, 'ForMetaClass', ref($self);
1150 if Class::MOP::is_class_loaded($class_name);
1152 # If the metaclass is a subclass of CMOP::Class which has had
1153 # metaclass roles applied (via Moose), then we want to make sure
1154 # that we preserve that anonymous class (see Fey::ORM for an
1155 # example of where this matters).
1156 my $meta_name = $meta->_real_ref_name;
1158 my $immutable_meta = $meta_name->create(
1160 superclasses => [ ref $self ],
1163 Class::MOP::load_class($trait);
1164 for my $meth ( Class::MOP::Class->initialize($trait)->get_all_methods ) {
1165 my $meth_name = $meth->name;
1167 if ( $immutable_meta->find_method_by_name( $meth_name ) ) {
1168 $immutable_meta->add_around_method_modifier( $meth_name, $meth->body );
1171 $immutable_meta->add_method( $meth_name, $meth->clone );
1175 $immutable_meta->make_immutable(
1176 inline_constructor => 0,
1177 inline_accessors => 0,
1183 sub _remove_inlined_code {
1186 $self->remove_method( $_->name ) for $self->_inlined_methods;
1188 delete $self->{__immutable}{inlined_methods};
1191 sub _inlined_methods { @{ $_[0]{__immutable}{inlined_methods} || [] } }
1193 sub _add_inlined_method {
1194 my ( $self, $method ) = @_;
1196 push @{ $self->{__immutable}{inlined_methods} ||= [] }, $method;
1199 sub _initialize_immutable {
1200 my ( $self, %args ) = @_;
1202 $self->{__immutable}{options} = \%args;
1203 $self->_install_inlined_code(%args);
1206 sub _install_inlined_code {
1207 my ( $self, %args ) = @_;
1210 $self->_inline_accessors(%args) if $args{inline_accessors};
1211 $self->_inline_constructor(%args) if $args{inline_constructor};
1212 $self->_inline_destructor(%args) if $args{inline_destructor};
1215 sub _rebless_as_mutable {
1218 bless $self, $self->_get_mutable_metaclass_name;
1223 sub _inline_accessors {
1226 foreach my $attr_name ( $self->get_attribute_list ) {
1227 $self->get_attribute($attr_name)->install_accessors(1);
1231 sub _inline_constructor {
1232 my ( $self, %args ) = @_;
1234 my $name = $args{constructor_name};
1235 # A class may not even have a constructor, and that's okay.
1236 return unless defined $name;
1238 if ( $self->has_method($name) && !$args{replace_constructor} ) {
1239 my $class = $self->name;
1240 warn "Not inlining a constructor for $class since it defines"
1241 . " its own constructor.\n"
1242 . "If you are certain you don't need to inline your"
1243 . " constructor, specify inline_constructor => 0 in your"
1244 . " call to $class->meta->make_immutable\n";
1248 my $constructor_class = $args{constructor_class};
1250 Class::MOP::load_class($constructor_class);
1252 my $constructor = $constructor_class->new(
1256 package_name => $self->name,
1260 if ( $args{replace_constructor} or $constructor->can_be_inlined ) {
1261 $self->add_method( $name => $constructor );
1262 $self->_add_inlined_method($constructor);
1266 sub _inline_destructor {
1267 my ( $self, %args ) = @_;
1269 ( exists $args{destructor_class} && defined $args{destructor_class} )
1270 || confess "The 'inline_destructor' option is present, but "
1271 . "no destructor class was specified";
1273 if ( $self->has_method('DESTROY') && ! $args{replace_destructor} ) {
1274 my $class = $self->name;
1275 warn "Not inlining a destructor for $class since it defines"
1276 . " its own destructor.\n";
1280 my $destructor_class = $args{destructor_class};
1282 Class::MOP::load_class($destructor_class);
1284 return unless $destructor_class->is_needed($self);
1286 my $destructor = $destructor_class->new(
1289 package_name => $self->name,
1293 if ( $args{replace_destructor} or $destructor->can_be_inlined ) {
1294 $self->add_method( 'DESTROY' => $destructor );
1295 $self->_add_inlined_method($destructor);
1307 Class::MOP::Class - Class Meta Object
1311 # assuming that class Foo
1312 # has been defined, you can
1314 # use this for introspection ...
1316 # add a method to Foo ...
1317 Foo->meta->add_method( 'bar' => sub {...} )
1319 # get a list of all the classes searched
1320 # the method dispatcher in the correct order
1321 Foo->meta->class_precedence_list()
1323 # remove a method from Foo
1324 Foo->meta->remove_method('bar');
1326 # or use this to actually create classes ...
1328 Class::MOP::Class->create(
1331 superclasses => ['Foo'],
1333 Class::MOP::Attribute->new('$bar'),
1334 Class::MOP::Attribute->new('$baz'),
1337 calculate_bar => sub {...},
1338 construct_baz => sub {...}
1345 The Class Protocol is the largest and most complex part of the
1346 Class::MOP meta-object protocol. It controls the introspection and
1347 manipulation of Perl 5 classes, and it can create them as well. The
1348 best way to understand what this module can do is to read the
1349 documentation for each of its methods.
1353 C<Class::MOP::Class> is a subclass of L<Class::MOP::Module>.
1357 =head2 Class construction
1359 These methods all create new C<Class::MOP::Class> objects. These
1360 objects can represent existing classes or they can be used to create
1361 new classes from scratch.
1363 The metaclass object for a given class is a singleton. If you attempt
1364 to create a metaclass for the same class twice, you will just get the
1369 =item B<< Class::MOP::Class->create($package_name, %options) >>
1371 This method creates a new C<Class::MOP::Class> object with the given
1372 package name. It accepts a number of options:
1378 An optional version number for the newly created package.
1382 An optional authority for the newly created package.
1384 =item * superclasses
1386 An optional array reference of superclass names.
1390 An optional hash reference of methods for the class. The keys of the
1391 hash reference are method names and values are subroutine references.
1395 An optional array reference of L<Class::MOP::Attribute> objects.
1399 =item B<< Class::MOP::Class->create_anon_class(%options) >>
1401 This method works just like C<< Class::MOP::Class->create >> but it
1402 creates an "anonymous" class. In fact, the class does have a name, but
1403 that name is a unique name generated internally by this module.
1405 It accepts the same C<superclasses>, C<methods>, and C<attributes>
1406 parameters that C<create> accepts.
1408 Anonymous classes are destroyed once the metaclass they are attached
1409 to goes out of scope, and will be removed from Perl's internal symbol
1412 All instances of an anonymous class keep a special reference to the
1413 metaclass object, which prevents the metaclass from going out of scope
1414 while any instances exist.
1416 This only works if the instance is based on a hash reference, however.
1418 =item B<< Class::MOP::Class->initialize($package_name, %options) >>
1420 This method will initialize a C<Class::MOP::Class> object for the
1421 named package. Unlike C<create>, this method I<will not> create a new
1424 The purpose of this method is to retrieve a C<Class::MOP::Class>
1425 object for introspecting an existing class.
1427 If an existing C<Class::MOP::Class> object exists for the named
1428 package, it will be returned, and any options provided will be
1431 If the object does not yet exist, it will be created.
1433 The valid options that can be passed to this method are
1434 C<attribute_metaclass>, C<method_metaclass>,
1435 C<wrapped_method_metaclass>, and C<instance_metaclass>. These are all
1436 optional, and default to the appropriate class in the C<Class::MOP>
1441 =head2 Object instance construction and cloning
1443 These methods are all related to creating and/or cloning object
1448 =item B<< $metaclass->clone_object($instance, %params) >>
1450 This method clones an existing object instance. Any parameters you
1451 provide are will override existing attribute values in the object.
1453 This is a convenience method for cloning an object instance, then
1454 blessing it into the appropriate package.
1456 You could implement a clone method in your class, using this method:
1459 my ($self, %params) = @_;
1460 $self->meta->clone_object($self, %params);
1463 =item B<< $metaclass->rebless_instance($instance, %params) >>
1465 This method changes the class of C<$instance> to the metaclass's class.
1467 You can only rebless an instance into a subclass of its current
1468 class. If you pass any additional parameters, these will be treated
1469 like constructor parameters and used to initialize the object's
1470 attributes. Any existing attributes that are already set will be
1473 Before reblessing the instance, this method will call
1474 C<rebless_instance_away> on the instance's current metaclass. This method
1475 will be passed the instance, the new metaclass, and any parameters
1476 specified to C<rebless_instance>. By default, C<rebless_instance_away>
1477 does nothing; it is merely a hook.
1479 =item B<< $metaclass->rebless_instance_back($instance) >>
1481 Does the same thing as C<rebless_instance>, except that you can only
1482 rebless an instance into one of its superclasses. Any attributes that
1483 do not exist in the superclass will be deinitialized.
1485 This is a much more dangerous operation than C<rebless_instance>,
1486 especially when multiple inheritance is involved, so use this carefully!
1488 =item B<< $metaclass->new_object(%params) >>
1490 This method is used to create a new object of the metaclass's
1491 class. Any parameters you provide are used to initialize the
1492 instance's attributes. A special C<__INSTANCE__> key can be passed to
1493 provide an already generated instance, rather than having Class::MOP
1494 generate it for you. This is mostly useful for using Class::MOP with
1495 foreign classes which generate instances using their own constructors.
1497 =item B<< $metaclass->instance_metaclass >>
1499 Returns the class name of the instance metaclass. See
1500 L<Class::MOP::Instance> for more information on the instance
1503 =item B<< $metaclass->get_meta_instance >>
1505 Returns an instance of the C<instance_metaclass> to be used in the
1506 construction of a new instance of the class.
1510 =head2 Informational predicates
1512 These are a few predicate methods for asking information about the
1517 =item B<< $metaclass->is_anon_class >>
1519 This returns true if the class was created by calling C<<
1520 Class::MOP::Class->create_anon_class >>.
1522 =item B<< $metaclass->is_mutable >>
1524 This returns true if the class is still mutable.
1526 =item B<< $metaclass->is_immutable >>
1528 This returns true if the class has been made immutable.
1530 =item B<< $metaclass->is_pristine >>
1532 A class is I<not> pristine if it has non-inherited attributes or if it
1533 has any generated methods.
1537 =head2 Inheritance Relationships
1541 =item B<< $metaclass->superclasses(@superclasses) >>
1543 This is a read-write accessor which represents the superclass
1544 relationships of the metaclass's class.
1546 This is basically sugar around getting and setting C<@ISA>.
1548 =item B<< $metaclass->class_precedence_list >>
1550 This returns a list of all of the class's ancestor classes. The
1551 classes are returned in method dispatch order.
1553 =item B<< $metaclass->linearized_isa >>
1555 This returns a list based on C<class_precedence_list> but with all
1558 =item B<< $metaclass->subclasses >>
1560 This returns a list of all subclasses for this class, even indirect
1563 =item B<< $metaclass->direct_subclasses >>
1565 This returns a list of immediate subclasses for this class, which does not
1566 include indirect subclasses.
1570 =head2 Method introspection and creation
1572 These methods allow you to introspect a class's methods, as well as
1573 add, remove, or change methods.
1575 Determining what is truly a method in a Perl 5 class requires some
1576 heuristics (aka guessing).
1578 Methods defined outside the package with a fully qualified name (C<sub
1579 Package::name { ... }>) will be included. Similarly, methods named
1580 with a fully qualified name using L<Sub::Name> are also included.
1582 However, we attempt to ignore imported functions.
1584 Ultimately, we are using heuristics to determine what truly is a
1585 method in a class, and these heuristics may get the wrong answer in
1586 some edge cases. However, for most "normal" cases the heuristics work
1591 =item B<< $metaclass->get_method($method_name) >>
1593 This will return a L<Class::MOP::Method> for the specified
1594 C<$method_name>. If the class does not have the specified method, it
1597 =item B<< $metaclass->has_method($method_name) >>
1599 Returns a boolean indicating whether or not the class defines the
1600 named method. It does not include methods inherited from parent
1603 =item B<< $metaclass->get_method_list >>
1605 This will return a list of method I<names> for all methods defined in
1608 =item B<< $metaclass->add_method($method_name, $method) >>
1610 This method takes a method name and a subroutine reference, and adds
1611 the method to the class.
1613 The subroutine reference can be a L<Class::MOP::Method>, and you are
1614 strongly encouraged to pass a meta method object instead of a code
1615 reference. If you do so, that object gets stored as part of the
1616 class's method map directly. If not, the meta information will have to
1617 be recreated later, and may be incorrect.
1619 If you provide a method object, this method will clone that object if
1620 the object's package name does not match the class name. This lets us
1621 track the original source of any methods added from other classes
1622 (notably Moose roles).
1624 =item B<< $metaclass->remove_method($method_name) >>
1626 Remove the named method from the class. This method returns the
1627 L<Class::MOP::Method> object for the method.
1629 =item B<< $metaclass->method_metaclass >>
1631 Returns the class name of the method metaclass, see
1632 L<Class::MOP::Method> for more information on the method metaclass.
1634 =item B<< $metaclass->wrapped_method_metaclass >>
1636 Returns the class name of the wrapped method metaclass, see
1637 L<Class::MOP::Method::Wrapped> for more information on the wrapped
1640 =item B<< $metaclass->get_all_methods >>
1642 This will traverse the inheritance hierarchy and return a list of all
1643 the L<Class::MOP::Method> objects for this class and its parents.
1645 =item B<< $metaclass->find_method_by_name($method_name) >>
1647 This will return a L<Class::MOP::Method> for the specified
1648 C<$method_name>. If the class does not have the specified method, it
1651 Unlike C<get_method>, this method I<will> look for the named method in
1654 =item B<< $metaclass->get_all_method_names >>
1656 This will return a list of method I<names> for all of this class's
1657 methods, including inherited methods.
1659 =item B<< $metaclass->find_all_methods_by_name($method_name) >>
1661 This method looks for the named method in the class and all of its
1662 parents. It returns every matching method it finds in the inheritance
1663 tree, so it returns a list of methods.
1665 Each method is returned as a hash reference with three keys. The keys
1666 are C<name>, C<class>, and C<code>. The C<code> key has a
1667 L<Class::MOP::Method> object as its value.
1669 The list of methods is distinct.
1671 =item B<< $metaclass->find_next_method_by_name($method_name) >>
1673 This method returns the first method in any superclass matching the
1674 given name. It is effectively the method that C<SUPER::$method_name>
1679 =head2 Attribute introspection and creation
1681 Because Perl 5 does not have a core concept of attributes in classes,
1682 we can only return information about attributes which have been added
1683 via this class's methods. We cannot discover information about
1684 attributes which are defined in terms of "regular" Perl 5 methods.
1688 =item B<< $metaclass->get_attribute($attribute_name) >>
1690 This will return a L<Class::MOP::Attribute> for the specified
1691 C<$attribute_name>. If the class does not have the specified
1692 attribute, it returns C<undef>.
1694 NOTE that get_attribute does not search superclasses, for that you
1695 need to use C<find_attribute_by_name>.
1697 =item B<< $metaclass->has_attribute($attribute_name) >>
1699 Returns a boolean indicating whether or not the class defines the
1700 named attribute. It does not include attributes inherited from parent
1703 =item B<< $metaclass->get_attribute_list >>
1705 This will return a list of attributes I<names> for all attributes
1706 defined in this class.
1708 =item B<< $metaclass->get_all_attributes >>
1710 This will traverse the inheritance hierarchy and return a list of all
1711 the L<Class::MOP::Attribute> objects for this class and its parents.
1713 =item B<< $metaclass->find_attribute_by_name($attribute_name) >>
1715 This will return a L<Class::MOP::Attribute> for the specified
1716 C<$attribute_name>. If the class does not have the specified
1717 attribute, it returns C<undef>.
1719 Unlike C<get_attribute>, this attribute I<will> look for the named
1720 attribute in superclasses.
1722 =item B<< $metaclass->add_attribute(...) >>
1724 This method accepts either an existing L<Class::MOP::Attribute>
1725 object or parameters suitable for passing to that class's C<new>
1728 The attribute provided will be added to the class.
1730 Any accessor methods defined by the attribute will be added to the
1731 class when the attribute is added.
1733 If an attribute of the same name already exists, the old attribute
1734 will be removed first.
1736 =item B<< $metaclass->remove_attribute($attribute_name) >>
1738 This will remove the named attribute from the class, and
1739 L<Class::MOP::Attribute> object.
1741 Removing an attribute also removes any accessor methods defined by the
1744 However, note that removing an attribute will only affect I<future>
1745 object instances created for this class, not existing instances.
1747 =item B<< $metaclass->attribute_metaclass >>
1749 Returns the class name of the attribute metaclass for this class. By
1750 default, this is L<Class::MOP::Attribute>.
1754 =head2 Class Immutability
1756 Making a class immutable "freezes" the class definition. You can no
1757 longer call methods which alter the class, such as adding or removing
1758 methods or attributes.
1760 Making a class immutable lets us optimize the class by inlining some
1761 methods, and also allows us to optimize some methods on the metaclass
1764 After immutabilization, the metaclass object will cache most informational
1765 methods that returns information about methods or attributes. Methods which
1766 would alter the class, such as C<add_attribute> and C<add_method>, will
1767 throw an error on an immutable metaclass object.
1769 The immutabilization system in L<Moose> takes much greater advantage
1770 of the inlining features than Class::MOP itself does.
1774 =item B<< $metaclass->make_immutable(%options) >>
1776 This method will create an immutable transformer and use it to make
1777 the class and its metaclass object immutable.
1779 This method accepts the following options:
1783 =item * inline_accessors
1785 =item * inline_constructor
1787 =item * inline_destructor
1789 These are all booleans indicating whether the specified method(s)
1792 By default, accessors and the constructor are inlined, but not the
1795 =item * immutable_trait
1797 The name of a class which will be used as a parent class for the
1798 metaclass object being made immutable. This "trait" implements the
1799 post-immutability functionality of the metaclass (but not the
1800 transformation itself).
1802 This defaults to L<Class::MOP::Class::Immutable::Trait>.
1804 =item * constructor_name
1806 This is the constructor method name. This defaults to "new".
1808 =item * constructor_class
1810 The name of the method metaclass for constructors. It will be used to
1811 generate the inlined constructor. This defaults to
1812 "Class::MOP::Method::Constructor".
1814 =item * replace_constructor
1816 This is a boolean indicating whether an existing constructor should be
1817 replaced when inlining a constructor. This defaults to false.
1819 =item * destructor_class
1821 The name of the method metaclass for destructors. It will be used to
1822 generate the inlined destructor. This defaults to
1823 "Class::MOP::Method::Denstructor".
1825 =item * replace_destructor
1827 This is a boolean indicating whether an existing destructor should be
1828 replaced when inlining a destructor. This defaults to false.
1832 =item B<< $metaclass->immutable_options >>
1834 Returns a hash of the options used when making the class immutable, including
1835 both defaults and anything supplied by the user in the call to C<<
1836 $metaclass->make_immutable >>. This is useful if you need to temporarily make
1837 a class mutable and then restore immutability as it was before.
1839 =item B<< $metaclass->make_mutable >>
1841 Calling this method reverse the immutabilization transformation.
1845 =head2 Method Modifiers
1847 Method modifiers are hooks which allow a method to be wrapped with
1848 I<before>, I<after> and I<around> method modifiers. Every time a
1849 method is called, its modifiers are also called.
1851 A class can modify its own methods, as well as methods defined in
1854 =head3 How method modifiers work?
1856 Method modifiers work by wrapping the original method and then
1857 replacing it in the class's symbol table. The wrappers will handle
1858 calling all the modifiers in the appropriate order and preserving the
1859 calling context for the original method.
1861 The return values of C<before> and C<after> modifiers are
1862 ignored. This is because their purpose is B<not> to filter the input
1863 and output of the primary method (this is done with an I<around>
1866 This may seem like an odd restriction to some, but doing this allows
1867 for simple code to be added at the beginning or end of a method call
1868 without altering the function of the wrapped method or placing any
1869 extra responsibility on the code of the modifier.
1871 Of course if you have more complex needs, you can use the C<around>
1872 modifier which allows you to change both the parameters passed to the
1873 wrapped method, as well as its return value.
1875 Before and around modifiers are called in last-defined-first-called
1876 order, while after modifiers are called in first-defined-first-called
1877 order. So the call tree might looks something like this:
1889 =head3 What is the performance impact?
1891 Of course there is a performance cost associated with method
1892 modifiers, but we have made every effort to make that cost directly
1893 proportional to the number of modifier features you use.
1895 The wrapping method does its best to B<only> do as much work as it
1896 absolutely needs to. In order to do this we have moved some of the
1897 performance costs to set-up time, where they are easier to amortize.
1899 All this said, our benchmarks have indicated the following:
1901 simple wrapper with no modifiers 100% slower
1902 simple wrapper with simple before modifier 400% slower
1903 simple wrapper with simple after modifier 450% slower
1904 simple wrapper with simple around modifier 500-550% slower
1905 simple wrapper with all 3 modifiers 1100% slower
1907 These numbers may seem daunting, but you must remember, every feature
1908 comes with some cost. To put things in perspective, just doing a
1909 simple C<AUTOLOAD> which does nothing but extract the name of the
1910 method called and return it costs about 400% over a normal method
1915 =item B<< $metaclass->add_before_method_modifier($method_name, $code) >>
1917 This wraps the specified method with the supplied subroutine
1918 reference. The modifier will be called as a method itself, and will
1919 receive the same arguments as are passed to the method.
1921 When the modifier exits, the wrapped method will be called.
1923 The return value of the modifier will be ignored.
1925 =item B<< $metaclass->add_after_method_modifier($method_name, $code) >>
1927 This wraps the specified method with the supplied subroutine
1928 reference. The modifier will be called as a method itself, and will
1929 receive the same arguments as are passed to the method.
1931 When the wrapped methods exits, the modifier will be called.
1933 The return value of the modifier will be ignored.
1935 =item B<< $metaclass->add_around_method_modifier($method_name, $code) >>
1937 This wraps the specified method with the supplied subroutine
1940 The first argument passed to the modifier will be a subroutine
1941 reference to the wrapped method. The second argument is the object,
1942 and after that come any arguments passed when the method is called.
1944 The around modifier can choose to call the original method, as well as
1945 what arguments to pass if it does so.
1947 The return value of the modifier is what will be seen by the caller.
1951 =head2 Introspection
1955 =item B<< Class::MOP::Class->meta >>
1957 This will return a L<Class::MOP::Class> instance for this class.
1959 It should also be noted that L<Class::MOP> will actually bootstrap
1960 this module by installing a number of attribute meta-objects into its
1967 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1969 =head1 COPYRIGHT AND LICENSE
1971 Copyright 2006-2010 by Infinity Interactive, Inc.
1973 L<http://www.iinteractive.com>
1975 This library is free software; you can redistribute it and/or modify
1976 it under the same terms as Perl itself.