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 {
619 return $self->get_meta_instance->inline_create_instance(@_);
622 sub inline_rebless_instance {
625 return $self->get_meta_instance->inline_rebless_instance_structure(@_);
630 my $instance = shift;
631 (blessed($instance) && $instance->isa($class->name))
632 || confess "You must pass an instance of the metaclass (" . (ref $class ? $class->name : $class) . "), not ($instance)";
635 # we need to protect the integrity of the
636 # Class::MOP::Class singletons here, they
637 # should not be cloned.
638 return $instance if $instance->isa('Class::MOP::Class');
639 $class->_clone_instance($instance, @_);
642 sub _clone_instance {
643 my ($class, $instance, %params) = @_;
645 || confess "You can only clone instances, ($instance) is not a blessed instance";
646 my $meta_instance = $class->get_meta_instance();
647 my $clone = $meta_instance->clone_instance($instance);
648 foreach my $attr ($class->get_all_attributes()) {
649 if ( defined( my $init_arg = $attr->init_arg ) ) {
650 if (exists $params{$init_arg}) {
651 $attr->set_value($clone, $params{$init_arg});
658 sub rebless_instance {
659 my ($self, $instance, %params) = @_;
661 my $old_metaclass = Class::MOP::class_of($instance);
663 my $old_class = $old_metaclass ? $old_metaclass->name : blessed($instance);
664 $self->name->isa($old_class)
665 || confess "You may rebless only into a subclass of ($old_class), of which (". $self->name .") isn't.";
667 $old_metaclass->rebless_instance_away($instance, $self, %params)
670 my $meta_instance = $self->get_meta_instance();
673 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
674 $meta_instance->rebless_instance_structure($_[1], $self);
676 foreach my $attr ( $self->get_all_attributes ) {
677 if ( $attr->has_value($instance) ) {
678 if ( defined( my $init_arg = $attr->init_arg ) ) {
679 $params{$init_arg} = $attr->get_value($instance)
680 unless exists $params{$init_arg};
683 $attr->set_value($instance, $attr->get_value($instance));
688 foreach my $attr ($self->get_all_attributes) {
689 $attr->initialize_instance_slot($meta_instance, $instance, \%params);
695 sub rebless_instance_back {
696 my ( $self, $instance ) = @_;
698 my $old_metaclass = Class::MOP::class_of($instance);
701 = $old_metaclass ? $old_metaclass->name : blessed($instance);
702 $old_class->isa( $self->name )
704 "You may rebless only into a superclass of ($old_class), of which ("
708 $old_metaclass->rebless_instance_away( $instance, $self )
711 my $meta_instance = $self->get_meta_instance;
713 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
714 $meta_instance->rebless_instance_structure( $_[1], $self );
716 for my $attr ( $old_metaclass->get_all_attributes ) {
717 next if $self->has_attribute( $attr->name );
718 $meta_instance->deinitialize_slot( $instance, $_ ) for $attr->slots;
724 sub rebless_instance_away {
725 # this intentionally does nothing, it is just a hook
728 sub _attach_attribute {
729 my ($self, $attribute) = @_;
730 $attribute->attach_to_class($self);
733 sub _post_add_attribute {
734 my ( $self, $attribute ) = @_;
736 $self->invalidate_meta_instances;
738 # invalidate package flag here
741 $attribute->install_accessors;
744 $self->remove_attribute( $attribute->name );
749 sub remove_attribute {
752 my $removed_attribute = $self->SUPER::remove_attribute(@_)
755 $self->invalidate_meta_instances;
757 $removed_attribute->remove_accessors;
758 $removed_attribute->detach_from_class;
760 return$removed_attribute;
763 sub find_attribute_by_name {
764 my ( $self, $attr_name ) = @_;
766 foreach my $class ( $self->linearized_isa ) {
767 # fetch the meta-class ...
768 my $meta = Class::MOP::Class->initialize($class);
769 return $meta->get_attribute($attr_name)
770 if $meta->has_attribute($attr_name);
776 sub get_all_attributes {
778 my %attrs = map { %{ Class::MOP::Class->initialize($_)->_attribute_map } }
779 reverse $self->linearized_isa;
780 return values %attrs;
788 my $isa = $self->get_or_add_package_symbol(
789 { sigil => '@', type => 'ARRAY', name => 'ISA' } );
796 # on 5.8 and below, we need to call
797 # a method to get Perl to detect
798 # a cycle in the class hierarchy
799 my $class = $self->name;
803 # we need to check the metaclass
804 # compatibility here so that we can
805 # be sure that the superclass is
806 # not potentially creating an issues
807 # we don't know about
809 $self->_check_metaclass_compatibility();
810 $self->_superclasses_updated();
816 sub _superclasses_updated {
818 $self->update_meta_instance_dependencies();
823 my $super_class = $self->name;
825 return @{ $super_class->mro::get_isarev() };
828 sub direct_subclasses {
830 my $super_class = $self->name;
835 } Class::MOP::Class->initialize($_)->superclasses
840 return @{ mro::get_linear_isa( (shift)->name ) };
843 sub class_precedence_list {
845 my $name = $self->name;
847 unless (Class::MOP::IS_RUNNING_ON_5_10()) {
849 # We need to check for circular inheritance here
850 # if we are are not on 5.10, cause 5.8 detects it
851 # late. This will do nothing if all is well, and
852 # blow up otherwise. Yes, it's an ugly hack, better
853 # suggestions are welcome.
855 ($name || return)->isa('This is a test for circular inheritance')
858 # if our mro is c3, we can
859 # just grab the linear_isa
860 if (mro::get_mro($name) eq 'c3') {
861 return @{ mro::get_linear_isa($name) }
865 # we can't grab the linear_isa for dfs
866 # since it has all the duplicates
871 Class::MOP::Class->initialize($_)->class_precedence_list()
872 } $self->superclasses()
880 my $fetch_and_prepare_method = sub {
881 my ($self, $method_name) = @_;
882 my $wrapped_metaclass = $self->wrapped_method_metaclass;
884 my $method = $self->get_method($method_name);
885 # if we dont have local ...
887 # try to find the next method
888 $method = $self->find_next_method_by_name($method_name);
889 # die if it does not exist
891 || confess "The method '$method_name' was not found in the inheritance hierarchy for " . $self->name;
892 # and now make sure to wrap it
893 # even if it is already wrapped
894 # because we need a new sub ref
895 $method = $wrapped_metaclass->wrap($method,
896 package_name => $self->name,
897 name => $method_name,
901 # now make sure we wrap it properly
902 $method = $wrapped_metaclass->wrap($method,
903 package_name => $self->name,
904 name => $method_name,
905 ) unless $method->isa($wrapped_metaclass);
907 $self->add_method($method_name => $method);
911 sub add_before_method_modifier {
912 my ($self, $method_name, $method_modifier) = @_;
913 (defined $method_name && length $method_name)
914 || confess "You must pass in a method name";
915 my $method = $fetch_and_prepare_method->($self, $method_name);
916 $method->add_before_modifier(
917 subname(':before' => $method_modifier)
921 sub add_after_method_modifier {
922 my ($self, $method_name, $method_modifier) = @_;
923 (defined $method_name && length $method_name)
924 || confess "You must pass in a method name";
925 my $method = $fetch_and_prepare_method->($self, $method_name);
926 $method->add_after_modifier(
927 subname(':after' => $method_modifier)
931 sub add_around_method_modifier {
932 my ($self, $method_name, $method_modifier) = @_;
933 (defined $method_name && length $method_name)
934 || confess "You must pass in a method name";
935 my $method = $fetch_and_prepare_method->($self, $method_name);
936 $method->add_around_modifier(
937 subname(':around' => $method_modifier)
942 # the methods above used to be named like this:
943 # ${pkg}::${method}:(before|after|around)
944 # but this proved problematic when using one modifier
945 # to wrap multiple methods (something which is likely
946 # to happen pretty regularly IMO). So instead of naming
947 # it like this, I have chosen to just name them purely
948 # with their modifier names, like so:
949 # :(before|after|around)
950 # The fact is that in a stack trace, it will be fairly
951 # evident from the context what method they are attached
952 # to, and so don't need the fully qualified name.
955 sub find_method_by_name {
956 my ($self, $method_name) = @_;
957 (defined $method_name && length $method_name)
958 || confess "You must define a method name to find";
959 foreach my $class ($self->linearized_isa) {
960 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
961 return $method if defined $method;
966 sub get_all_methods {
970 for my $class ( reverse $self->linearized_isa ) {
971 my $meta = Class::MOP::Class->initialize($class);
973 $methods{ $_->name } = $_ for $meta->_get_local_methods;
976 return values %methods;
979 sub get_all_method_names {
982 return grep { !$uniq{$_}++ } map { Class::MOP::Class->initialize($_)->get_method_list } $self->linearized_isa;
985 sub find_all_methods_by_name {
986 my ($self, $method_name) = @_;
987 (defined $method_name && length $method_name)
988 || confess "You must define a method name to find";
990 foreach my $class ($self->linearized_isa) {
991 # fetch the meta-class ...
992 my $meta = Class::MOP::Class->initialize($class);
994 name => $method_name,
996 code => $meta->get_method($method_name)
997 } if $meta->has_method($method_name);
1002 sub find_next_method_by_name {
1003 my ($self, $method_name) = @_;
1004 (defined $method_name && length $method_name)
1005 || confess "You must define a method name to find";
1006 my @cpl = $self->linearized_isa;
1007 shift @cpl; # discard ourselves
1008 foreach my $class (@cpl) {
1009 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
1010 return $method if defined $method;
1015 sub update_meta_instance_dependencies {
1018 if ( $self->{meta_instance_dependencies} ) {
1019 return $self->add_meta_instance_dependencies;
1023 sub add_meta_instance_dependencies {
1026 $self->remove_meta_instance_dependencies;
1028 my @attrs = $self->get_all_attributes();
1031 my @classes = grep { not $seen{ $_->name }++ }
1032 map { $_->associated_class } @attrs;
1034 foreach my $class (@classes) {
1035 $class->add_dependent_meta_instance($self);
1038 $self->{meta_instance_dependencies} = \@classes;
1041 sub remove_meta_instance_dependencies {
1044 if ( my $classes = delete $self->{meta_instance_dependencies} ) {
1045 foreach my $class (@$classes) {
1046 $class->remove_dependent_meta_instance($self);
1056 sub add_dependent_meta_instance {
1057 my ( $self, $metaclass ) = @_;
1058 push @{ $self->{dependent_meta_instances} }, $metaclass;
1061 sub remove_dependent_meta_instance {
1062 my ( $self, $metaclass ) = @_;
1063 my $name = $metaclass->name;
1064 @$_ = grep { $_->name ne $name } @$_
1065 for $self->{dependent_meta_instances};
1068 sub invalidate_meta_instances {
1070 $_->invalidate_meta_instance()
1071 for $self, @{ $self->{dependent_meta_instances} };
1074 sub invalidate_meta_instance {
1076 undef $self->{_meta_instance};
1079 # check if we can reinitialize
1083 # if any local attr is defined
1084 return if $self->get_attribute_list;
1086 # or any non-declared methods
1087 for my $method ( map { $self->get_method($_) } $self->get_method_list ) {
1088 return if $method->isa("Class::MOP::Method::Generated");
1089 # FIXME do we need to enforce this too? return unless $method->isa( $self->method_metaclass );
1097 sub is_mutable { 1 }
1098 sub is_immutable { 0 }
1100 sub immutable_options { %{ $_[0]{__immutable}{options} || {} } }
1102 sub _immutable_options {
1103 my ( $self, @args ) = @_;
1106 inline_accessors => 1,
1107 inline_constructor => 1,
1108 inline_destructor => 0,
1110 immutable_trait => $self->immutable_trait,
1111 constructor_name => $self->constructor_name,
1112 constructor_class => $self->constructor_class,
1113 destructor_class => $self->destructor_class,
1118 sub make_immutable {
1119 my ( $self, @args ) = @_;
1121 if ( $self->is_mutable ) {
1122 $self->_initialize_immutable( $self->_immutable_options(@args) );
1123 $self->_rebless_as_immutable(@args);
1134 if ( $self->is_immutable ) {
1135 my @args = $self->immutable_options;
1136 $self->_rebless_as_mutable();
1137 $self->_remove_inlined_code(@args);
1138 delete $self->{__immutable};
1146 sub _rebless_as_immutable {
1147 my ( $self, @args ) = @_;
1149 $self->{__immutable}{original_class} = ref $self;
1151 bless $self => $self->_immutable_metaclass(@args);
1154 sub _immutable_metaclass {
1155 my ( $self, %args ) = @_;
1157 if ( my $class = $args{immutable_metaclass} ) {
1161 my $trait = $args{immutable_trait} = $self->immutable_trait
1162 || confess "no immutable trait specified for $self";
1164 my $meta = $self->meta;
1165 my $meta_attr = $meta->find_attribute_by_name("immutable_trait");
1169 if ( $meta_attr and $trait eq $meta_attr->default ) {
1170 # if the trait is the same as the default we try and pick a
1171 # predictable name for the immutable metaclass
1172 $class_name = 'Class::MOP::Class::Immutable::' . ref($self);
1175 $class_name = join '::', 'Class::MOP::Class::Immutable::CustomTrait',
1176 $trait, 'ForMetaClass', ref($self);
1180 if Class::MOP::is_class_loaded($class_name);
1182 # If the metaclass is a subclass of CMOP::Class which has had
1183 # metaclass roles applied (via Moose), then we want to make sure
1184 # that we preserve that anonymous class (see Fey::ORM for an
1185 # example of where this matters).
1186 my $meta_name = $meta->_real_ref_name;
1188 my $immutable_meta = $meta_name->create(
1190 superclasses => [ ref $self ],
1193 Class::MOP::MiniTrait::apply( $immutable_meta, $trait );
1195 $immutable_meta->make_immutable(
1196 inline_constructor => 0,
1197 inline_accessors => 0,
1203 sub _remove_inlined_code {
1206 $self->remove_method( $_->name ) for $self->_inlined_methods;
1208 delete $self->{__immutable}{inlined_methods};
1211 sub _inlined_methods { @{ $_[0]{__immutable}{inlined_methods} || [] } }
1213 sub _add_inlined_method {
1214 my ( $self, $method ) = @_;
1216 push @{ $self->{__immutable}{inlined_methods} ||= [] }, $method;
1219 sub _initialize_immutable {
1220 my ( $self, %args ) = @_;
1222 $self->{__immutable}{options} = \%args;
1223 $self->_install_inlined_code(%args);
1226 sub _install_inlined_code {
1227 my ( $self, %args ) = @_;
1230 $self->_inline_accessors(%args) if $args{inline_accessors};
1231 $self->_inline_constructor(%args) if $args{inline_constructor};
1232 $self->_inline_destructor(%args) if $args{inline_destructor};
1235 sub _rebless_as_mutable {
1238 bless $self, $self->_get_mutable_metaclass_name;
1243 sub _inline_accessors {
1246 foreach my $attr_name ( $self->get_attribute_list ) {
1247 $self->get_attribute($attr_name)->install_accessors(1);
1251 sub _inline_constructor {
1252 my ( $self, %args ) = @_;
1254 my $name = $args{constructor_name};
1255 # A class may not even have a constructor, and that's okay.
1256 return unless defined $name;
1258 if ( $self->has_method($name) && !$args{replace_constructor} ) {
1259 my $class = $self->name;
1260 warn "Not inlining a constructor for $class since it defines"
1261 . " its own constructor.\n"
1262 . "If you are certain you don't need to inline your"
1263 . " constructor, specify inline_constructor => 0 in your"
1264 . " call to $class->meta->make_immutable\n";
1268 my $constructor_class = $args{constructor_class};
1270 Class::MOP::load_class($constructor_class);
1272 my $constructor = $constructor_class->new(
1276 package_name => $self->name,
1280 if ( $args{replace_constructor} or $constructor->can_be_inlined ) {
1281 $self->add_method( $name => $constructor );
1282 $self->_add_inlined_method($constructor);
1286 sub _inline_destructor {
1287 my ( $self, %args ) = @_;
1289 ( exists $args{destructor_class} && defined $args{destructor_class} )
1290 || confess "The 'inline_destructor' option is present, but "
1291 . "no destructor class was specified";
1293 if ( $self->has_method('DESTROY') && ! $args{replace_destructor} ) {
1294 my $class = $self->name;
1295 warn "Not inlining a destructor for $class since it defines"
1296 . " its own destructor.\n";
1300 my $destructor_class = $args{destructor_class};
1302 Class::MOP::load_class($destructor_class);
1304 return unless $destructor_class->is_needed($self);
1306 my $destructor = $destructor_class->new(
1309 package_name => $self->name,
1313 if ( $args{replace_destructor} or $destructor->can_be_inlined ) {
1314 $self->add_method( 'DESTROY' => $destructor );
1315 $self->_add_inlined_method($destructor);
1327 Class::MOP::Class - Class Meta Object
1331 # assuming that class Foo
1332 # has been defined, you can
1334 # use this for introspection ...
1336 # add a method to Foo ...
1337 Foo->meta->add_method( 'bar' => sub {...} )
1339 # get a list of all the classes searched
1340 # the method dispatcher in the correct order
1341 Foo->meta->class_precedence_list()
1343 # remove a method from Foo
1344 Foo->meta->remove_method('bar');
1346 # or use this to actually create classes ...
1348 Class::MOP::Class->create(
1351 superclasses => ['Foo'],
1353 Class::MOP::Attribute->new('$bar'),
1354 Class::MOP::Attribute->new('$baz'),
1357 calculate_bar => sub {...},
1358 construct_baz => sub {...}
1365 The Class Protocol is the largest and most complex part of the
1366 Class::MOP meta-object protocol. It controls the introspection and
1367 manipulation of Perl 5 classes, and it can create them as well. The
1368 best way to understand what this module can do is to read the
1369 documentation for each of its methods.
1373 C<Class::MOP::Class> is a subclass of L<Class::MOP::Module>.
1377 =head2 Class construction
1379 These methods all create new C<Class::MOP::Class> objects. These
1380 objects can represent existing classes or they can be used to create
1381 new classes from scratch.
1383 The metaclass object for a given class is a singleton. If you attempt
1384 to create a metaclass for the same class twice, you will just get the
1389 =item B<< Class::MOP::Class->create($package_name, %options) >>
1391 This method creates a new C<Class::MOP::Class> object with the given
1392 package name. It accepts a number of options:
1398 An optional version number for the newly created package.
1402 An optional authority for the newly created package.
1404 =item * superclasses
1406 An optional array reference of superclass names.
1410 An optional hash reference of methods for the class. The keys of the
1411 hash reference are method names and values are subroutine references.
1415 An optional array reference of L<Class::MOP::Attribute> objects.
1419 If true, a C<meta> method will not be installed into the class.
1423 =item B<< Class::MOP::Class->create_anon_class(%options) >>
1425 This method works just like C<< Class::MOP::Class->create >> but it
1426 creates an "anonymous" class. In fact, the class does have a name, but
1427 that name is a unique name generated internally by this module.
1429 It accepts the same C<superclasses>, C<methods>, and C<attributes>
1430 parameters that C<create> accepts.
1432 Anonymous classes are destroyed once the metaclass they are attached
1433 to goes out of scope, and will be removed from Perl's internal symbol
1436 All instances of an anonymous class keep a special reference to the
1437 metaclass object, which prevents the metaclass from going out of scope
1438 while any instances exist.
1440 This only works if the instance is based on a hash reference, however.
1442 =item B<< Class::MOP::Class->initialize($package_name, %options) >>
1444 This method will initialize a C<Class::MOP::Class> object for the
1445 named package. Unlike C<create>, this method I<will not> create a new
1448 The purpose of this method is to retrieve a C<Class::MOP::Class>
1449 object for introspecting an existing class.
1451 If an existing C<Class::MOP::Class> object exists for the named
1452 package, it will be returned, and any options provided will be
1455 If the object does not yet exist, it will be created.
1457 The valid options that can be passed to this method are
1458 C<attribute_metaclass>, C<method_metaclass>,
1459 C<wrapped_method_metaclass>, and C<instance_metaclass>. These are all
1460 optional, and default to the appropriate class in the C<Class::MOP>
1465 =head2 Object instance construction and cloning
1467 These methods are all related to creating and/or cloning object
1472 =item B<< $metaclass->clone_object($instance, %params) >>
1474 This method clones an existing object instance. Any parameters you
1475 provide are will override existing attribute values in the object.
1477 This is a convenience method for cloning an object instance, then
1478 blessing it into the appropriate package.
1480 You could implement a clone method in your class, using this method:
1483 my ($self, %params) = @_;
1484 $self->meta->clone_object($self, %params);
1487 =item B<< $metaclass->rebless_instance($instance, %params) >>
1489 This method changes the class of C<$instance> to the metaclass's class.
1491 You can only rebless an instance into a subclass of its current
1492 class. If you pass any additional parameters, these will be treated
1493 like constructor parameters and used to initialize the object's
1494 attributes. Any existing attributes that are already set will be
1497 Before reblessing the instance, this method will call
1498 C<rebless_instance_away> on the instance's current metaclass. This method
1499 will be passed the instance, the new metaclass, and any parameters
1500 specified to C<rebless_instance>. By default, C<rebless_instance_away>
1501 does nothing; it is merely a hook.
1503 =item B<< $metaclass->rebless_instance_back($instance) >>
1505 Does the same thing as C<rebless_instance>, except that you can only
1506 rebless an instance into one of its superclasses. Any attributes that
1507 do not exist in the superclass will be deinitialized.
1509 This is a much more dangerous operation than C<rebless_instance>,
1510 especially when multiple inheritance is involved, so use this carefully!
1512 =item B<< $metaclass->new_object(%params) >>
1514 This method is used to create a new object of the metaclass's
1515 class. Any parameters you provide are used to initialize the
1516 instance's attributes. A special C<__INSTANCE__> key can be passed to
1517 provide an already generated instance, rather than having Class::MOP
1518 generate it for you. This is mostly useful for using Class::MOP with
1519 foreign classes which generate instances using their own constructors.
1521 =item B<< $metaclass->instance_metaclass >>
1523 Returns the class name of the instance metaclass. See
1524 L<Class::MOP::Instance> for more information on the instance
1527 =item B<< $metaclass->get_meta_instance >>
1529 Returns an instance of the C<instance_metaclass> to be used in the
1530 construction of a new instance of the class.
1532 =item B<< $metaclass->inline_create_instance($class_var) >>
1534 =item B<< $metaclass->inline_rebless_instance($instance_var, $class_var) >>
1536 These methods takes variable names, and use them to create an inline snippet
1537 of code that will create a new instance of the class.
1541 =head2 Informational predicates
1543 These are a few predicate methods for asking information about the
1548 =item B<< $metaclass->is_anon_class >>
1550 This returns true if the class was created by calling C<<
1551 Class::MOP::Class->create_anon_class >>.
1553 =item B<< $metaclass->is_mutable >>
1555 This returns true if the class is still mutable.
1557 =item B<< $metaclass->is_immutable >>
1559 This returns true if the class has been made immutable.
1561 =item B<< $metaclass->is_pristine >>
1563 A class is I<not> pristine if it has non-inherited attributes or if it
1564 has any generated methods.
1568 =head2 Inheritance Relationships
1572 =item B<< $metaclass->superclasses(@superclasses) >>
1574 This is a read-write accessor which represents the superclass
1575 relationships of the metaclass's class.
1577 This is basically sugar around getting and setting C<@ISA>.
1579 =item B<< $metaclass->class_precedence_list >>
1581 This returns a list of all of the class's ancestor classes. The
1582 classes are returned in method dispatch order.
1584 =item B<< $metaclass->linearized_isa >>
1586 This returns a list based on C<class_precedence_list> but with all
1589 =item B<< $metaclass->subclasses >>
1591 This returns a list of all subclasses for this class, even indirect
1594 =item B<< $metaclass->direct_subclasses >>
1596 This returns a list of immediate subclasses for this class, which does not
1597 include indirect subclasses.
1601 =head2 Method introspection and creation
1603 These methods allow you to introspect a class's methods, as well as
1604 add, remove, or change methods.
1606 Determining what is truly a method in a Perl 5 class requires some
1607 heuristics (aka guessing).
1609 Methods defined outside the package with a fully qualified name (C<sub
1610 Package::name { ... }>) will be included. Similarly, methods named
1611 with a fully qualified name using L<Sub::Name> are also included.
1613 However, we attempt to ignore imported functions.
1615 Ultimately, we are using heuristics to determine what truly is a
1616 method in a class, and these heuristics may get the wrong answer in
1617 some edge cases. However, for most "normal" cases the heuristics work
1622 =item B<< $metaclass->get_method($method_name) >>
1624 This will return a L<Class::MOP::Method> for the specified
1625 C<$method_name>. If the class does not have the specified method, it
1628 =item B<< $metaclass->has_method($method_name) >>
1630 Returns a boolean indicating whether or not the class defines the
1631 named method. It does not include methods inherited from parent
1634 =item B<< $metaclass->get_method_list >>
1636 This will return a list of method I<names> for all methods defined in
1639 =item B<< $metaclass->add_method($method_name, $method) >>
1641 This method takes a method name and a subroutine reference, and adds
1642 the method to the class.
1644 The subroutine reference can be a L<Class::MOP::Method>, and you are
1645 strongly encouraged to pass a meta method object instead of a code
1646 reference. If you do so, that object gets stored as part of the
1647 class's method map directly. If not, the meta information will have to
1648 be recreated later, and may be incorrect.
1650 If you provide a method object, this method will clone that object if
1651 the object's package name does not match the class name. This lets us
1652 track the original source of any methods added from other classes
1653 (notably Moose roles).
1655 =item B<< $metaclass->remove_method($method_name) >>
1657 Remove the named method from the class. This method returns the
1658 L<Class::MOP::Method> object for the method.
1660 =item B<< $metaclass->method_metaclass >>
1662 Returns the class name of the method metaclass, see
1663 L<Class::MOP::Method> for more information on the method metaclass.
1665 =item B<< $metaclass->wrapped_method_metaclass >>
1667 Returns the class name of the wrapped method metaclass, see
1668 L<Class::MOP::Method::Wrapped> for more information on the wrapped
1671 =item B<< $metaclass->get_all_methods >>
1673 This will traverse the inheritance hierarchy and return a list of all
1674 the L<Class::MOP::Method> objects for this class and its parents.
1676 =item B<< $metaclass->find_method_by_name($method_name) >>
1678 This will return a L<Class::MOP::Method> for the specified
1679 C<$method_name>. If the class does not have the specified method, it
1682 Unlike C<get_method>, this method I<will> look for the named method in
1685 =item B<< $metaclass->get_all_method_names >>
1687 This will return a list of method I<names> for all of this class's
1688 methods, including inherited methods.
1690 =item B<< $metaclass->find_all_methods_by_name($method_name) >>
1692 This method looks for the named method in the class and all of its
1693 parents. It returns every matching method it finds in the inheritance
1694 tree, so it returns a list of methods.
1696 Each method is returned as a hash reference with three keys. The keys
1697 are C<name>, C<class>, and C<code>. The C<code> key has a
1698 L<Class::MOP::Method> object as its value.
1700 The list of methods is distinct.
1702 =item B<< $metaclass->find_next_method_by_name($method_name) >>
1704 This method returns the first method in any superclass matching the
1705 given name. It is effectively the method that C<SUPER::$method_name>
1710 =head2 Attribute introspection and creation
1712 Because Perl 5 does not have a core concept of attributes in classes,
1713 we can only return information about attributes which have been added
1714 via this class's methods. We cannot discover information about
1715 attributes which are defined in terms of "regular" Perl 5 methods.
1719 =item B<< $metaclass->get_attribute($attribute_name) >>
1721 This will return a L<Class::MOP::Attribute> for the specified
1722 C<$attribute_name>. If the class does not have the specified
1723 attribute, it returns C<undef>.
1725 NOTE that get_attribute does not search superclasses, for that you
1726 need to use C<find_attribute_by_name>.
1728 =item B<< $metaclass->has_attribute($attribute_name) >>
1730 Returns a boolean indicating whether or not the class defines the
1731 named attribute. It does not include attributes inherited from parent
1734 =item B<< $metaclass->get_attribute_list >>
1736 This will return a list of attributes I<names> for all attributes
1737 defined in this class. Note that this operates on the current class
1738 only, it does not traverse the inheritance hierarchy.
1740 =item B<< $metaclass->get_all_attributes >>
1742 This will traverse the inheritance hierarchy and return a list of all
1743 the L<Class::MOP::Attribute> objects for this class and its parents.
1745 =item B<< $metaclass->find_attribute_by_name($attribute_name) >>
1747 This will return a L<Class::MOP::Attribute> for the specified
1748 C<$attribute_name>. If the class does not have the specified
1749 attribute, it returns C<undef>.
1751 Unlike C<get_attribute>, this attribute I<will> look for the named
1752 attribute in superclasses.
1754 =item B<< $metaclass->add_attribute(...) >>
1756 This method accepts either an existing L<Class::MOP::Attribute>
1757 object or parameters suitable for passing to that class's C<new>
1760 The attribute provided will be added to the class.
1762 Any accessor methods defined by the attribute will be added to the
1763 class when the attribute is added.
1765 If an attribute of the same name already exists, the old attribute
1766 will be removed first.
1768 =item B<< $metaclass->remove_attribute($attribute_name) >>
1770 This will remove the named attribute from the class, and
1771 L<Class::MOP::Attribute> object.
1773 Removing an attribute also removes any accessor methods defined by the
1776 However, note that removing an attribute will only affect I<future>
1777 object instances created for this class, not existing instances.
1779 =item B<< $metaclass->attribute_metaclass >>
1781 Returns the class name of the attribute metaclass for this class. By
1782 default, this is L<Class::MOP::Attribute>.
1786 =head2 Class Immutability
1788 Making a class immutable "freezes" the class definition. You can no
1789 longer call methods which alter the class, such as adding or removing
1790 methods or attributes.
1792 Making a class immutable lets us optimize the class by inlining some
1793 methods, and also allows us to optimize some methods on the metaclass
1796 After immutabilization, the metaclass object will cache most informational
1797 methods that returns information about methods or attributes. Methods which
1798 would alter the class, such as C<add_attribute> and C<add_method>, will
1799 throw an error on an immutable metaclass object.
1801 The immutabilization system in L<Moose> takes much greater advantage
1802 of the inlining features than Class::MOP itself does.
1806 =item B<< $metaclass->make_immutable(%options) >>
1808 This method will create an immutable transformer and use it to make
1809 the class and its metaclass object immutable.
1811 This method accepts the following options:
1815 =item * inline_accessors
1817 =item * inline_constructor
1819 =item * inline_destructor
1821 These are all booleans indicating whether the specified method(s)
1824 By default, accessors and the constructor are inlined, but not the
1827 =item * immutable_trait
1829 The name of a class which will be used as a parent class for the
1830 metaclass object being made immutable. This "trait" implements the
1831 post-immutability functionality of the metaclass (but not the
1832 transformation itself).
1834 This defaults to L<Class::MOP::Class::Immutable::Trait>.
1836 =item * constructor_name
1838 This is the constructor method name. This defaults to "new".
1840 =item * constructor_class
1842 The name of the method metaclass for constructors. It will be used to
1843 generate the inlined constructor. This defaults to
1844 "Class::MOP::Method::Constructor".
1846 =item * replace_constructor
1848 This is a boolean indicating whether an existing constructor should be
1849 replaced when inlining a constructor. This defaults to false.
1851 =item * destructor_class
1853 The name of the method metaclass for destructors. It will be used to
1854 generate the inlined destructor. This defaults to
1855 "Class::MOP::Method::Denstructor".
1857 =item * replace_destructor
1859 This is a boolean indicating whether an existing destructor should be
1860 replaced when inlining a destructor. This defaults to false.
1864 =item B<< $metaclass->immutable_options >>
1866 Returns a hash of the options used when making the class immutable, including
1867 both defaults and anything supplied by the user in the call to C<<
1868 $metaclass->make_immutable >>. This is useful if you need to temporarily make
1869 a class mutable and then restore immutability as it was before.
1871 =item B<< $metaclass->make_mutable >>
1873 Calling this method reverse the immutabilization transformation.
1877 =head2 Method Modifiers
1879 Method modifiers are hooks which allow a method to be wrapped with
1880 I<before>, I<after> and I<around> method modifiers. Every time a
1881 method is called, its modifiers are also called.
1883 A class can modify its own methods, as well as methods defined in
1886 =head3 How method modifiers work?
1888 Method modifiers work by wrapping the original method and then
1889 replacing it in the class's symbol table. The wrappers will handle
1890 calling all the modifiers in the appropriate order and preserving the
1891 calling context for the original method.
1893 The return values of C<before> and C<after> modifiers are
1894 ignored. This is because their purpose is B<not> to filter the input
1895 and output of the primary method (this is done with an I<around>
1898 This may seem like an odd restriction to some, but doing this allows
1899 for simple code to be added at the beginning or end of a method call
1900 without altering the function of the wrapped method or placing any
1901 extra responsibility on the code of the modifier.
1903 Of course if you have more complex needs, you can use the C<around>
1904 modifier which allows you to change both the parameters passed to the
1905 wrapped method, as well as its return value.
1907 Before and around modifiers are called in last-defined-first-called
1908 order, while after modifiers are called in first-defined-first-called
1909 order. So the call tree might looks something like this:
1921 =head3 What is the performance impact?
1923 Of course there is a performance cost associated with method
1924 modifiers, but we have made every effort to make that cost directly
1925 proportional to the number of modifier features you use.
1927 The wrapping method does its best to B<only> do as much work as it
1928 absolutely needs to. In order to do this we have moved some of the
1929 performance costs to set-up time, where they are easier to amortize.
1931 All this said, our benchmarks have indicated the following:
1933 simple wrapper with no modifiers 100% slower
1934 simple wrapper with simple before modifier 400% slower
1935 simple wrapper with simple after modifier 450% slower
1936 simple wrapper with simple around modifier 500-550% slower
1937 simple wrapper with all 3 modifiers 1100% slower
1939 These numbers may seem daunting, but you must remember, every feature
1940 comes with some cost. To put things in perspective, just doing a
1941 simple C<AUTOLOAD> which does nothing but extract the name of the
1942 method called and return it costs about 400% over a normal method
1947 =item B<< $metaclass->add_before_method_modifier($method_name, $code) >>
1949 This wraps the specified method with the supplied subroutine
1950 reference. The modifier will be called as a method itself, and will
1951 receive the same arguments as are passed to the method.
1953 When the modifier exits, the wrapped method will be called.
1955 The return value of the modifier will be ignored.
1957 =item B<< $metaclass->add_after_method_modifier($method_name, $code) >>
1959 This wraps the specified method with the supplied subroutine
1960 reference. The modifier will be called as a method itself, and will
1961 receive the same arguments as are passed to the method.
1963 When the wrapped methods exits, the modifier will be called.
1965 The return value of the modifier will be ignored.
1967 =item B<< $metaclass->add_around_method_modifier($method_name, $code) >>
1969 This wraps the specified method with the supplied subroutine
1972 The first argument passed to the modifier will be a subroutine
1973 reference to the wrapped method. The second argument is the object,
1974 and after that come any arguments passed when the method is called.
1976 The around modifier can choose to call the original method, as well as
1977 what arguments to pass if it does so.
1979 The return value of the modifier is what will be seen by the caller.
1983 =head2 Introspection
1987 =item B<< Class::MOP::Class->meta >>
1989 This will return a L<Class::MOP::Class> instance for this class.
1991 It should also be noted that L<Class::MOP> will actually bootstrap
1992 this module by installing a number of attribute meta-objects into its
1999 Stevan Little E<lt>stevan@iinteractive.comE<gt>
2001 =head1 COPYRIGHT AND LICENSE
2003 Copyright 2006-2010 by Infinity Interactive, Inc.
2005 L<http://www.iinteractive.com>
2007 This library is free software; you can redistribute it and/or modify
2008 it under the same terms as Perl itself.