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.08';
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 my @superclasses = $self->superclasses
196 $self->_fix_metaclass_incompatibility(@superclasses);
198 my %base_metaclass = $self->_base_metaclasses;
200 # this is always okay ...
202 if ref($self) eq 'Class::MOP::Class'
204 my $meta = $self->$_;
205 !defined($meta) || $meta eq $base_metaclass{$_};
207 keys %base_metaclass;
209 for my $superclass (@superclasses) {
210 $self->_check_class_metaclass_compatibility($superclass);
213 for my $metaclass_type ( keys %base_metaclass ) {
214 next unless defined $self->$metaclass_type;
215 for my $superclass (@superclasses) {
216 $self->_check_single_metaclass_compatibility( $metaclass_type,
222 sub _class_metaclass_is_compatible {
224 my ( $superclass_name ) = @_;
226 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
229 my $super_meta_type = $super_meta->_real_ref_name;
231 return $self->isa($super_meta_type);
234 sub _check_class_metaclass_compatibility {
236 my ( $superclass_name ) = @_;
238 if (!$self->_class_metaclass_is_compatible($superclass_name)) {
239 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
241 my $super_meta_type = $super_meta->_real_ref_name;
243 confess "The metaclass of " . $self->name . " ("
244 . (ref($self)) . ")" . " is not compatible with "
245 . "the metaclass of its superclass, "
246 . $superclass_name . " (" . ($super_meta_type) . ")";
250 sub _single_metaclass_is_compatible {
252 my ( $metaclass_type, $superclass_name ) = @_;
254 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
257 # for instance, Moose::Meta::Class has a error_class attribute, but
258 # Class::MOP::Class doesn't - this shouldn't be an error
259 return 1 unless $super_meta->can($metaclass_type);
260 # for instance, Moose::Meta::Class has a destructor_class, but
261 # Class::MOP::Class doesn't - this shouldn't be an error
262 return 1 unless defined $super_meta->$metaclass_type;
263 # if metaclass is defined in superclass but not here, it's not compatible
264 # this is a really odd case
265 return 0 unless defined $self->$metaclass_type;
267 return $self->$metaclass_type->isa($super_meta->$metaclass_type);
270 sub _check_single_metaclass_compatibility {
272 my ( $metaclass_type, $superclass_name ) = @_;
274 if (!$self->_single_metaclass_is_compatible($metaclass_type, $superclass_name)) {
275 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
276 my $metaclass_type_name = $metaclass_type;
277 $metaclass_type_name =~ s/_(?:meta)?class$//;
278 $metaclass_type_name =~ s/_/ /g;
279 confess "The $metaclass_type_name metaclass for "
280 . $self->name . " (" . ($self->$metaclass_type)
281 . ")" . " is not compatible with the "
282 . "$metaclass_type_name metaclass of its "
283 . "superclass, $superclass_name ("
284 . ($super_meta->$metaclass_type) . ")";
288 sub _can_fix_class_metaclass_incompatibility_by_subclassing {
290 my ($super_meta) = @_;
292 my $super_meta_type = $super_meta->_real_ref_name;
294 return $super_meta_type ne blessed($self)
295 && $super_meta->isa(blessed($self));
298 sub _can_fix_single_metaclass_incompatibility_by_subclassing {
300 my ($metaclass_type, $super_meta) = @_;
302 my $specific_meta = $self->$metaclass_type;
303 return unless $super_meta->can($metaclass_type);
304 my $super_specific_meta = $super_meta->$metaclass_type;
306 # for instance, Moose::Meta::Class has a destructor_class, but
307 # Class::MOP::Class doesn't - this shouldn't be an error
308 return unless defined $super_specific_meta;
310 # if metaclass is defined in superclass but not here, it's fixable
311 # this is a really odd case
312 return 1 unless defined $specific_meta;
314 return $specific_meta ne $super_specific_meta
315 && $super_specific_meta->isa($specific_meta);
318 sub _can_fix_metaclass_incompatibility_by_subclassing {
320 my ($super_meta) = @_;
322 return 1 if $self->_can_fix_class_metaclass_incompatibility_by_subclassing($super_meta);
324 my %base_metaclass = $self->_base_metaclasses;
325 for my $metaclass_type (keys %base_metaclass) {
326 return 1 if $self->_can_fix_single_metaclass_incompatibility_by_subclassing($metaclass_type, $super_meta);
332 sub _can_fix_metaclass_incompatibility {
334 return $self->_can_fix_metaclass_incompatibility_by_subclassing(@_);
337 sub _fix_metaclass_incompatibility {
339 my @supers = map { Class::MOP::Class->initialize($_) } @_;
342 for my $super (@supers) {
344 if $self->_can_fix_metaclass_incompatibility($super);
346 return unless $necessary;
348 for my $super (@supers) {
349 if (!$self->_class_metaclass_is_compatible($super->name)) {
350 $self->_fix_class_metaclass_incompatibility($super);
354 my %base_metaclass = $self->_base_metaclasses;
355 for my $metaclass_type (keys %base_metaclass) {
356 for my $super (@supers) {
357 if (!$self->_single_metaclass_is_compatible($metaclass_type, $super->name)) {
358 $self->_fix_single_metaclass_incompatibility(
359 $metaclass_type, $super
366 sub _fix_class_metaclass_incompatibility {
368 my ( $super_meta ) = @_;
370 if ($self->_can_fix_class_metaclass_incompatibility_by_subclassing($super_meta)) {
372 || confess "Can't fix metaclass incompatibility for "
374 . " because it is not pristine.";
376 my $super_meta_name = $super_meta->_real_ref_name;
378 $super_meta_name->meta->rebless_instance($self);
382 sub _fix_single_metaclass_incompatibility {
384 my ( $metaclass_type, $super_meta ) = @_;
386 if ($self->_can_fix_single_metaclass_incompatibility_by_subclassing($metaclass_type, $super_meta)) {
388 || confess "Can't fix metaclass incompatibility for "
390 . " because it is not pristine.";
392 $self->{$metaclass_type} = $super_meta->$metaclass_type;
400 # this should be sufficient, if you have a
401 # use case where it is not, write a test and
403 my $ANON_CLASS_SERIAL = 0;
406 # we need a sufficiently annoying prefix
407 # this should suffice for now, this is
408 # used in a couple of places below, so
409 # need to put it up here for now.
410 my $ANON_CLASS_PREFIX = 'Class::MOP::Class::__ANON__::SERIAL::';
414 no warnings 'uninitialized';
415 $self->name =~ /^$ANON_CLASS_PREFIX/o;
418 sub create_anon_class {
419 my ($class, %options) = @_;
420 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
421 return $class->create($package_name, %options);
425 # this will only get called for
426 # anon-classes, all other calls
427 # are assumed to occur during
428 # global destruction and so don't
429 # really need to be handled explicitly
433 return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated
435 no warnings 'uninitialized';
436 my $name = $self->name;
437 return unless $name =~ /^$ANON_CLASS_PREFIX/o;
439 # Moose does a weird thing where it replaces the metaclass for
440 # class when fixing metaclass incompatibility. In that case,
441 # we don't want to clean out the namespace now. We can detect
442 # that because Moose will explicitly update the singleton
443 # cache in Class::MOP.
444 my $current_meta = Class::MOP::get_metaclass_by_name($name);
445 return if $current_meta ne $self;
447 my ($serial_id) = ($name =~ /^$ANON_CLASS_PREFIX(\d+)/o);
449 @{$name . '::ISA'} = ();
450 %{$name . '::'} = ();
451 delete ${$ANON_CLASS_PREFIX}{$serial_id . '::'};
453 Class::MOP::remove_metaclass_by_name($name);
458 # creating classes with MOP ...
461 my ( $class, @args ) = @_;
463 unshift @args, 'package' if @args % 2 == 1;
465 my (%options) = @args;
466 my $package_name = $options{package};
468 (ref $options{superclasses} eq 'ARRAY')
469 || confess "You must pass an ARRAY ref of superclasses"
470 if exists $options{superclasses};
472 (ref $options{attributes} eq 'ARRAY')
473 || confess "You must pass an ARRAY ref of attributes"
474 if exists $options{attributes};
476 (ref $options{methods} eq 'HASH')
477 || confess "You must pass a HASH ref of methods"
478 if exists $options{methods};
480 my (%initialize_options) = @args;
481 delete @initialize_options{qw(
490 my $meta = $class->initialize( $package_name => %initialize_options );
492 $meta->_instantiate_module( $options{version}, $options{authority} );
495 $meta->add_method('meta' => sub {
496 if (Class::MOP::DEBUG_NO_META()) {
498 if (my $meta = try { $self->SUPER::meta }) {
499 return $meta if $meta->isa('Class::MOP::Class');
501 confess "'meta' method called by MOP internals"
502 if caller =~ /Class::MOP|metaclass/;
504 $class->initialize(ref($_[0]) || $_[0]);
505 }) unless $options{no_meta};
507 $meta->superclasses(@{$options{superclasses}})
508 if exists $options{superclasses};
510 # process attributes first, so that they can
511 # install accessors, but locally defined methods
512 # can then overwrite them. It is maybe a little odd, but
513 # I think this should be the order of things.
514 if (exists $options{attributes}) {
515 foreach my $attr (@{$options{attributes}}) {
516 $meta->add_attribute($attr);
519 if (exists $options{methods}) {
520 foreach my $method_name (keys %{$options{methods}}) {
521 $meta->add_method($method_name, $options{methods}->{$method_name});
530 # all these attribute readers will be bootstrapped
531 # away in the Class::MOP bootstrap section
533 sub instance_metaclass { $_[0]->{'instance_metaclass'} }
534 sub immutable_trait { $_[0]->{'immutable_trait'} }
535 sub constructor_class { $_[0]->{'constructor_class'} }
536 sub constructor_name { $_[0]->{'constructor_name'} }
537 sub destructor_class { $_[0]->{'destructor_class'} }
539 # Instance Construction & Cloning
545 # we need to protect the integrity of the
546 # Class::MOP::Class singletons here, so we
547 # delegate this to &construct_class_instance
548 # which will deal with the singletons
549 return $class->_construct_class_instance(@_)
550 if $class->name->isa('Class::MOP::Class');
551 return $class->_construct_instance(@_);
554 sub _construct_instance {
556 my $params = @_ == 1 ? $_[0] : {@_};
557 my $meta_instance = $class->get_meta_instance();
559 # the code below is almost certainly incorrect
560 # but this is foreign inheritance, so we might
561 # have to kludge it in the end.
563 if (my $instance_class = blessed($params->{__INSTANCE__})) {
564 ($instance_class eq $class->name)
565 || confess "Objects passed as the __INSTANCE__ parameter must "
566 . "already be blessed into the correct class, but "
567 . "$params->{__INSTANCE__} is not a " . $class->name;
568 $instance = $params->{__INSTANCE__};
570 elsif (exists $params->{__INSTANCE__}) {
571 confess "The __INSTANCE__ parameter must be a blessed reference, not "
572 . $params->{__INSTANCE__};
575 $instance = $meta_instance->create_instance();
577 foreach my $attr ($class->get_all_attributes()) {
578 $attr->initialize_instance_slot($meta_instance, $instance, $params);
581 # this will only work for a HASH instance type
582 if ($class->is_anon_class) {
583 (reftype($instance) eq 'HASH')
584 || confess "Currently only HASH based instances are supported with instance of anon-classes";
586 # At some point we should make this official
587 # as a reserved slot name, but right now I am
588 # going to keep it here.
589 # my $RESERVED_MOP_SLOT = '__MOP__';
590 $instance->{'__MOP__'} = $class;
596 sub get_meta_instance {
598 $self->{'_meta_instance'} ||= $self->_create_meta_instance();
601 sub _create_meta_instance {
604 my $instance = $self->instance_metaclass->new(
605 associated_metaclass => $self,
606 attributes => [ $self->get_all_attributes() ],
609 $self->add_meta_instance_dependencies()
610 if $instance->is_dependent_on_superclasses();
617 my $instance = shift;
618 (blessed($instance) && $instance->isa($class->name))
619 || confess "You must pass an instance of the metaclass (" . (ref $class ? $class->name : $class) . "), not ($instance)";
622 # we need to protect the integrity of the
623 # Class::MOP::Class singletons here, they
624 # should not be cloned.
625 return $instance if $instance->isa('Class::MOP::Class');
626 $class->_clone_instance($instance, @_);
629 sub _clone_instance {
630 my ($class, $instance, %params) = @_;
632 || confess "You can only clone instances, ($instance) is not a blessed instance";
633 my $meta_instance = $class->get_meta_instance();
634 my $clone = $meta_instance->clone_instance($instance);
635 foreach my $attr ($class->get_all_attributes()) {
636 if ( defined( my $init_arg = $attr->init_arg ) ) {
637 if (exists $params{$init_arg}) {
638 $attr->set_value($clone, $params{$init_arg});
645 sub rebless_instance {
646 my ($self, $instance, %params) = @_;
648 my $old_metaclass = Class::MOP::class_of($instance);
650 my $old_class = $old_metaclass ? $old_metaclass->name : blessed($instance);
651 $self->name->isa($old_class)
652 || confess "You may rebless only into a subclass of ($old_class), of which (". $self->name .") isn't.";
654 $old_metaclass->rebless_instance_away($instance, $self, %params)
657 my $meta_instance = $self->get_meta_instance();
660 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
661 $meta_instance->rebless_instance_structure($_[1], $self);
663 foreach my $attr ( $self->get_all_attributes ) {
664 if ( $attr->has_value($instance) ) {
665 if ( defined( my $init_arg = $attr->init_arg ) ) {
666 $params{$init_arg} = $attr->get_value($instance)
667 unless exists $params{$init_arg};
670 $attr->set_value($instance, $attr->get_value($instance));
675 foreach my $attr ($self->get_all_attributes) {
676 $attr->initialize_instance_slot($meta_instance, $instance, \%params);
682 sub rebless_instance_back {
683 my ( $self, $instance ) = @_;
685 my $old_metaclass = Class::MOP::class_of($instance);
688 = $old_metaclass ? $old_metaclass->name : blessed($instance);
689 $old_class->isa( $self->name )
691 "You may rebless only into a superclass of ($old_class), of which ("
695 $old_metaclass->rebless_instance_away( $instance, $self )
698 my $meta_instance = $self->get_meta_instance;
700 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
701 $meta_instance->rebless_instance_structure( $_[1], $self );
703 for my $attr ( $old_metaclass->get_all_attributes ) {
704 next if $self->has_attribute( $attr->name );
705 $meta_instance->deinitialize_slot( $instance, $_ ) for $attr->slots;
711 sub rebless_instance_away {
712 # this intentionally does nothing, it is just a hook
715 sub _attach_attribute {
716 my ($self, $attribute) = @_;
717 $attribute->attach_to_class($self);
720 sub _post_add_attribute {
721 my ( $self, $attribute ) = @_;
723 $self->invalidate_meta_instances;
725 # invalidate package flag here
728 $attribute->install_accessors;
731 $self->remove_attribute( $attribute->name );
736 sub remove_attribute {
739 my $removed_attribute = $self->SUPER::remove_attribute(@_)
742 $self->invalidate_meta_instances;
744 $removed_attribute->remove_accessors;
745 $removed_attribute->detach_from_class;
747 return$removed_attribute;
750 sub find_attribute_by_name {
751 my ( $self, $attr_name ) = @_;
753 foreach my $class ( $self->linearized_isa ) {
754 # fetch the meta-class ...
755 my $meta = Class::MOP::Class->initialize($class);
756 return $meta->get_attribute($attr_name)
757 if $meta->has_attribute($attr_name);
763 sub get_all_attributes {
765 my %attrs = map { %{ Class::MOP::Class->initialize($_)->_attribute_map } }
766 reverse $self->linearized_isa;
767 return values %attrs;
775 my $isa = $self->get_or_add_package_symbol(
776 { sigil => '@', type => 'ARRAY', name => 'ISA' } );
783 # on 5.8 and below, we need to call
784 # a method to get Perl to detect
785 # a cycle in the class hierarchy
786 my $class = $self->name;
790 # we need to check the metaclass
791 # compatibility here so that we can
792 # be sure that the superclass is
793 # not potentially creating an issues
794 # we don't know about
796 $self->_check_metaclass_compatibility();
797 $self->_superclasses_updated();
803 sub _superclasses_updated {
805 $self->update_meta_instance_dependencies();
810 my $super_class = $self->name;
812 return @{ $super_class->mro::get_isarev() };
815 sub direct_subclasses {
817 my $super_class = $self->name;
822 } Class::MOP::Class->initialize($_)->superclasses
827 return @{ mro::get_linear_isa( (shift)->name ) };
830 sub class_precedence_list {
832 my $name = $self->name;
834 unless (Class::MOP::IS_RUNNING_ON_5_10()) {
836 # We need to check for circular inheritance here
837 # if we are are not on 5.10, cause 5.8 detects it
838 # late. This will do nothing if all is well, and
839 # blow up otherwise. Yes, it's an ugly hack, better
840 # suggestions are welcome.
842 ($name || return)->isa('This is a test for circular inheritance')
845 # if our mro is c3, we can
846 # just grab the linear_isa
847 if (mro::get_mro($name) eq 'c3') {
848 return @{ mro::get_linear_isa($name) }
852 # we can't grab the linear_isa for dfs
853 # since it has all the duplicates
858 Class::MOP::Class->initialize($_)->class_precedence_list()
859 } $self->superclasses()
867 my $fetch_and_prepare_method = sub {
868 my ($self, $method_name) = @_;
869 my $wrapped_metaclass = $self->wrapped_method_metaclass;
871 my $method = $self->get_method($method_name);
872 # if we dont have local ...
874 # try to find the next method
875 $method = $self->find_next_method_by_name($method_name);
876 # die if it does not exist
878 || confess "The method '$method_name' was not found in the inheritance hierarchy for " . $self->name;
879 # and now make sure to wrap it
880 # even if it is already wrapped
881 # because we need a new sub ref
882 $method = $wrapped_metaclass->wrap($method,
883 package_name => $self->name,
884 name => $method_name,
888 # now make sure we wrap it properly
889 $method = $wrapped_metaclass->wrap($method,
890 package_name => $self->name,
891 name => $method_name,
892 ) unless $method->isa($wrapped_metaclass);
894 $self->add_method($method_name => $method);
898 sub add_before_method_modifier {
899 my ($self, $method_name, $method_modifier) = @_;
900 (defined $method_name && length $method_name)
901 || confess "You must pass in a method name";
902 my $method = $fetch_and_prepare_method->($self, $method_name);
903 $method->add_before_modifier(
904 subname(':before' => $method_modifier)
908 sub add_after_method_modifier {
909 my ($self, $method_name, $method_modifier) = @_;
910 (defined $method_name && length $method_name)
911 || confess "You must pass in a method name";
912 my $method = $fetch_and_prepare_method->($self, $method_name);
913 $method->add_after_modifier(
914 subname(':after' => $method_modifier)
918 sub add_around_method_modifier {
919 my ($self, $method_name, $method_modifier) = @_;
920 (defined $method_name && length $method_name)
921 || confess "You must pass in a method name";
922 my $method = $fetch_and_prepare_method->($self, $method_name);
923 $method->add_around_modifier(
924 subname(':around' => $method_modifier)
929 # the methods above used to be named like this:
930 # ${pkg}::${method}:(before|after|around)
931 # but this proved problematic when using one modifier
932 # to wrap multiple methods (something which is likely
933 # to happen pretty regularly IMO). So instead of naming
934 # it like this, I have chosen to just name them purely
935 # with their modifier names, like so:
936 # :(before|after|around)
937 # The fact is that in a stack trace, it will be fairly
938 # evident from the context what method they are attached
939 # to, and so don't need the fully qualified name.
942 sub find_method_by_name {
943 my ($self, $method_name) = @_;
944 (defined $method_name && length $method_name)
945 || confess "You must define a method name to find";
946 foreach my $class ($self->linearized_isa) {
947 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
948 return $method if defined $method;
953 sub get_all_methods {
957 for my $class ( reverse $self->linearized_isa ) {
958 my $meta = Class::MOP::Class->initialize($class);
960 $methods{ $_->name } = $_ for $meta->_get_local_methods;
963 return values %methods;
966 sub get_all_method_names {
969 return grep { !$uniq{$_}++ } map { Class::MOP::Class->initialize($_)->get_method_list } $self->linearized_isa;
972 sub find_all_methods_by_name {
973 my ($self, $method_name) = @_;
974 (defined $method_name && length $method_name)
975 || confess "You must define a method name to find";
977 foreach my $class ($self->linearized_isa) {
978 # fetch the meta-class ...
979 my $meta = Class::MOP::Class->initialize($class);
981 name => $method_name,
983 code => $meta->get_method($method_name)
984 } if $meta->has_method($method_name);
989 sub find_next_method_by_name {
990 my ($self, $method_name) = @_;
991 (defined $method_name && length $method_name)
992 || confess "You must define a method name to find";
993 my @cpl = $self->linearized_isa;
994 shift @cpl; # discard ourselves
995 foreach my $class (@cpl) {
996 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
997 return $method if defined $method;
1002 sub update_meta_instance_dependencies {
1005 if ( $self->{meta_instance_dependencies} ) {
1006 return $self->add_meta_instance_dependencies;
1010 sub add_meta_instance_dependencies {
1013 $self->remove_meta_instance_dependencies;
1015 my @attrs = $self->get_all_attributes();
1018 my @classes = grep { not $seen{ $_->name }++ }
1019 map { $_->associated_class } @attrs;
1021 foreach my $class (@classes) {
1022 $class->add_dependent_meta_instance($self);
1025 $self->{meta_instance_dependencies} = \@classes;
1028 sub remove_meta_instance_dependencies {
1031 if ( my $classes = delete $self->{meta_instance_dependencies} ) {
1032 foreach my $class (@$classes) {
1033 $class->remove_dependent_meta_instance($self);
1043 sub add_dependent_meta_instance {
1044 my ( $self, $metaclass ) = @_;
1045 push @{ $self->{dependent_meta_instances} }, $metaclass;
1048 sub remove_dependent_meta_instance {
1049 my ( $self, $metaclass ) = @_;
1050 my $name = $metaclass->name;
1051 @$_ = grep { $_->name ne $name } @$_
1052 for $self->{dependent_meta_instances};
1055 sub invalidate_meta_instances {
1057 $_->invalidate_meta_instance()
1058 for $self, @{ $self->{dependent_meta_instances} };
1061 sub invalidate_meta_instance {
1063 undef $self->{_meta_instance};
1066 # check if we can reinitialize
1070 # if any local attr is defined
1071 return if $self->get_attribute_list;
1073 # or any non-declared methods
1074 for my $method ( map { $self->get_method($_) } $self->get_method_list ) {
1075 return if $method->isa("Class::MOP::Method::Generated");
1076 # FIXME do we need to enforce this too? return unless $method->isa( $self->method_metaclass );
1084 sub is_mutable { 1 }
1085 sub is_immutable { 0 }
1087 sub immutable_options { %{ $_[0]{__immutable}{options} || {} } }
1089 sub _immutable_options {
1090 my ( $self, @args ) = @_;
1093 inline_accessors => 1,
1094 inline_constructor => 1,
1095 inline_destructor => 0,
1097 immutable_trait => $self->immutable_trait,
1098 constructor_name => $self->constructor_name,
1099 constructor_class => $self->constructor_class,
1100 destructor_class => $self->destructor_class,
1105 sub make_immutable {
1106 my ( $self, @args ) = @_;
1108 if ( $self->is_mutable ) {
1109 $self->_initialize_immutable( $self->_immutable_options(@args) );
1110 $self->_rebless_as_immutable(@args);
1121 if ( $self->is_immutable ) {
1122 my @args = $self->immutable_options;
1123 $self->_rebless_as_mutable();
1124 $self->_remove_inlined_code(@args);
1125 delete $self->{__immutable};
1133 sub _rebless_as_immutable {
1134 my ( $self, @args ) = @_;
1136 $self->{__immutable}{original_class} = ref $self;
1138 bless $self => $self->_immutable_metaclass(@args);
1141 sub _immutable_metaclass {
1142 my ( $self, %args ) = @_;
1144 if ( my $class = $args{immutable_metaclass} ) {
1148 my $trait = $args{immutable_trait} = $self->immutable_trait
1149 || confess "no immutable trait specified for $self";
1151 my $meta = $self->meta;
1152 my $meta_attr = $meta->find_attribute_by_name("immutable_trait");
1156 if ( $meta_attr and $trait eq $meta_attr->default ) {
1157 # if the trait is the same as the default we try and pick a
1158 # predictable name for the immutable metaclass
1159 $class_name = 'Class::MOP::Class::Immutable::' . ref($self);
1162 $class_name = join '::', 'Class::MOP::Class::Immutable::CustomTrait',
1163 $trait, 'ForMetaClass', ref($self);
1167 if Class::MOP::is_class_loaded($class_name);
1169 # If the metaclass is a subclass of CMOP::Class which has had
1170 # metaclass roles applied (via Moose), then we want to make sure
1171 # that we preserve that anonymous class (see Fey::ORM for an
1172 # example of where this matters).
1173 my $meta_name = $meta->_real_ref_name;
1175 my $immutable_meta = $meta_name->create(
1177 superclasses => [ ref $self ],
1180 Class::MOP::load_class($trait);
1181 for my $meth ( Class::MOP::Class->initialize($trait)->get_all_methods ) {
1182 my $meth_name = $meth->name;
1184 if ( $immutable_meta->find_method_by_name( $meth_name ) ) {
1185 $immutable_meta->add_around_method_modifier( $meth_name, $meth->body );
1188 $immutable_meta->add_method( $meth_name, $meth->clone );
1192 $immutable_meta->make_immutable(
1193 inline_constructor => 0,
1194 inline_accessors => 0,
1200 sub _remove_inlined_code {
1203 $self->remove_method( $_->name ) for $self->_inlined_methods;
1205 delete $self->{__immutable}{inlined_methods};
1208 sub _inlined_methods { @{ $_[0]{__immutable}{inlined_methods} || [] } }
1210 sub _add_inlined_method {
1211 my ( $self, $method ) = @_;
1213 push @{ $self->{__immutable}{inlined_methods} ||= [] }, $method;
1216 sub _initialize_immutable {
1217 my ( $self, %args ) = @_;
1219 $self->{__immutable}{options} = \%args;
1220 $self->_install_inlined_code(%args);
1223 sub _install_inlined_code {
1224 my ( $self, %args ) = @_;
1227 $self->_inline_accessors(%args) if $args{inline_accessors};
1228 $self->_inline_constructor(%args) if $args{inline_constructor};
1229 $self->_inline_destructor(%args) if $args{inline_destructor};
1232 sub _rebless_as_mutable {
1235 bless $self, $self->_get_mutable_metaclass_name;
1240 sub _inline_accessors {
1243 foreach my $attr_name ( $self->get_attribute_list ) {
1244 $self->get_attribute($attr_name)->install_accessors(1);
1248 sub _inline_constructor {
1249 my ( $self, %args ) = @_;
1251 my $name = $args{constructor_name};
1252 # A class may not even have a constructor, and that's okay.
1253 return unless defined $name;
1255 if ( $self->has_method($name) && !$args{replace_constructor} ) {
1256 my $class = $self->name;
1257 warn "Not inlining a constructor for $class since it defines"
1258 . " its own constructor.\n"
1259 . "If you are certain you don't need to inline your"
1260 . " constructor, specify inline_constructor => 0 in your"
1261 . " call to $class->meta->make_immutable\n";
1265 my $constructor_class = $args{constructor_class};
1267 Class::MOP::load_class($constructor_class);
1269 my $constructor = $constructor_class->new(
1273 package_name => $self->name,
1277 if ( $args{replace_constructor} or $constructor->can_be_inlined ) {
1278 $self->add_method( $name => $constructor );
1279 $self->_add_inlined_method($constructor);
1283 sub _inline_destructor {
1284 my ( $self, %args ) = @_;
1286 ( exists $args{destructor_class} && defined $args{destructor_class} )
1287 || confess "The 'inline_destructor' option is present, but "
1288 . "no destructor class was specified";
1290 if ( $self->has_method('DESTROY') && ! $args{replace_destructor} ) {
1291 my $class = $self->name;
1292 warn "Not inlining a destructor for $class since it defines"
1293 . " its own destructor.\n";
1297 my $destructor_class = $args{destructor_class};
1299 Class::MOP::load_class($destructor_class);
1301 return unless $destructor_class->is_needed($self);
1303 my $destructor = $destructor_class->new(
1306 package_name => $self->name,
1310 if ( $args{replace_destructor} or $destructor->can_be_inlined ) {
1311 $self->add_method( 'DESTROY' => $destructor );
1312 $self->_add_inlined_method($destructor);
1324 Class::MOP::Class - Class Meta Object
1328 # assuming that class Foo
1329 # has been defined, you can
1331 # use this for introspection ...
1333 # add a method to Foo ...
1334 Foo->meta->add_method( 'bar' => sub {...} )
1336 # get a list of all the classes searched
1337 # the method dispatcher in the correct order
1338 Foo->meta->class_precedence_list()
1340 # remove a method from Foo
1341 Foo->meta->remove_method('bar');
1343 # or use this to actually create classes ...
1345 Class::MOP::Class->create(
1348 superclasses => ['Foo'],
1350 Class::MOP::Attribute->new('$bar'),
1351 Class::MOP::Attribute->new('$baz'),
1354 calculate_bar => sub {...},
1355 construct_baz => sub {...}
1362 The Class Protocol is the largest and most complex part of the
1363 Class::MOP meta-object protocol. It controls the introspection and
1364 manipulation of Perl 5 classes, and it can create them as well. The
1365 best way to understand what this module can do is to read the
1366 documentation for each of its methods.
1370 C<Class::MOP::Class> is a subclass of L<Class::MOP::Module>.
1374 =head2 Class construction
1376 These methods all create new C<Class::MOP::Class> objects. These
1377 objects can represent existing classes or they can be used to create
1378 new classes from scratch.
1380 The metaclass object for a given class is a singleton. If you attempt
1381 to create a metaclass for the same class twice, you will just get the
1386 =item B<< Class::MOP::Class->create($package_name, %options) >>
1388 This method creates a new C<Class::MOP::Class> object with the given
1389 package name. It accepts a number of options:
1395 An optional version number for the newly created package.
1399 An optional authority for the newly created package.
1401 =item * superclasses
1403 An optional array reference of superclass names.
1407 An optional hash reference of methods for the class. The keys of the
1408 hash reference are method names and values are subroutine references.
1412 An optional array reference of L<Class::MOP::Attribute> objects.
1416 If true, a C<meta> method will not be installed into the class.
1420 =item B<< Class::MOP::Class->create_anon_class(%options) >>
1422 This method works just like C<< Class::MOP::Class->create >> but it
1423 creates an "anonymous" class. In fact, the class does have a name, but
1424 that name is a unique name generated internally by this module.
1426 It accepts the same C<superclasses>, C<methods>, and C<attributes>
1427 parameters that C<create> accepts.
1429 Anonymous classes are destroyed once the metaclass they are attached
1430 to goes out of scope, and will be removed from Perl's internal symbol
1433 All instances of an anonymous class keep a special reference to the
1434 metaclass object, which prevents the metaclass from going out of scope
1435 while any instances exist.
1437 This only works if the instance is based on a hash reference, however.
1439 =item B<< Class::MOP::Class->initialize($package_name, %options) >>
1441 This method will initialize a C<Class::MOP::Class> object for the
1442 named package. Unlike C<create>, this method I<will not> create a new
1445 The purpose of this method is to retrieve a C<Class::MOP::Class>
1446 object for introspecting an existing class.
1448 If an existing C<Class::MOP::Class> object exists for the named
1449 package, it will be returned, and any options provided will be
1452 If the object does not yet exist, it will be created.
1454 The valid options that can be passed to this method are
1455 C<attribute_metaclass>, C<method_metaclass>,
1456 C<wrapped_method_metaclass>, and C<instance_metaclass>. These are all
1457 optional, and default to the appropriate class in the C<Class::MOP>
1462 =head2 Object instance construction and cloning
1464 These methods are all related to creating and/or cloning object
1469 =item B<< $metaclass->clone_object($instance, %params) >>
1471 This method clones an existing object instance. Any parameters you
1472 provide are will override existing attribute values in the object.
1474 This is a convenience method for cloning an object instance, then
1475 blessing it into the appropriate package.
1477 You could implement a clone method in your class, using this method:
1480 my ($self, %params) = @_;
1481 $self->meta->clone_object($self, %params);
1484 =item B<< $metaclass->rebless_instance($instance, %params) >>
1486 This method changes the class of C<$instance> to the metaclass's class.
1488 You can only rebless an instance into a subclass of its current
1489 class. If you pass any additional parameters, these will be treated
1490 like constructor parameters and used to initialize the object's
1491 attributes. Any existing attributes that are already set will be
1494 Before reblessing the instance, this method will call
1495 C<rebless_instance_away> on the instance's current metaclass. This method
1496 will be passed the instance, the new metaclass, and any parameters
1497 specified to C<rebless_instance>. By default, C<rebless_instance_away>
1498 does nothing; it is merely a hook.
1500 =item B<< $metaclass->rebless_instance_back($instance) >>
1502 Does the same thing as C<rebless_instance>, except that you can only
1503 rebless an instance into one of its superclasses. Any attributes that
1504 do not exist in the superclass will be deinitialized.
1506 This is a much more dangerous operation than C<rebless_instance>,
1507 especially when multiple inheritance is involved, so use this carefully!
1509 =item B<< $metaclass->new_object(%params) >>
1511 This method is used to create a new object of the metaclass's
1512 class. Any parameters you provide are used to initialize the
1513 instance's attributes. A special C<__INSTANCE__> key can be passed to
1514 provide an already generated instance, rather than having Class::MOP
1515 generate it for you. This is mostly useful for using Class::MOP with
1516 foreign classes which generate instances using their own constructors.
1518 =item B<< $metaclass->instance_metaclass >>
1520 Returns the class name of the instance metaclass. See
1521 L<Class::MOP::Instance> for more information on the instance
1524 =item B<< $metaclass->get_meta_instance >>
1526 Returns an instance of the C<instance_metaclass> to be used in the
1527 construction of a new instance of the class.
1531 =head2 Informational predicates
1533 These are a few predicate methods for asking information about the
1538 =item B<< $metaclass->is_anon_class >>
1540 This returns true if the class was created by calling C<<
1541 Class::MOP::Class->create_anon_class >>.
1543 =item B<< $metaclass->is_mutable >>
1545 This returns true if the class is still mutable.
1547 =item B<< $metaclass->is_immutable >>
1549 This returns true if the class has been made immutable.
1551 =item B<< $metaclass->is_pristine >>
1553 A class is I<not> pristine if it has non-inherited attributes or if it
1554 has any generated methods.
1558 =head2 Inheritance Relationships
1562 =item B<< $metaclass->superclasses(@superclasses) >>
1564 This is a read-write accessor which represents the superclass
1565 relationships of the metaclass's class.
1567 This is basically sugar around getting and setting C<@ISA>.
1569 =item B<< $metaclass->class_precedence_list >>
1571 This returns a list of all of the class's ancestor classes. The
1572 classes are returned in method dispatch order.
1574 =item B<< $metaclass->linearized_isa >>
1576 This returns a list based on C<class_precedence_list> but with all
1579 =item B<< $metaclass->subclasses >>
1581 This returns a list of all subclasses for this class, even indirect
1584 =item B<< $metaclass->direct_subclasses >>
1586 This returns a list of immediate subclasses for this class, which does not
1587 include indirect subclasses.
1591 =head2 Method introspection and creation
1593 These methods allow you to introspect a class's methods, as well as
1594 add, remove, or change methods.
1596 Determining what is truly a method in a Perl 5 class requires some
1597 heuristics (aka guessing).
1599 Methods defined outside the package with a fully qualified name (C<sub
1600 Package::name { ... }>) will be included. Similarly, methods named
1601 with a fully qualified name using L<Sub::Name> are also included.
1603 However, we attempt to ignore imported functions.
1605 Ultimately, we are using heuristics to determine what truly is a
1606 method in a class, and these heuristics may get the wrong answer in
1607 some edge cases. However, for most "normal" cases the heuristics work
1612 =item B<< $metaclass->get_method($method_name) >>
1614 This will return a L<Class::MOP::Method> for the specified
1615 C<$method_name>. If the class does not have the specified method, it
1618 =item B<< $metaclass->has_method($method_name) >>
1620 Returns a boolean indicating whether or not the class defines the
1621 named method. It does not include methods inherited from parent
1624 =item B<< $metaclass->get_method_list >>
1626 This will return a list of method I<names> for all methods defined in
1629 =item B<< $metaclass->add_method($method_name, $method) >>
1631 This method takes a method name and a subroutine reference, and adds
1632 the method to the class.
1634 The subroutine reference can be a L<Class::MOP::Method>, and you are
1635 strongly encouraged to pass a meta method object instead of a code
1636 reference. If you do so, that object gets stored as part of the
1637 class's method map directly. If not, the meta information will have to
1638 be recreated later, and may be incorrect.
1640 If you provide a method object, this method will clone that object if
1641 the object's package name does not match the class name. This lets us
1642 track the original source of any methods added from other classes
1643 (notably Moose roles).
1645 =item B<< $metaclass->remove_method($method_name) >>
1647 Remove the named method from the class. This method returns the
1648 L<Class::MOP::Method> object for the method.
1650 =item B<< $metaclass->method_metaclass >>
1652 Returns the class name of the method metaclass, see
1653 L<Class::MOP::Method> for more information on the method metaclass.
1655 =item B<< $metaclass->wrapped_method_metaclass >>
1657 Returns the class name of the wrapped method metaclass, see
1658 L<Class::MOP::Method::Wrapped> for more information on the wrapped
1661 =item B<< $metaclass->get_all_methods >>
1663 This will traverse the inheritance hierarchy and return a list of all
1664 the L<Class::MOP::Method> objects for this class and its parents.
1666 =item B<< $metaclass->find_method_by_name($method_name) >>
1668 This will return a L<Class::MOP::Method> for the specified
1669 C<$method_name>. If the class does not have the specified method, it
1672 Unlike C<get_method>, this method I<will> look for the named method in
1675 =item B<< $metaclass->get_all_method_names >>
1677 This will return a list of method I<names> for all of this class's
1678 methods, including inherited methods.
1680 =item B<< $metaclass->find_all_methods_by_name($method_name) >>
1682 This method looks for the named method in the class and all of its
1683 parents. It returns every matching method it finds in the inheritance
1684 tree, so it returns a list of methods.
1686 Each method is returned as a hash reference with three keys. The keys
1687 are C<name>, C<class>, and C<code>. The C<code> key has a
1688 L<Class::MOP::Method> object as its value.
1690 The list of methods is distinct.
1692 =item B<< $metaclass->find_next_method_by_name($method_name) >>
1694 This method returns the first method in any superclass matching the
1695 given name. It is effectively the method that C<SUPER::$method_name>
1700 =head2 Attribute introspection and creation
1702 Because Perl 5 does not have a core concept of attributes in classes,
1703 we can only return information about attributes which have been added
1704 via this class's methods. We cannot discover information about
1705 attributes which are defined in terms of "regular" Perl 5 methods.
1709 =item B<< $metaclass->get_attribute($attribute_name) >>
1711 This will return a L<Class::MOP::Attribute> for the specified
1712 C<$attribute_name>. If the class does not have the specified
1713 attribute, it returns C<undef>.
1715 NOTE that get_attribute does not search superclasses, for that you
1716 need to use C<find_attribute_by_name>.
1718 =item B<< $metaclass->has_attribute($attribute_name) >>
1720 Returns a boolean indicating whether or not the class defines the
1721 named attribute. It does not include attributes inherited from parent
1724 =item B<< $metaclass->get_attribute_list >>
1726 This will return a list of attributes I<names> for all attributes
1727 defined in this class. Note that this operates on the current class
1728 only, it does not traverse the inheritance hierarchy.
1730 =item B<< $metaclass->get_all_attributes >>
1732 This will traverse the inheritance hierarchy and return a list of all
1733 the L<Class::MOP::Attribute> objects for this class and its parents.
1735 =item B<< $metaclass->find_attribute_by_name($attribute_name) >>
1737 This will return a L<Class::MOP::Attribute> for the specified
1738 C<$attribute_name>. If the class does not have the specified
1739 attribute, it returns C<undef>.
1741 Unlike C<get_attribute>, this attribute I<will> look for the named
1742 attribute in superclasses.
1744 =item B<< $metaclass->add_attribute(...) >>
1746 This method accepts either an existing L<Class::MOP::Attribute>
1747 object or parameters suitable for passing to that class's C<new>
1750 The attribute provided will be added to the class.
1752 Any accessor methods defined by the attribute will be added to the
1753 class when the attribute is added.
1755 If an attribute of the same name already exists, the old attribute
1756 will be removed first.
1758 =item B<< $metaclass->remove_attribute($attribute_name) >>
1760 This will remove the named attribute from the class, and
1761 L<Class::MOP::Attribute> object.
1763 Removing an attribute also removes any accessor methods defined by the
1766 However, note that removing an attribute will only affect I<future>
1767 object instances created for this class, not existing instances.
1769 =item B<< $metaclass->attribute_metaclass >>
1771 Returns the class name of the attribute metaclass for this class. By
1772 default, this is L<Class::MOP::Attribute>.
1776 =head2 Class Immutability
1778 Making a class immutable "freezes" the class definition. You can no
1779 longer call methods which alter the class, such as adding or removing
1780 methods or attributes.
1782 Making a class immutable lets us optimize the class by inlining some
1783 methods, and also allows us to optimize some methods on the metaclass
1786 After immutabilization, the metaclass object will cache most informational
1787 methods that returns information about methods or attributes. Methods which
1788 would alter the class, such as C<add_attribute> and C<add_method>, will
1789 throw an error on an immutable metaclass object.
1791 The immutabilization system in L<Moose> takes much greater advantage
1792 of the inlining features than Class::MOP itself does.
1796 =item B<< $metaclass->make_immutable(%options) >>
1798 This method will create an immutable transformer and use it to make
1799 the class and its metaclass object immutable.
1801 This method accepts the following options:
1805 =item * inline_accessors
1807 =item * inline_constructor
1809 =item * inline_destructor
1811 These are all booleans indicating whether the specified method(s)
1814 By default, accessors and the constructor are inlined, but not the
1817 =item * immutable_trait
1819 The name of a class which will be used as a parent class for the
1820 metaclass object being made immutable. This "trait" implements the
1821 post-immutability functionality of the metaclass (but not the
1822 transformation itself).
1824 This defaults to L<Class::MOP::Class::Immutable::Trait>.
1826 =item * constructor_name
1828 This is the constructor method name. This defaults to "new".
1830 =item * constructor_class
1832 The name of the method metaclass for constructors. It will be used to
1833 generate the inlined constructor. This defaults to
1834 "Class::MOP::Method::Constructor".
1836 =item * replace_constructor
1838 This is a boolean indicating whether an existing constructor should be
1839 replaced when inlining a constructor. This defaults to false.
1841 =item * destructor_class
1843 The name of the method metaclass for destructors. It will be used to
1844 generate the inlined destructor. This defaults to
1845 "Class::MOP::Method::Denstructor".
1847 =item * replace_destructor
1849 This is a boolean indicating whether an existing destructor should be
1850 replaced when inlining a destructor. This defaults to false.
1854 =item B<< $metaclass->immutable_options >>
1856 Returns a hash of the options used when making the class immutable, including
1857 both defaults and anything supplied by the user in the call to C<<
1858 $metaclass->make_immutable >>. This is useful if you need to temporarily make
1859 a class mutable and then restore immutability as it was before.
1861 =item B<< $metaclass->make_mutable >>
1863 Calling this method reverse the immutabilization transformation.
1867 =head2 Method Modifiers
1869 Method modifiers are hooks which allow a method to be wrapped with
1870 I<before>, I<after> and I<around> method modifiers. Every time a
1871 method is called, its modifiers are also called.
1873 A class can modify its own methods, as well as methods defined in
1876 =head3 How method modifiers work?
1878 Method modifiers work by wrapping the original method and then
1879 replacing it in the class's symbol table. The wrappers will handle
1880 calling all the modifiers in the appropriate order and preserving the
1881 calling context for the original method.
1883 The return values of C<before> and C<after> modifiers are
1884 ignored. This is because their purpose is B<not> to filter the input
1885 and output of the primary method (this is done with an I<around>
1888 This may seem like an odd restriction to some, but doing this allows
1889 for simple code to be added at the beginning or end of a method call
1890 without altering the function of the wrapped method or placing any
1891 extra responsibility on the code of the modifier.
1893 Of course if you have more complex needs, you can use the C<around>
1894 modifier which allows you to change both the parameters passed to the
1895 wrapped method, as well as its return value.
1897 Before and around modifiers are called in last-defined-first-called
1898 order, while after modifiers are called in first-defined-first-called
1899 order. So the call tree might looks something like this:
1911 =head3 What is the performance impact?
1913 Of course there is a performance cost associated with method
1914 modifiers, but we have made every effort to make that cost directly
1915 proportional to the number of modifier features you use.
1917 The wrapping method does its best to B<only> do as much work as it
1918 absolutely needs to. In order to do this we have moved some of the
1919 performance costs to set-up time, where they are easier to amortize.
1921 All this said, our benchmarks have indicated the following:
1923 simple wrapper with no modifiers 100% slower
1924 simple wrapper with simple before modifier 400% slower
1925 simple wrapper with simple after modifier 450% slower
1926 simple wrapper with simple around modifier 500-550% slower
1927 simple wrapper with all 3 modifiers 1100% slower
1929 These numbers may seem daunting, but you must remember, every feature
1930 comes with some cost. To put things in perspective, just doing a
1931 simple C<AUTOLOAD> which does nothing but extract the name of the
1932 method called and return it costs about 400% over a normal method
1937 =item B<< $metaclass->add_before_method_modifier($method_name, $code) >>
1939 This wraps the specified method with the supplied subroutine
1940 reference. The modifier will be called as a method itself, and will
1941 receive the same arguments as are passed to the method.
1943 When the modifier exits, the wrapped method will be called.
1945 The return value of the modifier will be ignored.
1947 =item B<< $metaclass->add_after_method_modifier($method_name, $code) >>
1949 This wraps the specified method with the supplied subroutine
1950 reference. The modifier will be called as a method itself, and will
1951 receive the same arguments as are passed to the method.
1953 When the wrapped methods exits, the modifier will be called.
1955 The return value of the modifier will be ignored.
1957 =item B<< $metaclass->add_around_method_modifier($method_name, $code) >>
1959 This wraps the specified method with the supplied subroutine
1962 The first argument passed to the modifier will be a subroutine
1963 reference to the wrapped method. The second argument is the object,
1964 and after that come any arguments passed when the method is called.
1966 The around modifier can choose to call the original method, as well as
1967 what arguments to pass if it does so.
1969 The return value of the modifier is what will be seen by the caller.
1973 =head2 Introspection
1977 =item B<< Class::MOP::Class->meta >>
1979 This will return a L<Class::MOP::Class> instance for this class.
1981 It should also be noted that L<Class::MOP> will actually bootstrap
1982 this module by installing a number of attribute meta-objects into its
1989 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1991 =head1 COPYRIGHT AND LICENSE
1993 Copyright 2006-2010 by Infinity Interactive, Inc.
1995 L<http://www.iinteractive.com>
1997 This library is free software; you can redistribute it and/or modify
1998 it under the same terms as Perl itself.