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;
11 use Class::MOP::MiniTrait;
14 use Scalar::Util 'blessed', 'reftype', 'weaken';
15 use Sub::Name 'subname';
16 use Devel::GlobalDestruction 'in_global_destruction';
18 use List::MoreUtils 'all';
20 our $VERSION = '1.08';
21 $VERSION = eval $VERSION;
22 our $AUTHORITY = 'cpan:STEVAN';
24 use base 'Class::MOP::Module',
25 'Class::MOP::Mixin::HasAttributes',
26 'Class::MOP::Mixin::HasMethods';
36 $package_name = shift;
39 $package_name = $options{package};
42 ($package_name && !ref($package_name))
43 || confess "You must pass a package name and it cannot be blessed";
45 return Class::MOP::get_metaclass_by_name($package_name)
46 || $class->_construct_class_instance(package => $package_name, @_);
49 # NOTE: (meta-circularity)
50 # this is a special form of _construct_instance
51 # (see below), which is used to construct class
52 # meta-object instances for any Class::MOP::*
53 # class. All other classes will use the more
54 # normal &construct_instance.
55 sub _construct_class_instance {
57 my $options = @_ == 1 ? $_[0] : {@_};
58 my $package_name = $options->{package};
59 (defined $package_name && $package_name)
60 || confess "You must pass a package name";
62 # return the metaclass if we have it cached,
63 # and it is still defined (it has not been
64 # reaped by DESTROY yet, which can happen
65 # annoyingly enough during global destruction)
67 if (defined(my $meta = Class::MOP::get_metaclass_by_name($package_name))) {
73 ? $class->_real_ref_name
76 # now create the metaclass
78 if ($class eq 'Class::MOP::Class') {
79 $meta = $class->_new($options);
83 # it is safe to use meta here because
84 # class will always be a subclass of
85 # Class::MOP::Class, which defines meta
86 $meta = $class->meta->_construct_instance($options)
89 # and check the metaclass compatibility
90 $meta->_check_metaclass_compatibility();
92 Class::MOP::store_metaclass_by_name($package_name, $meta);
95 # we need to weaken any anon classes
96 # so that they can call DESTROY properly
97 Class::MOP::weaken_metaclass($package_name) if $meta->is_anon_class;
105 # NOTE: we need to deal with the possibility of class immutability here,
106 # and then get the name of the class appropriately
107 return $self->is_immutable
108 ? $self->_get_mutable_metaclass_name()
115 return Class::MOP::Class->initialize($class)->new_object(@_)
116 if $class ne __PACKAGE__;
118 my $options = @_ == 1 ? $_[0] : {@_};
121 # inherited from Class::MOP::Package
122 'package' => $options->{package},
125 # since the following attributes will
126 # actually be loaded from the symbol
127 # table, and actually bypass the instance
128 # entirely, we can just leave these things
129 # listed here for reference, because they
130 # should not actually have a value associated
132 'namespace' => \undef,
135 # inherited from Class::MOP::Module
137 'authority' => \undef,
139 # defined in Class::MOP::Class
140 'superclasses' => \undef,
143 'attribute_metaclass' =>
144 ( $options->{'attribute_metaclass'} || 'Class::MOP::Attribute' ),
145 'method_metaclass' =>
146 ( $options->{'method_metaclass'} || 'Class::MOP::Method' ),
147 'wrapped_method_metaclass' => (
148 $options->{'wrapped_method_metaclass'}
149 || 'Class::MOP::Method::Wrapped'
151 'instance_metaclass' =>
152 ( $options->{'instance_metaclass'} || 'Class::MOP::Instance' ),
153 'immutable_trait' => (
154 $options->{'immutable_trait'}
155 || 'Class::MOP::Class::Immutable::Trait'
157 'constructor_name' => ( $options->{constructor_name} || 'new' ),
158 'constructor_class' => (
159 $options->{constructor_class} || 'Class::MOP::Method::Constructor'
161 'destructor_class' => $options->{destructor_class},
165 sub reset_package_cache_flag { (shift)->{'_package_cache_flag'} = undef }
166 sub update_package_cache_flag {
169 # we can manually update the cache number
170 # since we are actually adding the method
171 # to our cache as well. This avoids us
172 # having to regenerate the method_map.
174 $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);
177 ## Metaclass compatibility
179 my %base_metaclass = (
180 attribute_metaclass => 'Class::MOP::Attribute',
181 method_metaclass => 'Class::MOP::Method',
182 wrapped_method_metaclass => 'Class::MOP::Method::Wrapped',
183 instance_metaclass => 'Class::MOP::Instance',
184 constructor_class => 'Class::MOP::Method::Constructor',
185 destructor_class => 'Class::MOP::Method::Destructor',
188 sub _base_metaclasses { %base_metaclass }
191 sub _check_metaclass_compatibility {
194 my @superclasses = $self->superclasses
197 $self->_fix_metaclass_incompatibility(@superclasses);
199 my %base_metaclass = $self->_base_metaclasses;
201 # this is always okay ...
203 if ref($self) eq 'Class::MOP::Class'
205 my $meta = $self->$_;
206 !defined($meta) || $meta eq $base_metaclass{$_};
208 keys %base_metaclass;
210 for my $superclass (@superclasses) {
211 $self->_check_class_metaclass_compatibility($superclass);
214 for my $metaclass_type ( keys %base_metaclass ) {
215 next unless defined $self->$metaclass_type;
216 for my $superclass (@superclasses) {
217 $self->_check_single_metaclass_compatibility( $metaclass_type,
223 sub _class_metaclass_is_compatible {
225 my ( $superclass_name ) = @_;
227 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
230 my $super_meta_type = $super_meta->_real_ref_name;
232 return $self->isa($super_meta_type);
235 sub _check_class_metaclass_compatibility {
237 my ( $superclass_name ) = @_;
239 if (!$self->_class_metaclass_is_compatible($superclass_name)) {
240 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
242 my $super_meta_type = $super_meta->_real_ref_name;
244 confess "The metaclass of " . $self->name . " ("
245 . (ref($self)) . ")" . " is not compatible with "
246 . "the metaclass of its superclass, "
247 . $superclass_name . " (" . ($super_meta_type) . ")";
251 sub _single_metaclass_is_compatible {
253 my ( $metaclass_type, $superclass_name ) = @_;
255 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
258 # for instance, Moose::Meta::Class has a error_class attribute, but
259 # Class::MOP::Class doesn't - this shouldn't be an error
260 return 1 unless $super_meta->can($metaclass_type);
261 # for instance, Moose::Meta::Class has a destructor_class, but
262 # Class::MOP::Class doesn't - this shouldn't be an error
263 return 1 unless defined $super_meta->$metaclass_type;
264 # if metaclass is defined in superclass but not here, it's not compatible
265 # this is a really odd case
266 return 0 unless defined $self->$metaclass_type;
268 return $self->$metaclass_type->isa($super_meta->$metaclass_type);
271 sub _check_single_metaclass_compatibility {
273 my ( $metaclass_type, $superclass_name ) = @_;
275 if (!$self->_single_metaclass_is_compatible($metaclass_type, $superclass_name)) {
276 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
277 my $metaclass_type_name = $metaclass_type;
278 $metaclass_type_name =~ s/_(?:meta)?class$//;
279 $metaclass_type_name =~ s/_/ /g;
280 confess "The $metaclass_type_name metaclass for "
281 . $self->name . " (" . ($self->$metaclass_type)
282 . ")" . " is not compatible with the "
283 . "$metaclass_type_name metaclass of its "
284 . "superclass, $superclass_name ("
285 . ($super_meta->$metaclass_type) . ")";
289 sub _can_fix_class_metaclass_incompatibility_by_subclassing {
291 my ($super_meta) = @_;
293 my $super_meta_type = $super_meta->_real_ref_name;
295 return $super_meta_type ne blessed($self)
296 && $super_meta->isa(blessed($self));
299 sub _can_fix_single_metaclass_incompatibility_by_subclassing {
301 my ($metaclass_type, $super_meta) = @_;
303 my $specific_meta = $self->$metaclass_type;
304 return unless $super_meta->can($metaclass_type);
305 my $super_specific_meta = $super_meta->$metaclass_type;
307 # for instance, Moose::Meta::Class has a destructor_class, but
308 # Class::MOP::Class doesn't - this shouldn't be an error
309 return unless defined $super_specific_meta;
311 # if metaclass is defined in superclass but not here, it's fixable
312 # this is a really odd case
313 return 1 unless defined $specific_meta;
315 return $specific_meta ne $super_specific_meta
316 && $super_specific_meta->isa($specific_meta);
319 sub _can_fix_metaclass_incompatibility_by_subclassing {
321 my ($super_meta) = @_;
323 return 1 if $self->_can_fix_class_metaclass_incompatibility_by_subclassing($super_meta);
325 my %base_metaclass = $self->_base_metaclasses;
326 for my $metaclass_type (keys %base_metaclass) {
327 return 1 if $self->_can_fix_single_metaclass_incompatibility_by_subclassing($metaclass_type, $super_meta);
333 sub _can_fix_metaclass_incompatibility {
335 return $self->_can_fix_metaclass_incompatibility_by_subclassing(@_);
338 sub _fix_metaclass_incompatibility {
340 my @supers = map { Class::MOP::Class->initialize($_) } @_;
343 for my $super (@supers) {
345 if $self->_can_fix_metaclass_incompatibility($super);
347 return unless $necessary;
349 for my $super (@supers) {
350 if (!$self->_class_metaclass_is_compatible($super->name)) {
351 $self->_fix_class_metaclass_incompatibility($super);
355 my %base_metaclass = $self->_base_metaclasses;
356 for my $metaclass_type (keys %base_metaclass) {
357 for my $super (@supers) {
358 if (!$self->_single_metaclass_is_compatible($metaclass_type, $super->name)) {
359 $self->_fix_single_metaclass_incompatibility(
360 $metaclass_type, $super
367 sub _fix_class_metaclass_incompatibility {
369 my ( $super_meta ) = @_;
371 if ($self->_can_fix_class_metaclass_incompatibility_by_subclassing($super_meta)) {
373 || confess "Can't fix metaclass incompatibility for "
375 . " because it is not pristine.";
377 my $super_meta_name = $super_meta->_real_ref_name;
379 $super_meta_name->meta->rebless_instance($self);
383 sub _fix_single_metaclass_incompatibility {
385 my ( $metaclass_type, $super_meta ) = @_;
387 if ($self->_can_fix_single_metaclass_incompatibility_by_subclassing($metaclass_type, $super_meta)) {
389 || confess "Can't fix metaclass incompatibility for "
391 . " because it is not pristine.";
393 $self->{$metaclass_type} = $super_meta->$metaclass_type;
401 # this should be sufficient, if you have a
402 # use case where it is not, write a test and
404 my $ANON_CLASS_SERIAL = 0;
407 # we need a sufficiently annoying prefix
408 # this should suffice for now, this is
409 # used in a couple of places below, so
410 # need to put it up here for now.
411 my $ANON_CLASS_PREFIX = 'Class::MOP::Class::__ANON__::SERIAL::';
415 no warnings 'uninitialized';
416 $self->name =~ /^$ANON_CLASS_PREFIX/o;
419 sub create_anon_class {
420 my ($class, %options) = @_;
421 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
422 return $class->create($package_name, %options);
426 # this will only get called for
427 # anon-classes, all other calls
428 # are assumed to occur during
429 # global destruction and so don't
430 # really need to be handled explicitly
434 return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated
436 no warnings 'uninitialized';
437 my $name = $self->name;
438 return unless $name =~ /^$ANON_CLASS_PREFIX/o;
440 # Moose does a weird thing where it replaces the metaclass for
441 # class when fixing metaclass incompatibility. In that case,
442 # we don't want to clean out the namespace now. We can detect
443 # that because Moose will explicitly update the singleton
444 # cache in Class::MOP.
445 my $current_meta = Class::MOP::get_metaclass_by_name($name);
446 return if $current_meta ne $self;
448 my ($serial_id) = ($name =~ /^$ANON_CLASS_PREFIX(\d+)/o);
450 @{$name . '::ISA'} = ();
451 %{$name . '::'} = ();
452 delete ${$ANON_CLASS_PREFIX}{$serial_id . '::'};
454 Class::MOP::remove_metaclass_by_name($name);
459 # creating classes with MOP ...
462 my ( $class, @args ) = @_;
464 unshift @args, 'package' if @args % 2 == 1;
466 my (%options) = @args;
467 my $package_name = $options{package};
469 (ref $options{superclasses} eq 'ARRAY')
470 || confess "You must pass an ARRAY ref of superclasses"
471 if exists $options{superclasses};
473 (ref $options{attributes} eq 'ARRAY')
474 || confess "You must pass an ARRAY ref of attributes"
475 if exists $options{attributes};
477 (ref $options{methods} eq 'HASH')
478 || confess "You must pass a HASH ref of methods"
479 if exists $options{methods};
481 my (%initialize_options) = @args;
482 delete @initialize_options{qw(
491 my $meta = $class->initialize( $package_name => %initialize_options );
493 $meta->_instantiate_module( $options{version}, $options{authority} );
496 $meta->add_method('meta' => sub {
497 if (Class::MOP::DEBUG_NO_META()) {
499 if (my $meta = try { $self->SUPER::meta }) {
500 return $meta if $meta->isa('Class::MOP::Class');
502 confess "'meta' method called by MOP internals"
503 if caller =~ /Class::MOP|metaclass/;
505 $class->initialize(ref($_[0]) || $_[0]);
506 }) unless $options{no_meta};
508 $meta->superclasses(@{$options{superclasses}})
509 if exists $options{superclasses};
511 # process attributes first, so that they can
512 # install accessors, but locally defined methods
513 # can then overwrite them. It is maybe a little odd, but
514 # I think this should be the order of things.
515 if (exists $options{attributes}) {
516 foreach my $attr (@{$options{attributes}}) {
517 $meta->add_attribute($attr);
520 if (exists $options{methods}) {
521 foreach my $method_name (keys %{$options{methods}}) {
522 $meta->add_method($method_name, $options{methods}->{$method_name});
531 # all these attribute readers will be bootstrapped
532 # away in the Class::MOP bootstrap section
534 sub instance_metaclass { $_[0]->{'instance_metaclass'} }
535 sub immutable_trait { $_[0]->{'immutable_trait'} }
536 sub constructor_class { $_[0]->{'constructor_class'} }
537 sub constructor_name { $_[0]->{'constructor_name'} }
538 sub destructor_class { $_[0]->{'destructor_class'} }
540 # Instance Construction & Cloning
546 # we need to protect the integrity of the
547 # Class::MOP::Class singletons here, so we
548 # delegate this to &construct_class_instance
549 # which will deal with the singletons
550 return $class->_construct_class_instance(@_)
551 if $class->name->isa('Class::MOP::Class');
552 return $class->_construct_instance(@_);
555 sub _construct_instance {
557 my $params = @_ == 1 ? $_[0] : {@_};
558 my $meta_instance = $class->get_meta_instance();
560 # the code below is almost certainly incorrect
561 # but this is foreign inheritance, so we might
562 # have to kludge it in the end.
564 if (my $instance_class = blessed($params->{__INSTANCE__})) {
565 ($instance_class eq $class->name)
566 || confess "Objects passed as the __INSTANCE__ parameter must "
567 . "already be blessed into the correct class, but "
568 . "$params->{__INSTANCE__} is not a " . $class->name;
569 $instance = $params->{__INSTANCE__};
571 elsif (exists $params->{__INSTANCE__}) {
572 confess "The __INSTANCE__ parameter must be a blessed reference, not "
573 . $params->{__INSTANCE__};
576 $instance = $meta_instance->create_instance();
578 foreach my $attr ($class->get_all_attributes()) {
579 $attr->initialize_instance_slot($meta_instance, $instance, $params);
582 # this will only work for a HASH instance type
583 if ($class->is_anon_class) {
584 (reftype($instance) eq 'HASH')
585 || confess "Currently only HASH based instances are supported with instance of anon-classes";
587 # At some point we should make this official
588 # as a reserved slot name, but right now I am
589 # going to keep it here.
590 # my $RESERVED_MOP_SLOT = '__MOP__';
591 $instance->{'__MOP__'} = $class;
597 sub get_meta_instance {
599 $self->{'_meta_instance'} ||= $self->_create_meta_instance();
602 sub _create_meta_instance {
605 my $instance = $self->instance_metaclass->new(
606 associated_metaclass => $self,
607 attributes => [ $self->get_all_attributes() ],
610 $self->add_meta_instance_dependencies()
611 if $instance->is_dependent_on_superclasses();
616 sub inline_create_instance {
620 return $self->get_meta_instance->inline_create_instance($class);
625 my $instance = shift;
626 (blessed($instance) && $instance->isa($class->name))
627 || confess "You must pass an instance of the metaclass (" . (ref $class ? $class->name : $class) . "), not ($instance)";
630 # we need to protect the integrity of the
631 # Class::MOP::Class singletons here, they
632 # should not be cloned.
633 return $instance if $instance->isa('Class::MOP::Class');
634 $class->_clone_instance($instance, @_);
637 sub _clone_instance {
638 my ($class, $instance, %params) = @_;
640 || confess "You can only clone instances, ($instance) is not a blessed instance";
641 my $meta_instance = $class->get_meta_instance();
642 my $clone = $meta_instance->clone_instance($instance);
643 foreach my $attr ($class->get_all_attributes()) {
644 if ( defined( my $init_arg = $attr->init_arg ) ) {
645 if (exists $params{$init_arg}) {
646 $attr->set_value($clone, $params{$init_arg});
653 sub rebless_instance {
654 my ($self, $instance, %params) = @_;
656 my $old_metaclass = Class::MOP::class_of($instance);
658 my $old_class = $old_metaclass ? $old_metaclass->name : blessed($instance);
659 $self->name->isa($old_class)
660 || confess "You may rebless only into a subclass of ($old_class), of which (". $self->name .") isn't.";
662 $old_metaclass->rebless_instance_away($instance, $self, %params)
665 my $meta_instance = $self->get_meta_instance();
668 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
669 $meta_instance->rebless_instance_structure($_[1], $self);
671 foreach my $attr ( $self->get_all_attributes ) {
672 if ( $attr->has_value($instance) ) {
673 if ( defined( my $init_arg = $attr->init_arg ) ) {
674 $params{$init_arg} = $attr->get_value($instance)
675 unless exists $params{$init_arg};
678 $attr->set_value($instance, $attr->get_value($instance));
683 foreach my $attr ($self->get_all_attributes) {
684 $attr->initialize_instance_slot($meta_instance, $instance, \%params);
690 sub rebless_instance_back {
691 my ( $self, $instance ) = @_;
693 my $old_metaclass = Class::MOP::class_of($instance);
696 = $old_metaclass ? $old_metaclass->name : blessed($instance);
697 $old_class->isa( $self->name )
699 "You may rebless only into a superclass of ($old_class), of which ("
703 $old_metaclass->rebless_instance_away( $instance, $self )
706 my $meta_instance = $self->get_meta_instance;
708 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
709 $meta_instance->rebless_instance_structure( $_[1], $self );
711 for my $attr ( $old_metaclass->get_all_attributes ) {
712 next if $self->has_attribute( $attr->name );
713 $meta_instance->deinitialize_slot( $instance, $_ ) for $attr->slots;
719 sub rebless_instance_away {
720 # this intentionally does nothing, it is just a hook
723 sub _attach_attribute {
724 my ($self, $attribute) = @_;
725 $attribute->attach_to_class($self);
728 sub _post_add_attribute {
729 my ( $self, $attribute ) = @_;
731 $self->invalidate_meta_instances;
733 # invalidate package flag here
736 $attribute->install_accessors;
739 $self->remove_attribute( $attribute->name );
744 sub remove_attribute {
747 my $removed_attribute = $self->SUPER::remove_attribute(@_)
750 $self->invalidate_meta_instances;
752 $removed_attribute->remove_accessors;
753 $removed_attribute->detach_from_class;
755 return$removed_attribute;
758 sub find_attribute_by_name {
759 my ( $self, $attr_name ) = @_;
761 foreach my $class ( $self->linearized_isa ) {
762 # fetch the meta-class ...
763 my $meta = Class::MOP::Class->initialize($class);
764 return $meta->get_attribute($attr_name)
765 if $meta->has_attribute($attr_name);
771 sub get_all_attributes {
773 my %attrs = map { %{ Class::MOP::Class->initialize($_)->_attribute_map } }
774 reverse $self->linearized_isa;
775 return values %attrs;
783 my $isa = $self->get_or_add_package_symbol(
784 { sigil => '@', type => 'ARRAY', name => 'ISA' } );
791 # on 5.8 and below, we need to call
792 # a method to get Perl to detect
793 # a cycle in the class hierarchy
794 my $class = $self->name;
798 # we need to check the metaclass
799 # compatibility here so that we can
800 # be sure that the superclass is
801 # not potentially creating an issues
802 # we don't know about
804 $self->_check_metaclass_compatibility();
805 $self->_superclasses_updated();
811 sub _superclasses_updated {
813 $self->update_meta_instance_dependencies();
818 my $super_class = $self->name;
820 return @{ $super_class->mro::get_isarev() };
823 sub direct_subclasses {
825 my $super_class = $self->name;
830 } Class::MOP::Class->initialize($_)->superclasses
835 return @{ mro::get_linear_isa( (shift)->name ) };
838 sub class_precedence_list {
840 my $name = $self->name;
842 unless (Class::MOP::IS_RUNNING_ON_5_10()) {
844 # We need to check for circular inheritance here
845 # if we are are not on 5.10, cause 5.8 detects it
846 # late. This will do nothing if all is well, and
847 # blow up otherwise. Yes, it's an ugly hack, better
848 # suggestions are welcome.
850 ($name || return)->isa('This is a test for circular inheritance')
853 # if our mro is c3, we can
854 # just grab the linear_isa
855 if (mro::get_mro($name) eq 'c3') {
856 return @{ mro::get_linear_isa($name) }
860 # we can't grab the linear_isa for dfs
861 # since it has all the duplicates
866 Class::MOP::Class->initialize($_)->class_precedence_list()
867 } $self->superclasses()
875 my $fetch_and_prepare_method = sub {
876 my ($self, $method_name) = @_;
877 my $wrapped_metaclass = $self->wrapped_method_metaclass;
879 my $method = $self->get_method($method_name);
880 # if we dont have local ...
882 # try to find the next method
883 $method = $self->find_next_method_by_name($method_name);
884 # die if it does not exist
886 || confess "The method '$method_name' was not found in the inheritance hierarchy for " . $self->name;
887 # and now make sure to wrap it
888 # even if it is already wrapped
889 # because we need a new sub ref
890 $method = $wrapped_metaclass->wrap($method,
891 package_name => $self->name,
892 name => $method_name,
896 # now make sure we wrap it properly
897 $method = $wrapped_metaclass->wrap($method,
898 package_name => $self->name,
899 name => $method_name,
900 ) unless $method->isa($wrapped_metaclass);
902 $self->add_method($method_name => $method);
906 sub add_before_method_modifier {
907 my ($self, $method_name, $method_modifier) = @_;
908 (defined $method_name && length $method_name)
909 || confess "You must pass in a method name";
910 my $method = $fetch_and_prepare_method->($self, $method_name);
911 $method->add_before_modifier(
912 subname(':before' => $method_modifier)
916 sub add_after_method_modifier {
917 my ($self, $method_name, $method_modifier) = @_;
918 (defined $method_name && length $method_name)
919 || confess "You must pass in a method name";
920 my $method = $fetch_and_prepare_method->($self, $method_name);
921 $method->add_after_modifier(
922 subname(':after' => $method_modifier)
926 sub add_around_method_modifier {
927 my ($self, $method_name, $method_modifier) = @_;
928 (defined $method_name && length $method_name)
929 || confess "You must pass in a method name";
930 my $method = $fetch_and_prepare_method->($self, $method_name);
931 $method->add_around_modifier(
932 subname(':around' => $method_modifier)
937 # the methods above used to be named like this:
938 # ${pkg}::${method}:(before|after|around)
939 # but this proved problematic when using one modifier
940 # to wrap multiple methods (something which is likely
941 # to happen pretty regularly IMO). So instead of naming
942 # it like this, I have chosen to just name them purely
943 # with their modifier names, like so:
944 # :(before|after|around)
945 # The fact is that in a stack trace, it will be fairly
946 # evident from the context what method they are attached
947 # to, and so don't need the fully qualified name.
950 sub find_method_by_name {
951 my ($self, $method_name) = @_;
952 (defined $method_name && length $method_name)
953 || confess "You must define a method name to find";
954 foreach my $class ($self->linearized_isa) {
955 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
956 return $method if defined $method;
961 sub get_all_methods {
965 for my $class ( reverse $self->linearized_isa ) {
966 my $meta = Class::MOP::Class->initialize($class);
968 $methods{ $_->name } = $_ for $meta->_get_local_methods;
971 return values %methods;
974 sub get_all_method_names {
977 return grep { !$uniq{$_}++ } map { Class::MOP::Class->initialize($_)->get_method_list } $self->linearized_isa;
980 sub find_all_methods_by_name {
981 my ($self, $method_name) = @_;
982 (defined $method_name && length $method_name)
983 || confess "You must define a method name to find";
985 foreach my $class ($self->linearized_isa) {
986 # fetch the meta-class ...
987 my $meta = Class::MOP::Class->initialize($class);
989 name => $method_name,
991 code => $meta->get_method($method_name)
992 } if $meta->has_method($method_name);
997 sub find_next_method_by_name {
998 my ($self, $method_name) = @_;
999 (defined $method_name && length $method_name)
1000 || confess "You must define a method name to find";
1001 my @cpl = $self->linearized_isa;
1002 shift @cpl; # discard ourselves
1003 foreach my $class (@cpl) {
1004 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
1005 return $method if defined $method;
1010 sub update_meta_instance_dependencies {
1013 if ( $self->{meta_instance_dependencies} ) {
1014 return $self->add_meta_instance_dependencies;
1018 sub add_meta_instance_dependencies {
1021 $self->remove_meta_instance_dependencies;
1023 my @attrs = $self->get_all_attributes();
1026 my @classes = grep { not $seen{ $_->name }++ }
1027 map { $_->associated_class } @attrs;
1029 foreach my $class (@classes) {
1030 $class->add_dependent_meta_instance($self);
1033 $self->{meta_instance_dependencies} = \@classes;
1036 sub remove_meta_instance_dependencies {
1039 if ( my $classes = delete $self->{meta_instance_dependencies} ) {
1040 foreach my $class (@$classes) {
1041 $class->remove_dependent_meta_instance($self);
1051 sub add_dependent_meta_instance {
1052 my ( $self, $metaclass ) = @_;
1053 push @{ $self->{dependent_meta_instances} }, $metaclass;
1056 sub remove_dependent_meta_instance {
1057 my ( $self, $metaclass ) = @_;
1058 my $name = $metaclass->name;
1059 @$_ = grep { $_->name ne $name } @$_
1060 for $self->{dependent_meta_instances};
1063 sub invalidate_meta_instances {
1065 $_->invalidate_meta_instance()
1066 for $self, @{ $self->{dependent_meta_instances} };
1069 sub invalidate_meta_instance {
1071 undef $self->{_meta_instance};
1074 # check if we can reinitialize
1078 # if any local attr is defined
1079 return if $self->get_attribute_list;
1081 # or any non-declared methods
1082 for my $method ( map { $self->get_method($_) } $self->get_method_list ) {
1083 return if $method->isa("Class::MOP::Method::Generated");
1084 # FIXME do we need to enforce this too? return unless $method->isa( $self->method_metaclass );
1092 sub is_mutable { 1 }
1093 sub is_immutable { 0 }
1095 sub immutable_options { %{ $_[0]{__immutable}{options} || {} } }
1097 sub _immutable_options {
1098 my ( $self, @args ) = @_;
1101 inline_accessors => 1,
1102 inline_constructor => 1,
1103 inline_destructor => 0,
1105 immutable_trait => $self->immutable_trait,
1106 constructor_name => $self->constructor_name,
1107 constructor_class => $self->constructor_class,
1108 destructor_class => $self->destructor_class,
1113 sub make_immutable {
1114 my ( $self, @args ) = @_;
1116 if ( $self->is_mutable ) {
1117 $self->_initialize_immutable( $self->_immutable_options(@args) );
1118 $self->_rebless_as_immutable(@args);
1129 if ( $self->is_immutable ) {
1130 my @args = $self->immutable_options;
1131 $self->_rebless_as_mutable();
1132 $self->_remove_inlined_code(@args);
1133 delete $self->{__immutable};
1141 sub _rebless_as_immutable {
1142 my ( $self, @args ) = @_;
1144 $self->{__immutable}{original_class} = ref $self;
1146 bless $self => $self->_immutable_metaclass(@args);
1149 sub _immutable_metaclass {
1150 my ( $self, %args ) = @_;
1152 if ( my $class = $args{immutable_metaclass} ) {
1156 my $trait = $args{immutable_trait} = $self->immutable_trait
1157 || confess "no immutable trait specified for $self";
1159 my $meta = $self->meta;
1160 my $meta_attr = $meta->find_attribute_by_name("immutable_trait");
1164 if ( $meta_attr and $trait eq $meta_attr->default ) {
1165 # if the trait is the same as the default we try and pick a
1166 # predictable name for the immutable metaclass
1167 $class_name = 'Class::MOP::Class::Immutable::' . ref($self);
1170 $class_name = join '::', 'Class::MOP::Class::Immutable::CustomTrait',
1171 $trait, 'ForMetaClass', ref($self);
1175 if Class::MOP::is_class_loaded($class_name);
1177 # If the metaclass is a subclass of CMOP::Class which has had
1178 # metaclass roles applied (via Moose), then we want to make sure
1179 # that we preserve that anonymous class (see Fey::ORM for an
1180 # example of where this matters).
1181 my $meta_name = $meta->_real_ref_name;
1183 my $immutable_meta = $meta_name->create(
1185 superclasses => [ ref $self ],
1188 Class::MOP::MiniTrait::apply( $immutable_meta, $trait );
1190 $immutable_meta->make_immutable(
1191 inline_constructor => 0,
1192 inline_accessors => 0,
1198 sub _remove_inlined_code {
1201 $self->remove_method( $_->name ) for $self->_inlined_methods;
1203 delete $self->{__immutable}{inlined_methods};
1206 sub _inlined_methods { @{ $_[0]{__immutable}{inlined_methods} || [] } }
1208 sub _add_inlined_method {
1209 my ( $self, $method ) = @_;
1211 push @{ $self->{__immutable}{inlined_methods} ||= [] }, $method;
1214 sub _initialize_immutable {
1215 my ( $self, %args ) = @_;
1217 $self->{__immutable}{options} = \%args;
1218 $self->_install_inlined_code(%args);
1221 sub _install_inlined_code {
1222 my ( $self, %args ) = @_;
1225 $self->_inline_accessors(%args) if $args{inline_accessors};
1226 $self->_inline_constructor(%args) if $args{inline_constructor};
1227 $self->_inline_destructor(%args) if $args{inline_destructor};
1230 sub _rebless_as_mutable {
1233 bless $self, $self->_get_mutable_metaclass_name;
1238 sub _inline_accessors {
1241 foreach my $attr_name ( $self->get_attribute_list ) {
1242 $self->get_attribute($attr_name)->install_accessors(1);
1246 sub _inline_constructor {
1247 my ( $self, %args ) = @_;
1249 my $name = $args{constructor_name};
1250 # A class may not even have a constructor, and that's okay.
1251 return unless defined $name;
1253 if ( $self->has_method($name) && !$args{replace_constructor} ) {
1254 my $class = $self->name;
1255 warn "Not inlining a constructor for $class since it defines"
1256 . " its own constructor.\n"
1257 . "If you are certain you don't need to inline your"
1258 . " constructor, specify inline_constructor => 0 in your"
1259 . " call to $class->meta->make_immutable\n";
1263 my $constructor_class = $args{constructor_class};
1265 Class::MOP::load_class($constructor_class);
1267 my $constructor = $constructor_class->new(
1271 package_name => $self->name,
1275 if ( $args{replace_constructor} or $constructor->can_be_inlined ) {
1276 $self->add_method( $name => $constructor );
1277 $self->_add_inlined_method($constructor);
1281 sub _inline_destructor {
1282 my ( $self, %args ) = @_;
1284 ( exists $args{destructor_class} && defined $args{destructor_class} )
1285 || confess "The 'inline_destructor' option is present, but "
1286 . "no destructor class was specified";
1288 if ( $self->has_method('DESTROY') && ! $args{replace_destructor} ) {
1289 my $class = $self->name;
1290 warn "Not inlining a destructor for $class since it defines"
1291 . " its own destructor.\n";
1295 my $destructor_class = $args{destructor_class};
1297 Class::MOP::load_class($destructor_class);
1299 return unless $destructor_class->is_needed($self);
1301 my $destructor = $destructor_class->new(
1304 package_name => $self->name,
1308 if ( $args{replace_destructor} or $destructor->can_be_inlined ) {
1309 $self->add_method( 'DESTROY' => $destructor );
1310 $self->_add_inlined_method($destructor);
1322 Class::MOP::Class - Class Meta Object
1326 # assuming that class Foo
1327 # has been defined, you can
1329 # use this for introspection ...
1331 # add a method to Foo ...
1332 Foo->meta->add_method( 'bar' => sub {...} )
1334 # get a list of all the classes searched
1335 # the method dispatcher in the correct order
1336 Foo->meta->class_precedence_list()
1338 # remove a method from Foo
1339 Foo->meta->remove_method('bar');
1341 # or use this to actually create classes ...
1343 Class::MOP::Class->create(
1346 superclasses => ['Foo'],
1348 Class::MOP::Attribute->new('$bar'),
1349 Class::MOP::Attribute->new('$baz'),
1352 calculate_bar => sub {...},
1353 construct_baz => sub {...}
1360 The Class Protocol is the largest and most complex part of the
1361 Class::MOP meta-object protocol. It controls the introspection and
1362 manipulation of Perl 5 classes, and it can create them as well. The
1363 best way to understand what this module can do is to read the
1364 documentation for each of its methods.
1368 C<Class::MOP::Class> is a subclass of L<Class::MOP::Module>.
1372 =head2 Class construction
1374 These methods all create new C<Class::MOP::Class> objects. These
1375 objects can represent existing classes or they can be used to create
1376 new classes from scratch.
1378 The metaclass object for a given class is a singleton. If you attempt
1379 to create a metaclass for the same class twice, you will just get the
1384 =item B<< Class::MOP::Class->create($package_name, %options) >>
1386 This method creates a new C<Class::MOP::Class> object with the given
1387 package name. It accepts a number of options:
1393 An optional version number for the newly created package.
1397 An optional authority for the newly created package.
1399 =item * superclasses
1401 An optional array reference of superclass names.
1405 An optional hash reference of methods for the class. The keys of the
1406 hash reference are method names and values are subroutine references.
1410 An optional array reference of L<Class::MOP::Attribute> objects.
1414 If true, a C<meta> method will not be installed into the class.
1418 =item B<< Class::MOP::Class->create_anon_class(%options) >>
1420 This method works just like C<< Class::MOP::Class->create >> but it
1421 creates an "anonymous" class. In fact, the class does have a name, but
1422 that name is a unique name generated internally by this module.
1424 It accepts the same C<superclasses>, C<methods>, and C<attributes>
1425 parameters that C<create> accepts.
1427 Anonymous classes are destroyed once the metaclass they are attached
1428 to goes out of scope, and will be removed from Perl's internal symbol
1431 All instances of an anonymous class keep a special reference to the
1432 metaclass object, which prevents the metaclass from going out of scope
1433 while any instances exist.
1435 This only works if the instance is based on a hash reference, however.
1437 =item B<< Class::MOP::Class->initialize($package_name, %options) >>
1439 This method will initialize a C<Class::MOP::Class> object for the
1440 named package. Unlike C<create>, this method I<will not> create a new
1443 The purpose of this method is to retrieve a C<Class::MOP::Class>
1444 object for introspecting an existing class.
1446 If an existing C<Class::MOP::Class> object exists for the named
1447 package, it will be returned, and any options provided will be
1450 If the object does not yet exist, it will be created.
1452 The valid options that can be passed to this method are
1453 C<attribute_metaclass>, C<method_metaclass>,
1454 C<wrapped_method_metaclass>, and C<instance_metaclass>. These are all
1455 optional, and default to the appropriate class in the C<Class::MOP>
1460 =head2 Object instance construction and cloning
1462 These methods are all related to creating and/or cloning object
1467 =item B<< $metaclass->clone_object($instance, %params) >>
1469 This method clones an existing object instance. Any parameters you
1470 provide are will override existing attribute values in the object.
1472 This is a convenience method for cloning an object instance, then
1473 blessing it into the appropriate package.
1475 You could implement a clone method in your class, using this method:
1478 my ($self, %params) = @_;
1479 $self->meta->clone_object($self, %params);
1482 =item B<< $metaclass->rebless_instance($instance, %params) >>
1484 This method changes the class of C<$instance> to the metaclass's class.
1486 You can only rebless an instance into a subclass of its current
1487 class. If you pass any additional parameters, these will be treated
1488 like constructor parameters and used to initialize the object's
1489 attributes. Any existing attributes that are already set will be
1492 Before reblessing the instance, this method will call
1493 C<rebless_instance_away> on the instance's current metaclass. This method
1494 will be passed the instance, the new metaclass, and any parameters
1495 specified to C<rebless_instance>. By default, C<rebless_instance_away>
1496 does nothing; it is merely a hook.
1498 =item B<< $metaclass->rebless_instance_back($instance) >>
1500 Does the same thing as C<rebless_instance>, except that you can only
1501 rebless an instance into one of its superclasses. Any attributes that
1502 do not exist in the superclass will be deinitialized.
1504 This is a much more dangerous operation than C<rebless_instance>,
1505 especially when multiple inheritance is involved, so use this carefully!
1507 =item B<< $metaclass->new_object(%params) >>
1509 This method is used to create a new object of the metaclass's
1510 class. Any parameters you provide are used to initialize the
1511 instance's attributes. A special C<__INSTANCE__> key can be passed to
1512 provide an already generated instance, rather than having Class::MOP
1513 generate it for you. This is mostly useful for using Class::MOP with
1514 foreign classes which generate instances using their own constructors.
1516 =item B<< $metaclass->instance_metaclass >>
1518 Returns the class name of the instance metaclass. See
1519 L<Class::MOP::Instance> for more information on the instance
1522 =item B<< $metaclass->get_meta_instance >>
1524 Returns an instance of the C<instance_metaclass> to be used in the
1525 construction of a new instance of the class.
1527 =item B<< $metaclass->inline_create_instance($class_var) >>
1529 This method takes a variable name, and uses it create an inline snippet of
1530 code that will create a new instance of the class.
1534 =head2 Informational predicates
1536 These are a few predicate methods for asking information about the
1541 =item B<< $metaclass->is_anon_class >>
1543 This returns true if the class was created by calling C<<
1544 Class::MOP::Class->create_anon_class >>.
1546 =item B<< $metaclass->is_mutable >>
1548 This returns true if the class is still mutable.
1550 =item B<< $metaclass->is_immutable >>
1552 This returns true if the class has been made immutable.
1554 =item B<< $metaclass->is_pristine >>
1556 A class is I<not> pristine if it has non-inherited attributes or if it
1557 has any generated methods.
1561 =head2 Inheritance Relationships
1565 =item B<< $metaclass->superclasses(@superclasses) >>
1567 This is a read-write accessor which represents the superclass
1568 relationships of the metaclass's class.
1570 This is basically sugar around getting and setting C<@ISA>.
1572 =item B<< $metaclass->class_precedence_list >>
1574 This returns a list of all of the class's ancestor classes. The
1575 classes are returned in method dispatch order.
1577 =item B<< $metaclass->linearized_isa >>
1579 This returns a list based on C<class_precedence_list> but with all
1582 =item B<< $metaclass->subclasses >>
1584 This returns a list of all subclasses for this class, even indirect
1587 =item B<< $metaclass->direct_subclasses >>
1589 This returns a list of immediate subclasses for this class, which does not
1590 include indirect subclasses.
1594 =head2 Method introspection and creation
1596 These methods allow you to introspect a class's methods, as well as
1597 add, remove, or change methods.
1599 Determining what is truly a method in a Perl 5 class requires some
1600 heuristics (aka guessing).
1602 Methods defined outside the package with a fully qualified name (C<sub
1603 Package::name { ... }>) will be included. Similarly, methods named
1604 with a fully qualified name using L<Sub::Name> are also included.
1606 However, we attempt to ignore imported functions.
1608 Ultimately, we are using heuristics to determine what truly is a
1609 method in a class, and these heuristics may get the wrong answer in
1610 some edge cases. However, for most "normal" cases the heuristics work
1615 =item B<< $metaclass->get_method($method_name) >>
1617 This will return a L<Class::MOP::Method> for the specified
1618 C<$method_name>. If the class does not have the specified method, it
1621 =item B<< $metaclass->has_method($method_name) >>
1623 Returns a boolean indicating whether or not the class defines the
1624 named method. It does not include methods inherited from parent
1627 =item B<< $metaclass->get_method_list >>
1629 This will return a list of method I<names> for all methods defined in
1632 =item B<< $metaclass->add_method($method_name, $method) >>
1634 This method takes a method name and a subroutine reference, and adds
1635 the method to the class.
1637 The subroutine reference can be a L<Class::MOP::Method>, and you are
1638 strongly encouraged to pass a meta method object instead of a code
1639 reference. If you do so, that object gets stored as part of the
1640 class's method map directly. If not, the meta information will have to
1641 be recreated later, and may be incorrect.
1643 If you provide a method object, this method will clone that object if
1644 the object's package name does not match the class name. This lets us
1645 track the original source of any methods added from other classes
1646 (notably Moose roles).
1648 =item B<< $metaclass->remove_method($method_name) >>
1650 Remove the named method from the class. This method returns the
1651 L<Class::MOP::Method> object for the method.
1653 =item B<< $metaclass->method_metaclass >>
1655 Returns the class name of the method metaclass, see
1656 L<Class::MOP::Method> for more information on the method metaclass.
1658 =item B<< $metaclass->wrapped_method_metaclass >>
1660 Returns the class name of the wrapped method metaclass, see
1661 L<Class::MOP::Method::Wrapped> for more information on the wrapped
1664 =item B<< $metaclass->get_all_methods >>
1666 This will traverse the inheritance hierarchy and return a list of all
1667 the L<Class::MOP::Method> objects for this class and its parents.
1669 =item B<< $metaclass->find_method_by_name($method_name) >>
1671 This will return a L<Class::MOP::Method> for the specified
1672 C<$method_name>. If the class does not have the specified method, it
1675 Unlike C<get_method>, this method I<will> look for the named method in
1678 =item B<< $metaclass->get_all_method_names >>
1680 This will return a list of method I<names> for all of this class's
1681 methods, including inherited methods.
1683 =item B<< $metaclass->find_all_methods_by_name($method_name) >>
1685 This method looks for the named method in the class and all of its
1686 parents. It returns every matching method it finds in the inheritance
1687 tree, so it returns a list of methods.
1689 Each method is returned as a hash reference with three keys. The keys
1690 are C<name>, C<class>, and C<code>. The C<code> key has a
1691 L<Class::MOP::Method> object as its value.
1693 The list of methods is distinct.
1695 =item B<< $metaclass->find_next_method_by_name($method_name) >>
1697 This method returns the first method in any superclass matching the
1698 given name. It is effectively the method that C<SUPER::$method_name>
1703 =head2 Attribute introspection and creation
1705 Because Perl 5 does not have a core concept of attributes in classes,
1706 we can only return information about attributes which have been added
1707 via this class's methods. We cannot discover information about
1708 attributes which are defined in terms of "regular" Perl 5 methods.
1712 =item B<< $metaclass->get_attribute($attribute_name) >>
1714 This will return a L<Class::MOP::Attribute> for the specified
1715 C<$attribute_name>. If the class does not have the specified
1716 attribute, it returns C<undef>.
1718 NOTE that get_attribute does not search superclasses, for that you
1719 need to use C<find_attribute_by_name>.
1721 =item B<< $metaclass->has_attribute($attribute_name) >>
1723 Returns a boolean indicating whether or not the class defines the
1724 named attribute. It does not include attributes inherited from parent
1727 =item B<< $metaclass->get_attribute_list >>
1729 This will return a list of attributes I<names> for all attributes
1730 defined in this class. Note that this operates on the current class
1731 only, it does not traverse the inheritance hierarchy.
1733 =item B<< $metaclass->get_all_attributes >>
1735 This will traverse the inheritance hierarchy and return a list of all
1736 the L<Class::MOP::Attribute> objects for this class and its parents.
1738 =item B<< $metaclass->find_attribute_by_name($attribute_name) >>
1740 This will return a L<Class::MOP::Attribute> for the specified
1741 C<$attribute_name>. If the class does not have the specified
1742 attribute, it returns C<undef>.
1744 Unlike C<get_attribute>, this attribute I<will> look for the named
1745 attribute in superclasses.
1747 =item B<< $metaclass->add_attribute(...) >>
1749 This method accepts either an existing L<Class::MOP::Attribute>
1750 object or parameters suitable for passing to that class's C<new>
1753 The attribute provided will be added to the class.
1755 Any accessor methods defined by the attribute will be added to the
1756 class when the attribute is added.
1758 If an attribute of the same name already exists, the old attribute
1759 will be removed first.
1761 =item B<< $metaclass->remove_attribute($attribute_name) >>
1763 This will remove the named attribute from the class, and
1764 L<Class::MOP::Attribute> object.
1766 Removing an attribute also removes any accessor methods defined by the
1769 However, note that removing an attribute will only affect I<future>
1770 object instances created for this class, not existing instances.
1772 =item B<< $metaclass->attribute_metaclass >>
1774 Returns the class name of the attribute metaclass for this class. By
1775 default, this is L<Class::MOP::Attribute>.
1779 =head2 Class Immutability
1781 Making a class immutable "freezes" the class definition. You can no
1782 longer call methods which alter the class, such as adding or removing
1783 methods or attributes.
1785 Making a class immutable lets us optimize the class by inlining some
1786 methods, and also allows us to optimize some methods on the metaclass
1789 After immutabilization, the metaclass object will cache most informational
1790 methods that returns information about methods or attributes. Methods which
1791 would alter the class, such as C<add_attribute> and C<add_method>, will
1792 throw an error on an immutable metaclass object.
1794 The immutabilization system in L<Moose> takes much greater advantage
1795 of the inlining features than Class::MOP itself does.
1799 =item B<< $metaclass->make_immutable(%options) >>
1801 This method will create an immutable transformer and use it to make
1802 the class and its metaclass object immutable.
1804 This method accepts the following options:
1808 =item * inline_accessors
1810 =item * inline_constructor
1812 =item * inline_destructor
1814 These are all booleans indicating whether the specified method(s)
1817 By default, accessors and the constructor are inlined, but not the
1820 =item * immutable_trait
1822 The name of a class which will be used as a parent class for the
1823 metaclass object being made immutable. This "trait" implements the
1824 post-immutability functionality of the metaclass (but not the
1825 transformation itself).
1827 This defaults to L<Class::MOP::Class::Immutable::Trait>.
1829 =item * constructor_name
1831 This is the constructor method name. This defaults to "new".
1833 =item * constructor_class
1835 The name of the method metaclass for constructors. It will be used to
1836 generate the inlined constructor. This defaults to
1837 "Class::MOP::Method::Constructor".
1839 =item * replace_constructor
1841 This is a boolean indicating whether an existing constructor should be
1842 replaced when inlining a constructor. This defaults to false.
1844 =item * destructor_class
1846 The name of the method metaclass for destructors. It will be used to
1847 generate the inlined destructor. This defaults to
1848 "Class::MOP::Method::Denstructor".
1850 =item * replace_destructor
1852 This is a boolean indicating whether an existing destructor should be
1853 replaced when inlining a destructor. This defaults to false.
1857 =item B<< $metaclass->immutable_options >>
1859 Returns a hash of the options used when making the class immutable, including
1860 both defaults and anything supplied by the user in the call to C<<
1861 $metaclass->make_immutable >>. This is useful if you need to temporarily make
1862 a class mutable and then restore immutability as it was before.
1864 =item B<< $metaclass->make_mutable >>
1866 Calling this method reverse the immutabilization transformation.
1870 =head2 Method Modifiers
1872 Method modifiers are hooks which allow a method to be wrapped with
1873 I<before>, I<after> and I<around> method modifiers. Every time a
1874 method is called, its modifiers are also called.
1876 A class can modify its own methods, as well as methods defined in
1879 =head3 How method modifiers work?
1881 Method modifiers work by wrapping the original method and then
1882 replacing it in the class's symbol table. The wrappers will handle
1883 calling all the modifiers in the appropriate order and preserving the
1884 calling context for the original method.
1886 The return values of C<before> and C<after> modifiers are
1887 ignored. This is because their purpose is B<not> to filter the input
1888 and output of the primary method (this is done with an I<around>
1891 This may seem like an odd restriction to some, but doing this allows
1892 for simple code to be added at the beginning or end of a method call
1893 without altering the function of the wrapped method or placing any
1894 extra responsibility on the code of the modifier.
1896 Of course if you have more complex needs, you can use the C<around>
1897 modifier which allows you to change both the parameters passed to the
1898 wrapped method, as well as its return value.
1900 Before and around modifiers are called in last-defined-first-called
1901 order, while after modifiers are called in first-defined-first-called
1902 order. So the call tree might looks something like this:
1914 =head3 What is the performance impact?
1916 Of course there is a performance cost associated with method
1917 modifiers, but we have made every effort to make that cost directly
1918 proportional to the number of modifier features you use.
1920 The wrapping method does its best to B<only> do as much work as it
1921 absolutely needs to. In order to do this we have moved some of the
1922 performance costs to set-up time, where they are easier to amortize.
1924 All this said, our benchmarks have indicated the following:
1926 simple wrapper with no modifiers 100% slower
1927 simple wrapper with simple before modifier 400% slower
1928 simple wrapper with simple after modifier 450% slower
1929 simple wrapper with simple around modifier 500-550% slower
1930 simple wrapper with all 3 modifiers 1100% slower
1932 These numbers may seem daunting, but you must remember, every feature
1933 comes with some cost. To put things in perspective, just doing a
1934 simple C<AUTOLOAD> which does nothing but extract the name of the
1935 method called and return it costs about 400% over a normal method
1940 =item B<< $metaclass->add_before_method_modifier($method_name, $code) >>
1942 This wraps the specified method with the supplied subroutine
1943 reference. The modifier will be called as a method itself, and will
1944 receive the same arguments as are passed to the method.
1946 When the modifier exits, the wrapped method will be called.
1948 The return value of the modifier will be ignored.
1950 =item B<< $metaclass->add_after_method_modifier($method_name, $code) >>
1952 This wraps the specified method with the supplied subroutine
1953 reference. The modifier will be called as a method itself, and will
1954 receive the same arguments as are passed to the method.
1956 When the wrapped methods exits, the modifier will be called.
1958 The return value of the modifier will be ignored.
1960 =item B<< $metaclass->add_around_method_modifier($method_name, $code) >>
1962 This wraps the specified method with the supplied subroutine
1965 The first argument passed to the modifier will be a subroutine
1966 reference to the wrapped method. The second argument is the object,
1967 and after that come any arguments passed when the method is called.
1969 The around modifier can choose to call the original method, as well as
1970 what arguments to pass if it does so.
1972 The return value of the modifier is what will be seen by the caller.
1976 =head2 Introspection
1980 =item B<< Class::MOP::Class->meta >>
1982 This will return a L<Class::MOP::Class> instance for this class.
1984 It should also be noted that L<Class::MOP> will actually bootstrap
1985 this module by installing a number of attribute meta-objects into its
1992 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1994 =head1 COPYRIGHT AND LICENSE
1996 Copyright 2006-2010 by Infinity Interactive, Inc.
1998 L<http://www.iinteractive.com>
2000 This library is free software; you can redistribute it and/or modify
2001 it under the same terms as Perl itself.