2 package Class::MOP::Class;
7 use Class::MOP::Instance;
8 use Class::MOP::Method::Wrapped;
9 use Class::MOP::Method::Accessor;
10 use Class::MOP::Method::Constructor;
13 use Scalar::Util 'blessed', 'reftype', 'weaken';
14 use Sub::Name 'subname';
15 use Devel::GlobalDestruction 'in_global_destruction';
17 use List::MoreUtils 'all';
19 our $VERSION = '1.01';
20 $VERSION = eval $VERSION;
21 our $AUTHORITY = 'cpan:STEVAN';
23 use base 'Class::MOP::Module',
24 'Class::MOP::Mixin::HasAttributes',
25 'Class::MOP::Mixin::HasMethods';
35 $package_name = shift;
38 $package_name = $options{package};
41 ($package_name && !ref($package_name))
42 || confess "You must pass a package name and it cannot be blessed";
44 return Class::MOP::get_metaclass_by_name($package_name)
45 || $class->_construct_class_instance(package => $package_name, @_);
48 # NOTE: (meta-circularity)
49 # this is a special form of _construct_instance
50 # (see below), which is used to construct class
51 # meta-object instances for any Class::MOP::*
52 # class. All other classes will use the more
53 # normal &construct_instance.
54 sub _construct_class_instance {
56 my $options = @_ == 1 ? $_[0] : {@_};
57 my $package_name = $options->{package};
58 (defined $package_name && $package_name)
59 || confess "You must pass a package name";
61 # return the metaclass if we have it cached,
62 # and it is still defined (it has not been
63 # reaped by DESTROY yet, which can happen
64 # annoyingly enough during global destruction)
66 if (defined(my $meta = Class::MOP::get_metaclass_by_name($package_name))) {
71 # we need to deal with the possibility
72 # of class immutability here, and then
73 # get the name of the class appropriately
75 ? ($class->is_immutable
76 ? $class->_get_mutable_metaclass_name()
80 # now create the metaclass
82 if ($class eq 'Class::MOP::Class') {
83 $meta = $class->_new($options);
87 # it is safe to use meta here because
88 # class will always be a subclass of
89 # Class::MOP::Class, which defines meta
90 $meta = $class->meta->_construct_instance($options)
93 # and check the metaclass compatibility
94 $meta->_check_metaclass_compatibility();
96 Class::MOP::store_metaclass_by_name($package_name, $meta);
99 # we need to weaken any anon classes
100 # so that they can call DESTROY properly
101 Class::MOP::weaken_metaclass($package_name) if $meta->is_anon_class;
109 return Class::MOP::Class->initialize($class)->new_object(@_)
110 if $class ne __PACKAGE__;
112 my $options = @_ == 1 ? $_[0] : {@_};
115 # inherited from Class::MOP::Package
116 'package' => $options->{package},
119 # since the following attributes will
120 # actually be loaded from the symbol
121 # table, and actually bypass the instance
122 # entirely, we can just leave these things
123 # listed here for reference, because they
124 # should not actually have a value associated
126 'namespace' => \undef,
129 # inherited from Class::MOP::Module
131 'authority' => \undef,
133 # defined in Class::MOP::Class
134 'superclasses' => \undef,
137 'attribute_metaclass' =>
138 ( $options->{'attribute_metaclass'} || 'Class::MOP::Attribute' ),
139 'method_metaclass' =>
140 ( $options->{'method_metaclass'} || 'Class::MOP::Method' ),
141 'wrapped_method_metaclass' => (
142 $options->{'wrapped_method_metaclass'}
143 || 'Class::MOP::Method::Wrapped'
145 'instance_metaclass' =>
146 ( $options->{'instance_metaclass'} || 'Class::MOP::Instance' ),
147 'immutable_trait' => (
148 $options->{'immutable_trait'}
149 || 'Class::MOP::Class::Immutable::Trait'
151 'constructor_name' => ( $options->{constructor_name} || 'new' ),
152 'constructor_class' => (
153 $options->{constructor_class} || 'Class::MOP::Method::Constructor'
155 'destructor_class' => $options->{destructor_class},
159 sub reset_package_cache_flag { (shift)->{'_package_cache_flag'} = undef }
160 sub update_package_cache_flag {
163 # we can manually update the cache number
164 # since we are actually adding the method
165 # to our cache as well. This avoids us
166 # having to regenerate the method_map.
168 $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);
171 ## Metaclass compatibility
173 my %base_metaclass = (
174 attribute_metaclass => 'Class::MOP::Attribute',
175 method_metaclass => 'Class::MOP::Method',
176 wrapped_method_metaclass => 'Class::MOP::Method::Wrapped',
177 instance_metaclass => 'Class::MOP::Instance',
178 constructor_class => 'Class::MOP::Method::Constructor',
179 destructor_class => 'Class::MOP::Method::Destructor',
182 sub _base_metaclasses { %base_metaclass }
185 sub _check_metaclass_compatibility {
188 if (my @superclasses = $self->superclasses) {
189 $self->_fix_metaclass_incompatibility(@superclasses);
191 my %base_metaclass = $self->_base_metaclasses;
193 # this is always okay ...
194 return if ref($self) eq 'Class::MOP::Class'
196 my $meta = $self->$_;
197 !defined($meta) || $meta eq $base_metaclass{$_}
198 } keys %base_metaclass;
200 for my $superclass (@superclasses) {
201 $self->_check_class_metaclass_compatibility($superclass);
204 for my $metaclass_type (keys %base_metaclass) {
205 next unless defined $self->$metaclass_type;
206 for my $superclass (@superclasses) {
207 $self->_check_single_metaclass_compatibility(
208 $metaclass_type, $superclass
215 sub _class_metaclass_is_compatible {
217 my ( $superclass_name ) = @_;
219 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
223 # we need to deal with the possibility
224 # of class immutability here, and then
225 # get the name of the class appropriately
227 = $super_meta->is_immutable
228 ? $super_meta->_get_mutable_metaclass_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);
242 # we need to deal with the possibility
243 # of class immutability here, and then
244 # get the name of the class appropriately
246 = $super_meta->is_immutable
247 ? $super_meta->_get_mutable_metaclass_name()
250 confess "The metaclass of " . $self->name . " ("
251 . (ref($self)) . ")" . " is not compatible with "
252 . "the metaclass of its superclass, "
253 . $superclass_name . " (" . ($super_meta_type) . ")";
257 sub _single_metaclass_is_compatible {
259 my ( $metaclass_type, $superclass_name ) = @_;
261 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
264 # for instance, Moose::Meta::Class has a error_class attribute, but
265 # Class::MOP::Class doesn't - this shouldn't be an error
266 return 1 unless $super_meta->can($metaclass_type);
267 # for instance, Moose::Meta::Class has a destructor_class, but
268 # Class::MOP::Class doesn't - this shouldn't be an error
269 return 1 if defined $self->$metaclass_type
270 && !defined $super_meta->$metaclass_type;
272 return $self->$metaclass_type->isa($super_meta->$metaclass_type);
275 sub _check_single_metaclass_compatibility {
277 my ( $metaclass_type, $superclass_name ) = @_;
279 if (!$self->_single_metaclass_is_compatible($metaclass_type, $superclass_name)) {
280 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
281 my $metaclass_type_name = $metaclass_type;
282 $metaclass_type_name =~ s/_(?:meta)?class$//;
283 $metaclass_type_name =~ s/_/ /g;
284 confess "The $metaclass_type_name metaclass for "
285 . $self->name . " (" . ($self->$metaclass_type)
286 . ")" . " is not compatible with the "
287 . "$metaclass_type_name metaclass of its "
288 . "superclass, " . $superclass_name . " ("
289 . ($super_meta->$metaclass_type) . ")";
293 sub _can_fix_class_metaclass_incompatibility_by_subclassing {
295 my ($super_meta) = @_;
298 # we need to deal with the possibility
299 # of class immutability here, and then
300 # get the name of the class appropriately
302 = $super_meta->is_immutable
303 ? $super_meta->_get_mutable_metaclass_name()
306 return $super_meta_type ne blessed($self)
307 && $super_meta->isa(blessed($self));
310 sub _can_fix_single_metaclass_incompatibility_by_subclassing {
312 my ($metaclass_type, $super_meta) = @_;
314 my $specific_meta = $self->$metaclass_type;
315 return unless $super_meta->can($metaclass_type);
316 my $super_specific_meta = $super_meta->$metaclass_type;
318 # for instance, Moose::Meta::Class has a destructor_class, but
319 # Class::MOP::Class doesn't - this shouldn't be an error
320 return if defined $specific_meta
321 && !defined $super_specific_meta;
323 return $specific_meta ne $super_specific_meta
324 && $super_specific_meta->isa($specific_meta);
327 sub _can_fix_metaclass_incompatibility_by_subclassing {
329 my ($super_meta) = @_;
331 return 1 if $self->_can_fix_class_metaclass_incompatibility_by_subclassing($super_meta);
333 my %base_metaclass = $self->_base_metaclasses;
334 for my $metaclass_type (keys %base_metaclass) {
335 next unless defined $self->$metaclass_type;
336 return 1 if $self->_can_fix_single_metaclass_incompatibility_by_subclassing($metaclass_type, $super_meta);
342 sub _can_fix_metaclass_incompatibility {
344 return $self->_can_fix_metaclass_incompatibility_by_subclassing(@_);
347 sub _fix_metaclass_incompatibility {
352 for my $super (map { Class::MOP::Class->initialize($_) } @supers) {
354 if $self->_can_fix_metaclass_incompatibility($super);
356 return unless $necessary;
359 || confess "Can't fix metaclass incompatibility for "
361 . " because it is not pristine.";
363 for my $super (map { Class::MOP::Class->initialize($_) } @supers) {
364 if (!$self->_class_metaclass_is_compatible($super->name)) {
365 $self->_fix_class_metaclass_incompatibility($super);
369 my %base_metaclass = $self->_base_metaclasses;
370 for my $metaclass_type (keys %base_metaclass) {
371 next unless defined $self->$metaclass_type;
372 for my $super (map { Class::MOP::Class->initialize($_) } @supers) {
373 if (!$self->_single_metaclass_is_compatible($metaclass_type, $super->name)) {
374 $self->_fix_single_metaclass_incompatibility(
375 $metaclass_type, $super
382 sub _fix_class_metaclass_incompatibility {
384 my ( $super_meta ) = @_;
386 if ($self->_can_fix_class_metaclass_incompatibility_by_subclassing($super_meta)) {
387 my $super_meta_name = $super_meta->is_immutable
388 ? $super_meta->_get_mutable_metaclass_name
389 : blessed($super_meta);
390 $super_meta_name->meta->rebless_instance($self);
394 sub _fix_single_metaclass_incompatibility {
396 my ( $metaclass_type, $super_meta ) = @_;
398 if ($self->_can_fix_single_metaclass_incompatibility_by_subclassing($metaclass_type, $super_meta)) {
399 $self->{$metaclass_type} = $super_meta->$metaclass_type;
407 # this should be sufficient, if you have a
408 # use case where it is not, write a test and
410 my $ANON_CLASS_SERIAL = 0;
413 # we need a sufficiently annoying prefix
414 # this should suffice for now, this is
415 # used in a couple of places below, so
416 # need to put it up here for now.
417 my $ANON_CLASS_PREFIX = 'Class::MOP::Class::__ANON__::SERIAL::';
421 no warnings 'uninitialized';
422 $self->name =~ /^$ANON_CLASS_PREFIX/o;
425 sub create_anon_class {
426 my ($class, %options) = @_;
427 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
428 return $class->create($package_name, %options);
432 # this will only get called for
433 # anon-classes, all other calls
434 # are assumed to occur during
435 # global destruction and so don't
436 # really need to be handled explicitly
440 return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated
442 no warnings 'uninitialized';
443 my $name = $self->name;
444 return unless $name =~ /^$ANON_CLASS_PREFIX/o;
446 # Moose does a weird thing where it replaces the metaclass for
447 # class when fixing metaclass incompatibility. In that case,
448 # we don't want to clean out the namespace now. We can detect
449 # that because Moose will explicitly update the singleton
450 # cache in Class::MOP.
451 my $current_meta = Class::MOP::get_metaclass_by_name($name);
452 return if $current_meta ne $self;
454 my ($serial_id) = ($name =~ /^$ANON_CLASS_PREFIX(\d+)/o);
456 @{$name . '::ISA'} = ();
457 %{$name . '::'} = ();
458 delete ${$ANON_CLASS_PREFIX}{$serial_id . '::'};
460 Class::MOP::remove_metaclass_by_name($name);
465 # creating classes with MOP ...
468 my ( $class, @args ) = @_;
470 unshift @args, 'package' if @args % 2 == 1;
472 my (%options) = @args;
473 my $package_name = $options{package};
475 (ref $options{superclasses} eq 'ARRAY')
476 || confess "You must pass an ARRAY ref of superclasses"
477 if exists $options{superclasses};
479 (ref $options{attributes} eq 'ARRAY')
480 || confess "You must pass an ARRAY ref of attributes"
481 if exists $options{attributes};
483 (ref $options{methods} eq 'HASH')
484 || confess "You must pass a HASH ref of methods"
485 if exists $options{methods};
487 my (%initialize_options) = @args;
488 delete @initialize_options{qw(
496 my $meta = $class->initialize( $package_name => %initialize_options );
498 $meta->_instantiate_module( $options{version}, $options{authority} );
501 $meta->add_method('meta' => sub {
502 $class->initialize(ref($_[0]) || $_[0]);
505 $meta->superclasses(@{$options{superclasses}})
506 if exists $options{superclasses};
508 # process attributes first, so that they can
509 # install accessors, but locally defined methods
510 # can then overwrite them. It is maybe a little odd, but
511 # I think this should be the order of things.
512 if (exists $options{attributes}) {
513 foreach my $attr (@{$options{attributes}}) {
514 $meta->add_attribute($attr);
517 if (exists $options{methods}) {
518 foreach my $method_name (keys %{$options{methods}}) {
519 $meta->add_method($method_name, $options{methods}->{$method_name});
528 # all these attribute readers will be bootstrapped
529 # away in the Class::MOP bootstrap section
531 sub instance_metaclass { $_[0]->{'instance_metaclass'} }
532 sub immutable_trait { $_[0]->{'immutable_trait'} }
533 sub constructor_class { $_[0]->{'constructor_class'} }
534 sub constructor_name { $_[0]->{'constructor_name'} }
535 sub destructor_class { $_[0]->{'destructor_class'} }
537 # Instance Construction & Cloning
543 # we need to protect the integrity of the
544 # Class::MOP::Class singletons here, so we
545 # delegate this to &construct_class_instance
546 # which will deal with the singletons
547 return $class->_construct_class_instance(@_)
548 if $class->name->isa('Class::MOP::Class');
549 return $class->_construct_instance(@_);
552 sub _construct_instance {
554 my $params = @_ == 1 ? $_[0] : {@_};
555 my $meta_instance = $class->get_meta_instance();
557 # the code below is almost certainly incorrect
558 # but this is foreign inheritance, so we might
559 # have to kludge it in the end.
561 if (my $instance_class = blessed($params->{__INSTANCE__})) {
562 ($instance_class eq $class->name)
563 || confess "Objects passed as the __INSTANCE__ parameter must "
564 . "already be blessed into the correct class, but "
565 . "$params->{__INSTANCE__} is not a " . $class->name;
566 $instance = $params->{__INSTANCE__};
568 elsif (exists $params->{__INSTANCE__}) {
569 confess "The __INSTANCE__ parameter must be a blessed reference, not "
570 . $params->{__INSTANCE__};
573 $instance = $meta_instance->create_instance();
575 foreach my $attr ($class->get_all_attributes()) {
576 $attr->initialize_instance_slot($meta_instance, $instance, $params);
579 # this will only work for a HASH instance type
580 if ($class->is_anon_class) {
581 (reftype($instance) eq 'HASH')
582 || confess "Currently only HASH based instances are supported with instance of anon-classes";
584 # At some point we should make this official
585 # as a reserved slot name, but right now I am
586 # going to keep it here.
587 # my $RESERVED_MOP_SLOT = '__MOP__';
588 $instance->{'__MOP__'} = $class;
594 sub get_meta_instance {
596 $self->{'_meta_instance'} ||= $self->_create_meta_instance();
599 sub _create_meta_instance {
602 my $instance = $self->instance_metaclass->new(
603 associated_metaclass => $self,
604 attributes => [ $self->get_all_attributes() ],
607 $self->add_meta_instance_dependencies()
608 if $instance->is_dependent_on_superclasses();
615 my $instance = shift;
616 (blessed($instance) && $instance->isa($class->name))
617 || confess "You must pass an instance of the metaclass (" . (ref $class ? $class->name : $class) . "), not ($instance)";
620 # we need to protect the integrity of the
621 # Class::MOP::Class singletons here, they
622 # should not be cloned.
623 return $instance if $instance->isa('Class::MOP::Class');
624 $class->_clone_instance($instance, @_);
627 sub _clone_instance {
628 my ($class, $instance, %params) = @_;
630 || confess "You can only clone instances, ($instance) is not a blessed instance";
631 my $meta_instance = $class->get_meta_instance();
632 my $clone = $meta_instance->clone_instance($instance);
633 foreach my $attr ($class->get_all_attributes()) {
634 if ( defined( my $init_arg = $attr->init_arg ) ) {
635 if (exists $params{$init_arg}) {
636 $attr->set_value($clone, $params{$init_arg});
643 sub rebless_instance {
644 my ($self, $instance, %params) = @_;
646 my $old_metaclass = Class::MOP::class_of($instance);
648 my $old_class = $old_metaclass ? $old_metaclass->name : blessed($instance);
649 $self->name->isa($old_class)
650 || confess "You may rebless only into a subclass of ($old_class), of which (". $self->name .") isn't.";
652 $old_metaclass->rebless_instance_away($instance, $self, %params)
655 my $meta_instance = $self->get_meta_instance();
658 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
659 $meta_instance->rebless_instance_structure($_[1], $self);
661 foreach my $attr ( $self->get_all_attributes ) {
662 if ( $attr->has_value($instance) ) {
663 if ( defined( my $init_arg = $attr->init_arg ) ) {
664 $params{$init_arg} = $attr->get_value($instance)
665 unless exists $params{$init_arg};
668 $attr->set_value($instance, $attr->get_value($instance));
673 foreach my $attr ($self->get_all_attributes) {
674 $attr->initialize_instance_slot($meta_instance, $instance, \%params);
680 sub rebless_instance_back {
681 my ( $self, $instance ) = @_;
683 my $old_metaclass = Class::MOP::class_of($instance);
686 = $old_metaclass ? $old_metaclass->name : blessed($instance);
687 $old_class->isa( $self->name )
689 "You may rebless only into a superclass of ($old_class), of which ("
693 $old_metaclass->rebless_instance_away( $instance, $self )
696 my $meta_instance = $self->get_meta_instance;
698 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
699 $meta_instance->rebless_instance_structure( $_[1], $self );
701 for my $attr ( $old_metaclass->get_all_attributes ) {
702 next if $self->has_attribute( $attr->name );
703 $meta_instance->deinitialize_slot( $instance, $_ ) for $attr->slots;
709 sub rebless_instance_away {
710 # this intentionally does nothing, it is just a hook
713 sub _attach_attribute {
714 my ($self, $attribute) = @_;
715 $attribute->attach_to_class($self);
718 sub _post_add_attribute {
719 my ( $self, $attribute ) = @_;
721 $self->invalidate_meta_instances;
723 # invalidate package flag here
726 $attribute->install_accessors;
729 $self->remove_attribute( $attribute->name );
734 sub remove_attribute {
737 my $removed_attribute = $self->SUPER::remove_attribute(@_)
740 $self->invalidate_meta_instances;
742 $removed_attribute->remove_accessors;
743 $removed_attribute->detach_from_class;
745 return$removed_attribute;
748 sub find_attribute_by_name {
749 my ( $self, $attr_name ) = @_;
751 foreach my $class ( $self->linearized_isa ) {
752 # fetch the meta-class ...
753 my $meta = Class::MOP::Class->initialize($class);
754 return $meta->get_attribute($attr_name)
755 if $meta->has_attribute($attr_name);
761 sub get_all_attributes {
763 my %attrs = map { %{ Class::MOP::Class->initialize($_)->_attribute_map } }
764 reverse $self->linearized_isa;
765 return values %attrs;
772 my $var_spec = { sigil => '@', type => 'ARRAY', name => 'ISA' };
775 @{$self->get_package_symbol($var_spec)} = @supers;
778 # on 5.8 and below, we need to call
779 # a method to get Perl to detect
780 # a cycle in the class hierarchy
781 my $class = $self->name;
785 # we need to check the metaclass
786 # compatibility here so that we can
787 # be sure that the superclass is
788 # not potentially creating an issues
789 # we don't know about
791 $self->_check_metaclass_compatibility();
792 $self->_superclasses_updated();
794 @{$self->get_package_symbol($var_spec)};
797 sub _superclasses_updated {
799 $self->update_meta_instance_dependencies();
804 my $super_class = $self->name;
806 return @{ $super_class->mro::get_isarev() };
809 sub direct_subclasses {
811 my $super_class = $self->name;
816 } Class::MOP::Class->initialize($_)->superclasses
821 return @{ mro::get_linear_isa( (shift)->name ) };
824 sub class_precedence_list {
826 my $name = $self->name;
828 unless (Class::MOP::IS_RUNNING_ON_5_10()) {
830 # We need to check for circular inheritance here
831 # if we are are not on 5.10, cause 5.8 detects it
832 # late. This will do nothing if all is well, and
833 # blow up otherwise. Yes, it's an ugly hack, better
834 # suggestions are welcome.
836 ($name || return)->isa('This is a test for circular inheritance')
839 # if our mro is c3, we can
840 # just grab the linear_isa
841 if (mro::get_mro($name) eq 'c3') {
842 return @{ mro::get_linear_isa($name) }
846 # we can't grab the linear_isa for dfs
847 # since it has all the duplicates
852 Class::MOP::Class->initialize($_)->class_precedence_list()
853 } $self->superclasses()
861 my $fetch_and_prepare_method = sub {
862 my ($self, $method_name) = @_;
863 my $wrapped_metaclass = $self->wrapped_method_metaclass;
865 my $method = $self->get_method($method_name);
866 # if we dont have local ...
868 # try to find the next method
869 $method = $self->find_next_method_by_name($method_name);
870 # die if it does not exist
872 || confess "The method '$method_name' was not found in the inheritance hierarchy for " . $self->name;
873 # and now make sure to wrap it
874 # even if it is already wrapped
875 # because we need a new sub ref
876 $method = $wrapped_metaclass->wrap($method,
877 package_name => $self->name,
878 name => $method_name,
882 # now make sure we wrap it properly
883 $method = $wrapped_metaclass->wrap($method,
884 package_name => $self->name,
885 name => $method_name,
886 ) unless $method->isa($wrapped_metaclass);
888 $self->add_method($method_name => $method);
892 sub add_before_method_modifier {
893 my ($self, $method_name, $method_modifier) = @_;
894 (defined $method_name && length $method_name)
895 || confess "You must pass in a method name";
896 my $method = $fetch_and_prepare_method->($self, $method_name);
897 $method->add_before_modifier(
898 subname(':before' => $method_modifier)
902 sub add_after_method_modifier {
903 my ($self, $method_name, $method_modifier) = @_;
904 (defined $method_name && length $method_name)
905 || confess "You must pass in a method name";
906 my $method = $fetch_and_prepare_method->($self, $method_name);
907 $method->add_after_modifier(
908 subname(':after' => $method_modifier)
912 sub add_around_method_modifier {
913 my ($self, $method_name, $method_modifier) = @_;
914 (defined $method_name && length $method_name)
915 || confess "You must pass in a method name";
916 my $method = $fetch_and_prepare_method->($self, $method_name);
917 $method->add_around_modifier(
918 subname(':around' => $method_modifier)
923 # the methods above used to be named like this:
924 # ${pkg}::${method}:(before|after|around)
925 # but this proved problematic when using one modifier
926 # to wrap multiple methods (something which is likely
927 # to happen pretty regularly IMO). So instead of naming
928 # it like this, I have chosen to just name them purely
929 # with their modifier names, like so:
930 # :(before|after|around)
931 # The fact is that in a stack trace, it will be fairly
932 # evident from the context what method they are attached
933 # to, and so don't need the fully qualified name.
936 sub find_method_by_name {
937 my ($self, $method_name) = @_;
938 (defined $method_name && length $method_name)
939 || confess "You must define a method name to find";
940 foreach my $class ($self->linearized_isa) {
941 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
942 return $method if defined $method;
947 sub get_all_methods {
951 for my $class ( reverse $self->linearized_isa ) {
952 my $meta = Class::MOP::Class->initialize($class);
954 $methods{$_} = $meta->get_method($_)
955 for $meta->get_method_list;
958 return values %methods;
961 sub get_all_method_names {
964 return grep { !$uniq{$_}++ } map { Class::MOP::Class->initialize($_)->get_method_list } $self->linearized_isa;
967 sub find_all_methods_by_name {
968 my ($self, $method_name) = @_;
969 (defined $method_name && length $method_name)
970 || confess "You must define a method name to find";
972 foreach my $class ($self->linearized_isa) {
973 # fetch the meta-class ...
974 my $meta = Class::MOP::Class->initialize($class);
976 name => $method_name,
978 code => $meta->get_method($method_name)
979 } if $meta->has_method($method_name);
984 sub find_next_method_by_name {
985 my ($self, $method_name) = @_;
986 (defined $method_name && length $method_name)
987 || confess "You must define a method name to find";
988 my @cpl = $self->linearized_isa;
989 shift @cpl; # discard ourselves
990 foreach my $class (@cpl) {
991 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
992 return $method if defined $method;
997 sub update_meta_instance_dependencies {
1000 if ( $self->{meta_instance_dependencies} ) {
1001 return $self->add_meta_instance_dependencies;
1005 sub add_meta_instance_dependencies {
1008 $self->remove_meta_instance_dependencies;
1010 my @attrs = $self->get_all_attributes();
1013 my @classes = grep { not $seen{ $_->name }++ }
1014 map { $_->associated_class } @attrs;
1016 foreach my $class (@classes) {
1017 $class->add_dependent_meta_instance($self);
1020 $self->{meta_instance_dependencies} = \@classes;
1023 sub remove_meta_instance_dependencies {
1026 if ( my $classes = delete $self->{meta_instance_dependencies} ) {
1027 foreach my $class (@$classes) {
1028 $class->remove_dependent_meta_instance($self);
1038 sub add_dependent_meta_instance {
1039 my ( $self, $metaclass ) = @_;
1040 push @{ $self->{dependent_meta_instances} }, $metaclass;
1043 sub remove_dependent_meta_instance {
1044 my ( $self, $metaclass ) = @_;
1045 my $name = $metaclass->name;
1046 @$_ = grep { $_->name ne $name } @$_
1047 for $self->{dependent_meta_instances};
1050 sub invalidate_meta_instances {
1052 $_->invalidate_meta_instance()
1053 for $self, @{ $self->{dependent_meta_instances} };
1056 sub invalidate_meta_instance {
1058 undef $self->{_meta_instance};
1061 # check if we can reinitialize
1065 # if any local attr is defined
1066 return if $self->get_attribute_list;
1068 # or any non-declared methods
1069 for my $method ( map { $self->get_method($_) } $self->get_method_list ) {
1070 return if $method->isa("Class::MOP::Method::Generated");
1071 # FIXME do we need to enforce this too? return unless $method->isa( $self->method_metaclass );
1079 sub is_mutable { 1 }
1080 sub is_immutable { 0 }
1082 sub immutable_options { %{ $_[0]{__immutable}{options} || {} } }
1084 sub _immutable_options {
1085 my ( $self, @args ) = @_;
1088 inline_accessors => 1,
1089 inline_constructor => 1,
1090 inline_destructor => 0,
1092 immutable_trait => $self->immutable_trait,
1093 constructor_name => $self->constructor_name,
1094 constructor_class => $self->constructor_class,
1095 destructor_class => $self->destructor_class,
1100 sub make_immutable {
1101 my ( $self, @args ) = @_;
1103 if ( $self->is_mutable ) {
1104 $self->_initialize_immutable( $self->_immutable_options(@args) );
1105 $self->_rebless_as_immutable(@args);
1116 if ( $self->is_immutable ) {
1117 my @args = $self->immutable_options;
1118 $self->_rebless_as_mutable();
1119 $self->_remove_inlined_code(@args);
1120 delete $self->{__immutable};
1128 sub _rebless_as_immutable {
1129 my ( $self, @args ) = @_;
1131 $self->{__immutable}{original_class} = ref $self;
1133 bless $self => $self->_immutable_metaclass(@args);
1136 sub _immutable_metaclass {
1137 my ( $self, %args ) = @_;
1139 if ( my $class = $args{immutable_metaclass} ) {
1143 my $trait = $args{immutable_trait} = $self->immutable_trait
1144 || confess "no immutable trait specified for $self";
1146 my $meta = $self->meta;
1147 my $meta_attr = $meta->find_attribute_by_name("immutable_trait");
1151 if ( $meta_attr and $trait eq $meta_attr->default ) {
1152 # if the trait is the same as the default we try and pick a
1153 # predictable name for the immutable metaclass
1154 $class_name = 'Class::MOP::Class::Immutable::' . ref($self);
1157 $class_name = join '::', 'Class::MOP::Class::Immutable::CustomTrait',
1158 $trait, 'ForMetaClass', ref($self);
1162 if Class::MOP::is_class_loaded($class_name);
1164 # If the metaclass is a subclass of CMOP::Class which has had
1165 # metaclass roles applied (via Moose), then we want to make sure
1166 # that we preserve that anonymous class (see Fey::ORM for an
1167 # example of where this matters).
1169 = $meta->is_immutable
1170 ? $meta->_get_mutable_metaclass_name
1173 my $immutable_meta = $meta_name->create(
1175 superclasses => [ ref $self ],
1178 Class::MOP::load_class($trait);
1179 for my $meth ( Class::MOP::Class->initialize($trait)->get_all_methods ) {
1180 my $meth_name = $meth->name;
1182 if ( $immutable_meta->find_method_by_name( $meth_name ) ) {
1183 $immutable_meta->add_around_method_modifier( $meth_name, $meth->body );
1186 $immutable_meta->add_method( $meth_name, $meth->clone );
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 =item B<< Class::MOP::Class->create_anon_class(%options) >>
1416 This method works just like C<< Class::MOP::Class->create >> but it
1417 creates an "anonymous" class. In fact, the class does have a name, but
1418 that name is a unique name generated internally by this module.
1420 It accepts the same C<superclasses>, C<methods>, and C<attributes>
1421 parameters that C<create> accepts.
1423 Anonymous classes are destroyed once the metaclass they are attached
1424 to goes out of scope, and will be removed from Perl's internal symbol
1427 All instances of an anonymous class keep a special reference to the
1428 metaclass object, which prevents the metaclass from going out of scope
1429 while any instances exist.
1431 This only works if the instance is based on a hash reference, however.
1433 =item B<< Class::MOP::Class->initialize($package_name, %options) >>
1435 This method will initialize a C<Class::MOP::Class> object for the
1436 named package. Unlike C<create>, this method I<will not> create a new
1439 The purpose of this method is to retrieve a C<Class::MOP::Class>
1440 object for introspecting an existing class.
1442 If an existing C<Class::MOP::Class> object exists for the named
1443 package, it will be returned, and any options provided will be
1446 If the object does not yet exist, it will be created.
1448 The valid options that can be passed to this method are
1449 C<attribute_metaclass>, C<method_metaclass>,
1450 C<wrapped_method_metaclass>, and C<instance_metaclass>. These are all
1451 optional, and default to the appropriate class in the C<Class::MOP>
1456 =head2 Object instance construction and cloning
1458 These methods are all related to creating and/or cloning object
1463 =item B<< $metaclass->clone_object($instance, %params) >>
1465 This method clones an existing object instance. Any parameters you
1466 provide are will override existing attribute values in the object.
1468 This is a convenience method for cloning an object instance, then
1469 blessing it into the appropriate package.
1471 You could implement a clone method in your class, using this method:
1474 my ($self, %params) = @_;
1475 $self->meta->clone_object($self, %params);
1478 =item B<< $metaclass->rebless_instance($instance, %params) >>
1480 This method changes the class of C<$instance> to the metaclass's class.
1482 You can only rebless an instance into a subclass of its current
1483 class. If you pass any additional parameters, these will be treated
1484 like constructor parameters and used to initialize the object's
1485 attributes. Any existing attributes that are already set will be
1488 Before reblessing the instance, this method will call
1489 C<rebless_instance_away> on the instance's current metaclass. This method
1490 will be passed the instance, the new metaclass, and any parameters
1491 specified to C<rebless_instance>. By default, C<rebless_instance_away>
1492 does nothing; it is merely a hook.
1494 =item B<< $metaclass->rebless_instance_back($instance) >>
1496 Does the same thing as C<rebless_instance>, except that you can only
1497 rebless an instance into one of its superclasses. Any attributes that
1498 do not exist in the superclass will be deinitialized.
1500 This is a much more dangerous operation than C<rebless_instance>,
1501 especially when multiple inheritance is involved, so use this carefully!
1503 =item B<< $metaclass->new_object(%params) >>
1505 This method is used to create a new object of the metaclass's
1506 class. Any parameters you provide are used to initialize the
1507 instance's attributes. A special C<__INSTANCE__> key can be passed to
1508 provide an already generated instance, rather than having Class::MOP
1509 generate it for you. This is mostly useful for using Class::MOP with
1510 foreign classes which generate instances using their own constructors.
1512 =item B<< $metaclass->instance_metaclass >>
1514 Returns the class name of the instance metaclass. See
1515 L<Class::MOP::Instance> for more information on the instance
1518 =item B<< $metaclass->get_meta_instance >>
1520 Returns an instance of the C<instance_metaclass> to be used in the
1521 construction of a new instance of the class.
1525 =head2 Informational predicates
1527 These are a few predicate methods for asking information about the
1532 =item B<< $metaclass->is_anon_class >>
1534 This returns true if the class was created by calling C<<
1535 Class::MOP::Class->create_anon_class >>.
1537 =item B<< $metaclass->is_mutable >>
1539 This returns true if the class is still mutable.
1541 =item B<< $metaclass->is_immutable >>
1543 This returns true if the class has been made immutable.
1545 =item B<< $metaclass->is_pristine >>
1547 A class is I<not> pristine if it has non-inherited attributes or if it
1548 has any generated methods.
1552 =head2 Inheritance Relationships
1556 =item B<< $metaclass->superclasses(@superclasses) >>
1558 This is a read-write accessor which represents the superclass
1559 relationships of the metaclass's class.
1561 This is basically sugar around getting and setting C<@ISA>.
1563 =item B<< $metaclass->class_precedence_list >>
1565 This returns a list of all of the class's ancestor classes. The
1566 classes are returned in method dispatch order.
1568 =item B<< $metaclass->linearized_isa >>
1570 This returns a list based on C<class_precedence_list> but with all
1573 =item B<< $metaclass->subclasses >>
1575 This returns a list of all subclasses for this class, even indirect
1578 =item B<< $metaclass->direct_subclasses >>
1580 This returns a list of immediate subclasses for this class, which does not
1581 include indirect subclasses.
1585 =head2 Method introspection and creation
1587 These methods allow you to introspect a class's methods, as well as
1588 add, remove, or change methods.
1590 Determining what is truly a method in a Perl 5 class requires some
1591 heuristics (aka guessing).
1593 Methods defined outside the package with a fully qualified name (C<sub
1594 Package::name { ... }>) will be included. Similarly, methods named
1595 with a fully qualified name using L<Sub::Name> are also included.
1597 However, we attempt to ignore imported functions.
1599 Ultimately, we are using heuristics to determine what truly is a
1600 method in a class, and these heuristics may get the wrong answer in
1601 some edge cases. However, for most "normal" cases the heuristics work
1606 =item B<< $metaclass->get_method($method_name) >>
1608 This will return a L<Class::MOP::Method> for the specified
1609 C<$method_name>. If the class does not have the specified method, it
1612 =item B<< $metaclass->has_method($method_name) >>
1614 Returns a boolean indicating whether or not the class defines the
1615 named method. It does not include methods inherited from parent
1618 =item B<< $metaclass->get_method_list >>
1620 This will return a list of method I<names> for all methods defined in
1623 =item B<< $metaclass->add_method($method_name, $method) >>
1625 This method takes a method name and a subroutine reference, and adds
1626 the method to the class.
1628 The subroutine reference can be a L<Class::MOP::Method>, and you are
1629 strongly encouraged to pass a meta method object instead of a code
1630 reference. If you do so, that object gets stored as part of the
1631 class's method map directly. If not, the meta information will have to
1632 be recreated later, and may be incorrect.
1634 If you provide a method object, this method will clone that object if
1635 the object's package name does not match the class name. This lets us
1636 track the original source of any methods added from other classes
1637 (notably Moose roles).
1639 =item B<< $metaclass->remove_method($method_name) >>
1641 Remove the named method from the class. This method returns the
1642 L<Class::MOP::Method> object for the method.
1644 =item B<< $metaclass->method_metaclass >>
1646 Returns the class name of the method metaclass, see
1647 L<Class::MOP::Method> for more information on the method metaclass.
1649 =item B<< $metaclass->wrapped_method_metaclass >>
1651 Returns the class name of the wrapped method metaclass, see
1652 L<Class::MOP::Method::Wrapped> for more information on the wrapped
1655 =item B<< $metaclass->get_all_methods >>
1657 This will traverse the inheritance hierarchy and return a list of all
1658 the L<Class::MOP::Method> objects for this class and its parents.
1660 =item B<< $metaclass->find_method_by_name($method_name) >>
1662 This will return a L<Class::MOP::Method> for the specified
1663 C<$method_name>. If the class does not have the specified method, it
1666 Unlike C<get_method>, this method I<will> look for the named method in
1669 =item B<< $metaclass->get_all_method_names >>
1671 This will return a list of method I<names> for all of this class's
1672 methods, including inherited methods.
1674 =item B<< $metaclass->find_all_methods_by_name($method_name) >>
1676 This method looks for the named method in the class and all of its
1677 parents. It returns every matching method it finds in the inheritance
1678 tree, so it returns a list of methods.
1680 Each method is returned as a hash reference with three keys. The keys
1681 are C<name>, C<class>, and C<code>. The C<code> key has a
1682 L<Class::MOP::Method> object as its value.
1684 The list of methods is distinct.
1686 =item B<< $metaclass->find_next_method_by_name($method_name) >>
1688 This method returns the first method in any superclass matching the
1689 given name. It is effectively the method that C<SUPER::$method_name>
1694 =head2 Attribute introspection and creation
1696 Because Perl 5 does not have a core concept of attributes in classes,
1697 we can only return information about attributes which have been added
1698 via this class's methods. We cannot discover information about
1699 attributes which are defined in terms of "regular" Perl 5 methods.
1703 =item B<< $metaclass->get_attribute($attribute_name) >>
1705 This will return a L<Class::MOP::Attribute> for the specified
1706 C<$attribute_name>. If the class does not have the specified
1707 attribute, it returns C<undef>.
1709 NOTE that get_attribute does not search superclasses, for that you
1710 need to use C<find_attribute_by_name>.
1712 =item B<< $metaclass->has_attribute($attribute_name) >>
1714 Returns a boolean indicating whether or not the class defines the
1715 named attribute. It does not include attributes inherited from parent
1718 =item B<< $metaclass->get_attribute_list >>
1720 This will return a list of attributes I<names> for all attributes
1721 defined in this class.
1723 =item B<< $metaclass->get_all_attributes >>
1725 This will traverse the inheritance hierarchy and return a list of all
1726 the L<Class::MOP::Attribute> objects for this class and its parents.
1728 =item B<< $metaclass->find_attribute_by_name($attribute_name) >>
1730 This will return a L<Class::MOP::Attribute> for the specified
1731 C<$attribute_name>. If the class does not have the specified
1732 attribute, it returns C<undef>.
1734 Unlike C<get_attribute>, this attribute I<will> look for the named
1735 attribute in superclasses.
1737 =item B<< $metaclass->add_attribute(...) >>
1739 This method accepts either an existing L<Class::MOP::Attribute>
1740 object or parameters suitable for passing to that class's C<new>
1743 The attribute provided will be added to the class.
1745 Any accessor methods defined by the attribute will be added to the
1746 class when the attribute is added.
1748 If an attribute of the same name already exists, the old attribute
1749 will be removed first.
1751 =item B<< $metaclass->remove_attribute($attribute_name) >>
1753 This will remove the named attribute from the class, and
1754 L<Class::MOP::Attribute> object.
1756 Removing an attribute also removes any accessor methods defined by the
1759 However, note that removing an attribute will only affect I<future>
1760 object instances created for this class, not existing instances.
1762 =item B<< $metaclass->attribute_metaclass >>
1764 Returns the class name of the attribute metaclass for this class. By
1765 default, this is L<Class::MOP::Attribute>.
1769 =head2 Class Immutability
1771 Making a class immutable "freezes" the class definition. You can no
1772 longer call methods which alter the class, such as adding or removing
1773 methods or attributes.
1775 Making a class immutable lets us optimize the class by inlining some
1776 methods, and also allows us to optimize some methods on the metaclass
1779 After immutabilization, the metaclass object will cache most informational
1780 methods that returns information about methods or attributes. Methods which
1781 would alter the class, such as C<add_attribute> and C<add_method>, will
1782 throw an error on an immutable metaclass object.
1784 The immutabilization system in L<Moose> takes much greater advantage
1785 of the inlining features than Class::MOP itself does.
1789 =item B<< $metaclass->make_immutable(%options) >>
1791 This method will create an immutable transformer and use it to make
1792 the class and its metaclass object immutable.
1794 This method accepts the following options:
1798 =item * inline_accessors
1800 =item * inline_constructor
1802 =item * inline_destructor
1804 These are all booleans indicating whether the specified method(s)
1807 By default, accessors and the constructor are inlined, but not the
1810 =item * immutable_trait
1812 The name of a class which will be used as a parent class for the
1813 metaclass object being made immutable. This "trait" implements the
1814 post-immutability functionality of the metaclass (but not the
1815 transformation itself).
1817 This defaults to L<Class::MOP::Class::Immutable::Trait>.
1819 =item * constructor_name
1821 This is the constructor method name. This defaults to "new".
1823 =item * constructor_class
1825 The name of the method metaclass for constructors. It will be used to
1826 generate the inlined constructor. This defaults to
1827 "Class::MOP::Method::Constructor".
1829 =item * replace_constructor
1831 This is a boolean indicating whether an existing constructor should be
1832 replaced when inlining a constructor. This defaults to false.
1834 =item * destructor_class
1836 The name of the method metaclass for destructors. It will be used to
1837 generate the inlined destructor. This defaults to
1838 "Class::MOP::Method::Denstructor".
1840 =item * replace_destructor
1842 This is a boolean indicating whether an existing destructor should be
1843 replaced when inlining a destructor. This defaults to false.
1847 =item B<< $metaclass->immutable_options >>
1849 Returns a hash of the options used when making the class immutable, including
1850 both defaults and anything supplied by the user in the call to C<<
1851 $metaclass->make_immutable >>. This is useful if you need to temporarily make
1852 a class mutable and then restore immutability as it was before.
1854 =item B<< $metaclass->make_mutable >>
1856 Calling this method reverse the immutabilization transformation.
1860 =head2 Method Modifiers
1862 Method modifiers are hooks which allow a method to be wrapped with
1863 I<before>, I<after> and I<around> method modifiers. Every time a
1864 method is called, its modifiers are also called.
1866 A class can modify its own methods, as well as methods defined in
1869 =head3 How method modifiers work?
1871 Method modifiers work by wrapping the original method and then
1872 replacing it in the class's symbol table. The wrappers will handle
1873 calling all the modifiers in the appropriate order and preserving the
1874 calling context for the original method.
1876 The return values of C<before> and C<after> modifiers are
1877 ignored. This is because their purpose is B<not> to filter the input
1878 and output of the primary method (this is done with an I<around>
1881 This may seem like an odd restriction to some, but doing this allows
1882 for simple code to be added at the beginning or end of a method call
1883 without altering the function of the wrapped method or placing any
1884 extra responsibility on the code of the modifier.
1886 Of course if you have more complex needs, you can use the C<around>
1887 modifier which allows you to change both the parameters passed to the
1888 wrapped method, as well as its return value.
1890 Before and around modifiers are called in last-defined-first-called
1891 order, while after modifiers are called in first-defined-first-called
1892 order. So the call tree might looks something like this:
1904 =head3 What is the performance impact?
1906 Of course there is a performance cost associated with method
1907 modifiers, but we have made every effort to make that cost directly
1908 proportional to the number of modifier features you use.
1910 The wrapping method does its best to B<only> do as much work as it
1911 absolutely needs to. In order to do this we have moved some of the
1912 performance costs to set-up time, where they are easier to amortize.
1914 All this said, our benchmarks have indicated the following:
1916 simple wrapper with no modifiers 100% slower
1917 simple wrapper with simple before modifier 400% slower
1918 simple wrapper with simple after modifier 450% slower
1919 simple wrapper with simple around modifier 500-550% slower
1920 simple wrapper with all 3 modifiers 1100% slower
1922 These numbers may seem daunting, but you must remember, every feature
1923 comes with some cost. To put things in perspective, just doing a
1924 simple C<AUTOLOAD> which does nothing but extract the name of the
1925 method called and return it costs about 400% over a normal method
1930 =item B<< $metaclass->add_before_method_modifier($method_name, $code) >>
1932 This wraps the specified method with the supplied subroutine
1933 reference. The modifier will be called as a method itself, and will
1934 receive the same arguments as are passed to the method.
1936 When the modifier exits, the wrapped method will be called.
1938 The return value of the modifier will be ignored.
1940 =item B<< $metaclass->add_after_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 wrapped methods exits, the modifier will be called.
1948 The return value of the modifier will be ignored.
1950 =item B<< $metaclass->add_around_method_modifier($method_name, $code) >>
1952 This wraps the specified method with the supplied subroutine
1955 The first argument passed to the modifier will be a subroutine
1956 reference to the wrapped method. The second argument is the object,
1957 and after that come any arguments passed when the method is called.
1959 The around modifier can choose to call the original method, as well as
1960 what arguments to pass if it does so.
1962 The return value of the modifier is what will be seen by the caller.
1966 =head2 Introspection
1970 =item B<< Class::MOP::Class->meta >>
1972 This will return a L<Class::MOP::Class> instance for this class.
1974 It should also be noted that L<Class::MOP> will actually bootstrap
1975 this module by installing a number of attribute meta-objects into its
1982 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1984 =head1 COPYRIGHT AND LICENSE
1986 Copyright 2006-2010 by Infinity Interactive, Inc.
1988 L<http://www.iinteractive.com>
1990 This library is free software; you can redistribute it and/or modify
1991 it under the same terms as Perl itself.