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.03';
20 $VERSION = eval $VERSION;
21 our $AUTHORITY = 'cpan:STEVAN';
23 use base 'Class::MOP::Module',
24 'Class::MOP::Mixin::HasAttributes',
25 'Class::MOP::Mixin::HasMethods';
35 $package_name = shift;
38 $package_name = $options{package};
41 ($package_name && !ref($package_name))
42 || confess "You must pass a package name and it cannot be blessed";
44 return Class::MOP::get_metaclass_by_name($package_name)
45 || $class->_construct_class_instance(package => $package_name, @_);
48 # NOTE: (meta-circularity)
49 # this is a special form of _construct_instance
50 # (see below), which is used to construct class
51 # meta-object instances for any Class::MOP::*
52 # class. All other classes will use the more
53 # normal &construct_instance.
54 sub _construct_class_instance {
56 my $options = @_ == 1 ? $_[0] : {@_};
57 my $package_name = $options->{package};
58 (defined $package_name && $package_name)
59 || confess "You must pass a package name";
61 # return the metaclass if we have it cached,
62 # and it is still defined (it has not been
63 # reaped by DESTROY yet, which can happen
64 # annoyingly enough during global destruction)
66 if (defined(my $meta = Class::MOP::get_metaclass_by_name($package_name))) {
72 ? $class->_real_ref_name
75 # now create the metaclass
77 if ($class eq 'Class::MOP::Class') {
78 $meta = $class->_new($options);
82 # it is safe to use meta here because
83 # class will always be a subclass of
84 # Class::MOP::Class, which defines meta
85 $meta = $class->meta->_construct_instance($options)
88 # and check the metaclass compatibility
89 $meta->_check_metaclass_compatibility();
91 Class::MOP::store_metaclass_by_name($package_name, $meta);
94 # we need to weaken any anon classes
95 # so that they can call DESTROY properly
96 Class::MOP::weaken_metaclass($package_name) if $meta->is_anon_class;
104 # NOTE: we need to deal with the possibility of class immutability here,
105 # and then get the name of the class appropriately
106 return $self->is_immutable
107 ? $self->_get_mutable_metaclass_name()
114 return Class::MOP::Class->initialize($class)->new_object(@_)
115 if $class ne __PACKAGE__;
117 my $options = @_ == 1 ? $_[0] : {@_};
120 # inherited from Class::MOP::Package
121 'package' => $options->{package},
124 # since the following attributes will
125 # actually be loaded from the symbol
126 # table, and actually bypass the instance
127 # entirely, we can just leave these things
128 # listed here for reference, because they
129 # should not actually have a value associated
131 'namespace' => \undef,
134 # inherited from Class::MOP::Module
136 'authority' => \undef,
138 # defined in Class::MOP::Class
139 'superclasses' => \undef,
142 'attribute_metaclass' =>
143 ( $options->{'attribute_metaclass'} || 'Class::MOP::Attribute' ),
144 'method_metaclass' =>
145 ( $options->{'method_metaclass'} || 'Class::MOP::Method' ),
146 'wrapped_method_metaclass' => (
147 $options->{'wrapped_method_metaclass'}
148 || 'Class::MOP::Method::Wrapped'
150 'instance_metaclass' =>
151 ( $options->{'instance_metaclass'} || 'Class::MOP::Instance' ),
152 'immutable_trait' => (
153 $options->{'immutable_trait'}
154 || 'Class::MOP::Class::Immutable::Trait'
156 'constructor_name' => ( $options->{constructor_name} || 'new' ),
157 'constructor_class' => (
158 $options->{constructor_class} || 'Class::MOP::Method::Constructor'
160 'destructor_class' => $options->{destructor_class},
164 sub reset_package_cache_flag { (shift)->{'_package_cache_flag'} = undef }
165 sub update_package_cache_flag {
168 # we can manually update the cache number
169 # since we are actually adding the method
170 # to our cache as well. This avoids us
171 # having to regenerate the method_map.
173 $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);
176 ## Metaclass compatibility
178 my %base_metaclass = (
179 attribute_metaclass => 'Class::MOP::Attribute',
180 method_metaclass => 'Class::MOP::Method',
181 wrapped_method_metaclass => 'Class::MOP::Method::Wrapped',
182 instance_metaclass => 'Class::MOP::Instance',
183 constructor_class => 'Class::MOP::Method::Constructor',
184 destructor_class => 'Class::MOP::Method::Destructor',
187 sub _base_metaclasses { %base_metaclass }
190 sub _check_metaclass_compatibility {
193 if (my @superclasses = $self->superclasses) {
194 $self->_fix_metaclass_incompatibility(@superclasses);
196 my %base_metaclass = $self->_base_metaclasses;
198 # this is always okay ...
199 return if ref($self) eq 'Class::MOP::Class'
201 my $meta = $self->$_;
202 !defined($meta) || $meta eq $base_metaclass{$_}
203 } keys %base_metaclass;
205 for my $superclass (@superclasses) {
206 $self->_check_class_metaclass_compatibility($superclass);
209 for my $metaclass_type (keys %base_metaclass) {
210 next unless defined $self->$metaclass_type;
211 for my $superclass (@superclasses) {
212 $self->_check_single_metaclass_compatibility(
213 $metaclass_type, $superclass
220 sub _class_metaclass_is_compatible {
222 my ( $superclass_name ) = @_;
224 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
227 my $super_meta_type = $super_meta->_real_ref_name;
229 return $self->isa($super_meta_type);
232 sub _check_class_metaclass_compatibility {
234 my ( $superclass_name ) = @_;
236 if (!$self->_class_metaclass_is_compatible($superclass_name)) {
237 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
239 my $super_meta_type = $super_meta->_real_ref_name;
241 confess "The metaclass of " . $self->name . " ("
242 . (ref($self)) . ")" . " is not compatible with "
243 . "the metaclass of its superclass, "
244 . $superclass_name . " (" . ($super_meta_type) . ")";
248 sub _single_metaclass_is_compatible {
250 my ( $metaclass_type, $superclass_name ) = @_;
252 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
255 # for instance, Moose::Meta::Class has a error_class attribute, but
256 # Class::MOP::Class doesn't - this shouldn't be an error
257 return 1 unless $super_meta->can($metaclass_type);
258 # for instance, Moose::Meta::Class has a destructor_class, but
259 # Class::MOP::Class doesn't - this shouldn't be an error
260 return 1 unless defined $super_meta->$metaclass_type;
261 # if metaclass is defined in superclass but not here, it's not compatible
262 # this is a really odd case
263 return 0 unless defined $self->$metaclass_type;
265 return $self->$metaclass_type->isa($super_meta->$metaclass_type);
268 sub _check_single_metaclass_compatibility {
270 my ( $metaclass_type, $superclass_name ) = @_;
272 if (!$self->_single_metaclass_is_compatible($metaclass_type, $superclass_name)) {
273 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
274 my $metaclass_type_name = $metaclass_type;
275 $metaclass_type_name =~ s/_(?:meta)?class$//;
276 $metaclass_type_name =~ s/_/ /g;
277 confess "The $metaclass_type_name metaclass for "
278 . $self->name . " (" . ($self->$metaclass_type)
279 . ")" . " is not compatible with the "
280 . "$metaclass_type_name metaclass of its "
281 . "superclass, " . $superclass_name . " ("
282 . ($super_meta->$metaclass_type) . ")";
286 sub _can_fix_class_metaclass_incompatibility_by_subclassing {
288 my ($super_meta) = @_;
290 my $super_meta_type = $super_meta->_real_ref_name;
292 return $super_meta_type ne blessed($self)
293 && $super_meta->isa(blessed($self));
296 sub _can_fix_single_metaclass_incompatibility_by_subclassing {
298 my ($metaclass_type, $super_meta) = @_;
300 my $specific_meta = $self->$metaclass_type;
301 return unless $super_meta->can($metaclass_type);
302 my $super_specific_meta = $super_meta->$metaclass_type;
304 # for instance, Moose::Meta::Class has a destructor_class, but
305 # Class::MOP::Class doesn't - this shouldn't be an error
306 return unless defined $super_specific_meta;
308 # if metaclass is defined in superclass but not here, it's fixable
309 # this is a really odd case
310 return 1 unless defined $specific_meta;
312 return $specific_meta ne $super_specific_meta
313 && $super_specific_meta->isa($specific_meta);
316 sub _can_fix_metaclass_incompatibility_by_subclassing {
318 my ($super_meta) = @_;
320 return 1 if $self->_can_fix_class_metaclass_incompatibility_by_subclassing($super_meta);
322 my %base_metaclass = $self->_base_metaclasses;
323 for my $metaclass_type (keys %base_metaclass) {
324 return 1 if $self->_can_fix_single_metaclass_incompatibility_by_subclassing($metaclass_type, $super_meta);
330 sub _can_fix_metaclass_incompatibility {
332 return $self->_can_fix_metaclass_incompatibility_by_subclassing(@_);
335 sub _fix_metaclass_incompatibility {
337 my @supers = map { Class::MOP::Class->initialize($_) } @_;
340 for my $super (@supers) {
342 if $self->_can_fix_metaclass_incompatibility($super);
344 return unless $necessary;
346 for my $super (@supers) {
347 if (!$self->_class_metaclass_is_compatible($super->name)) {
348 $self->_fix_class_metaclass_incompatibility($super);
352 my %base_metaclass = $self->_base_metaclasses;
353 for my $metaclass_type (keys %base_metaclass) {
354 for my $super (@supers) {
355 if (!$self->_single_metaclass_is_compatible($metaclass_type, $super->name)) {
356 $self->_fix_single_metaclass_incompatibility(
357 $metaclass_type, $super
364 sub _fix_class_metaclass_incompatibility {
366 my ( $super_meta ) = @_;
368 if ($self->_can_fix_class_metaclass_incompatibility_by_subclassing($super_meta)) {
370 || confess "Can't fix metaclass incompatibility for "
372 . " because it is not pristine.";
374 my $super_meta_name = $super_meta->_real_ref_name;
376 $super_meta_name->meta->rebless_instance($self);
380 sub _fix_single_metaclass_incompatibility {
382 my ( $metaclass_type, $super_meta ) = @_;
384 if ($self->_can_fix_single_metaclass_incompatibility_by_subclassing($metaclass_type, $super_meta)) {
386 || confess "Can't fix metaclass incompatibility for "
388 . " because it is not pristine.";
390 $self->{$metaclass_type} = $super_meta->$metaclass_type;
398 # this should be sufficient, if you have a
399 # use case where it is not, write a test and
401 my $ANON_CLASS_SERIAL = 0;
404 # we need a sufficiently annoying prefix
405 # this should suffice for now, this is
406 # used in a couple of places below, so
407 # need to put it up here for now.
408 my $ANON_CLASS_PREFIX = 'Class::MOP::Class::__ANON__::SERIAL::';
412 no warnings 'uninitialized';
413 $self->name =~ /^$ANON_CLASS_PREFIX/o;
416 sub create_anon_class {
417 my ($class, %options) = @_;
418 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
419 return $class->create($package_name, %options);
423 # this will only get called for
424 # anon-classes, all other calls
425 # are assumed to occur during
426 # global destruction and so don't
427 # really need to be handled explicitly
431 return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated
433 no warnings 'uninitialized';
434 my $name = $self->name;
435 return unless $name =~ /^$ANON_CLASS_PREFIX/o;
437 # Moose does a weird thing where it replaces the metaclass for
438 # class when fixing metaclass incompatibility. In that case,
439 # we don't want to clean out the namespace now. We can detect
440 # that because Moose will explicitly update the singleton
441 # cache in Class::MOP.
442 my $current_meta = Class::MOP::get_metaclass_by_name($name);
443 return if $current_meta ne $self;
445 my ($serial_id) = ($name =~ /^$ANON_CLASS_PREFIX(\d+)/o);
447 @{$name . '::ISA'} = ();
448 %{$name . '::'} = ();
449 delete ${$ANON_CLASS_PREFIX}{$serial_id . '::'};
451 Class::MOP::remove_metaclass_by_name($name);
456 # creating classes with MOP ...
459 my ( $class, @args ) = @_;
461 unshift @args, 'package' if @args % 2 == 1;
463 my (%options) = @args;
464 my $package_name = $options{package};
466 (ref $options{superclasses} eq 'ARRAY')
467 || confess "You must pass an ARRAY ref of superclasses"
468 if exists $options{superclasses};
470 (ref $options{attributes} eq 'ARRAY')
471 || confess "You must pass an ARRAY ref of attributes"
472 if exists $options{attributes};
474 (ref $options{methods} eq 'HASH')
475 || confess "You must pass a HASH ref of methods"
476 if exists $options{methods};
478 my (%initialize_options) = @args;
479 delete @initialize_options{qw(
487 my $meta = $class->initialize( $package_name => %initialize_options );
489 $meta->_instantiate_module( $options{version}, $options{authority} );
492 $meta->add_method('meta' => sub {
493 $class->initialize(ref($_[0]) || $_[0]);
496 $meta->superclasses(@{$options{superclasses}})
497 if exists $options{superclasses};
499 # process attributes first, so that they can
500 # install accessors, but locally defined methods
501 # can then overwrite them. It is maybe a little odd, but
502 # I think this should be the order of things.
503 if (exists $options{attributes}) {
504 foreach my $attr (@{$options{attributes}}) {
505 $meta->add_attribute($attr);
508 if (exists $options{methods}) {
509 foreach my $method_name (keys %{$options{methods}}) {
510 $meta->add_method($method_name, $options{methods}->{$method_name});
519 # all these attribute readers will be bootstrapped
520 # away in the Class::MOP bootstrap section
522 sub instance_metaclass { $_[0]->{'instance_metaclass'} }
523 sub immutable_trait { $_[0]->{'immutable_trait'} }
524 sub constructor_class { $_[0]->{'constructor_class'} }
525 sub constructor_name { $_[0]->{'constructor_name'} }
526 sub destructor_class { $_[0]->{'destructor_class'} }
528 # Instance Construction & Cloning
534 # we need to protect the integrity of the
535 # Class::MOP::Class singletons here, so we
536 # delegate this to &construct_class_instance
537 # which will deal with the singletons
538 return $class->_construct_class_instance(@_)
539 if $class->name->isa('Class::MOP::Class');
540 return $class->_construct_instance(@_);
543 sub _construct_instance {
545 my $params = @_ == 1 ? $_[0] : {@_};
546 my $meta_instance = $class->get_meta_instance();
548 # the code below is almost certainly incorrect
549 # but this is foreign inheritance, so we might
550 # have to kludge it in the end.
552 if (my $instance_class = blessed($params->{__INSTANCE__})) {
553 ($instance_class eq $class->name)
554 || confess "Objects passed as the __INSTANCE__ parameter must "
555 . "already be blessed into the correct class, but "
556 . "$params->{__INSTANCE__} is not a " . $class->name;
557 $instance = $params->{__INSTANCE__};
559 elsif (exists $params->{__INSTANCE__}) {
560 confess "The __INSTANCE__ parameter must be a blessed reference, not "
561 . $params->{__INSTANCE__};
564 $instance = $meta_instance->create_instance();
566 foreach my $attr ($class->get_all_attributes()) {
567 $attr->initialize_instance_slot($meta_instance, $instance, $params);
570 # this will only work for a HASH instance type
571 if ($class->is_anon_class) {
572 (reftype($instance) eq 'HASH')
573 || confess "Currently only HASH based instances are supported with instance of anon-classes";
575 # At some point we should make this official
576 # as a reserved slot name, but right now I am
577 # going to keep it here.
578 # my $RESERVED_MOP_SLOT = '__MOP__';
579 $instance->{'__MOP__'} = $class;
585 sub get_meta_instance {
587 $self->{'_meta_instance'} ||= $self->_create_meta_instance();
590 sub _create_meta_instance {
593 my $instance = $self->instance_metaclass->new(
594 associated_metaclass => $self,
595 attributes => [ $self->get_all_attributes() ],
598 $self->add_meta_instance_dependencies()
599 if $instance->is_dependent_on_superclasses();
606 my $instance = shift;
607 (blessed($instance) && $instance->isa($class->name))
608 || confess "You must pass an instance of the metaclass (" . (ref $class ? $class->name : $class) . "), not ($instance)";
611 # we need to protect the integrity of the
612 # Class::MOP::Class singletons here, they
613 # should not be cloned.
614 return $instance if $instance->isa('Class::MOP::Class');
615 $class->_clone_instance($instance, @_);
618 sub _clone_instance {
619 my ($class, $instance, %params) = @_;
621 || confess "You can only clone instances, ($instance) is not a blessed instance";
622 my $meta_instance = $class->get_meta_instance();
623 my $clone = $meta_instance->clone_instance($instance);
624 foreach my $attr ($class->get_all_attributes()) {
625 if ( defined( my $init_arg = $attr->init_arg ) ) {
626 if (exists $params{$init_arg}) {
627 $attr->set_value($clone, $params{$init_arg});
634 sub rebless_instance {
635 my ($self, $instance, %params) = @_;
637 my $old_metaclass = Class::MOP::class_of($instance);
639 my $old_class = $old_metaclass ? $old_metaclass->name : blessed($instance);
640 $self->name->isa($old_class)
641 || confess "You may rebless only into a subclass of ($old_class), of which (". $self->name .") isn't.";
643 $old_metaclass->rebless_instance_away($instance, $self, %params)
646 my $meta_instance = $self->get_meta_instance();
649 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
650 $meta_instance->rebless_instance_structure($_[1], $self);
652 foreach my $attr ( $self->get_all_attributes ) {
653 if ( $attr->has_value($instance) ) {
654 if ( defined( my $init_arg = $attr->init_arg ) ) {
655 $params{$init_arg} = $attr->get_value($instance)
656 unless exists $params{$init_arg};
659 $attr->set_value($instance, $attr->get_value($instance));
664 foreach my $attr ($self->get_all_attributes) {
665 $attr->initialize_instance_slot($meta_instance, $instance, \%params);
671 sub rebless_instance_back {
672 my ( $self, $instance ) = @_;
674 my $old_metaclass = Class::MOP::class_of($instance);
677 = $old_metaclass ? $old_metaclass->name : blessed($instance);
678 $old_class->isa( $self->name )
680 "You may rebless only into a superclass of ($old_class), of which ("
684 $old_metaclass->rebless_instance_away( $instance, $self )
687 my $meta_instance = $self->get_meta_instance;
689 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
690 $meta_instance->rebless_instance_structure( $_[1], $self );
692 for my $attr ( $old_metaclass->get_all_attributes ) {
693 next if $self->has_attribute( $attr->name );
694 $meta_instance->deinitialize_slot( $instance, $_ ) for $attr->slots;
700 sub rebless_instance_away {
701 # this intentionally does nothing, it is just a hook
704 sub _attach_attribute {
705 my ($self, $attribute) = @_;
706 $attribute->attach_to_class($self);
709 sub _post_add_attribute {
710 my ( $self, $attribute ) = @_;
712 $self->invalidate_meta_instances;
714 # invalidate package flag here
717 $attribute->install_accessors;
720 $self->remove_attribute( $attribute->name );
725 sub remove_attribute {
728 my $removed_attribute = $self->SUPER::remove_attribute(@_)
731 $self->invalidate_meta_instances;
733 $removed_attribute->remove_accessors;
734 $removed_attribute->detach_from_class;
736 return$removed_attribute;
739 sub find_attribute_by_name {
740 my ( $self, $attr_name ) = @_;
742 foreach my $class ( $self->linearized_isa ) {
743 # fetch the meta-class ...
744 my $meta = Class::MOP::Class->initialize($class);
745 return $meta->get_attribute($attr_name)
746 if $meta->has_attribute($attr_name);
752 sub get_all_attributes {
754 my %attrs = map { %{ Class::MOP::Class->initialize($_)->_attribute_map } }
755 reverse $self->linearized_isa;
756 return values %attrs;
763 my $var_spec = { sigil => '@', type => 'ARRAY', name => 'ISA' };
766 @{$self->get_package_symbol($var_spec)} = @supers;
769 # on 5.8 and below, we need to call
770 # a method to get Perl to detect
771 # a cycle in the class hierarchy
772 my $class = $self->name;
776 # we need to check the metaclass
777 # compatibility here so that we can
778 # be sure that the superclass is
779 # not potentially creating an issues
780 # we don't know about
782 $self->_check_metaclass_compatibility();
783 $self->_superclasses_updated();
785 @{$self->get_package_symbol($var_spec)};
788 sub _superclasses_updated {
790 $self->update_meta_instance_dependencies();
795 my $super_class = $self->name;
797 return @{ $super_class->mro::get_isarev() };
800 sub direct_subclasses {
802 my $super_class = $self->name;
807 } Class::MOP::Class->initialize($_)->superclasses
812 return @{ mro::get_linear_isa( (shift)->name ) };
815 sub class_precedence_list {
817 my $name = $self->name;
819 unless (Class::MOP::IS_RUNNING_ON_5_10()) {
821 # We need to check for circular inheritance here
822 # if we are are not on 5.10, cause 5.8 detects it
823 # late. This will do nothing if all is well, and
824 # blow up otherwise. Yes, it's an ugly hack, better
825 # suggestions are welcome.
827 ($name || return)->isa('This is a test for circular inheritance')
830 # if our mro is c3, we can
831 # just grab the linear_isa
832 if (mro::get_mro($name) eq 'c3') {
833 return @{ mro::get_linear_isa($name) }
837 # we can't grab the linear_isa for dfs
838 # since it has all the duplicates
843 Class::MOP::Class->initialize($_)->class_precedence_list()
844 } $self->superclasses()
852 my $fetch_and_prepare_method = sub {
853 my ($self, $method_name) = @_;
854 my $wrapped_metaclass = $self->wrapped_method_metaclass;
856 my $method = $self->get_method($method_name);
857 # if we dont have local ...
859 # try to find the next method
860 $method = $self->find_next_method_by_name($method_name);
861 # die if it does not exist
863 || confess "The method '$method_name' was not found in the inheritance hierarchy for " . $self->name;
864 # and now make sure to wrap it
865 # even if it is already wrapped
866 # because we need a new sub ref
867 $method = $wrapped_metaclass->wrap($method,
868 package_name => $self->name,
869 name => $method_name,
873 # now make sure we wrap it properly
874 $method = $wrapped_metaclass->wrap($method,
875 package_name => $self->name,
876 name => $method_name,
877 ) unless $method->isa($wrapped_metaclass);
879 $self->add_method($method_name => $method);
883 sub add_before_method_modifier {
884 my ($self, $method_name, $method_modifier) = @_;
885 (defined $method_name && length $method_name)
886 || confess "You must pass in a method name";
887 my $method = $fetch_and_prepare_method->($self, $method_name);
888 $method->add_before_modifier(
889 subname(':before' => $method_modifier)
893 sub add_after_method_modifier {
894 my ($self, $method_name, $method_modifier) = @_;
895 (defined $method_name && length $method_name)
896 || confess "You must pass in a method name";
897 my $method = $fetch_and_prepare_method->($self, $method_name);
898 $method->add_after_modifier(
899 subname(':after' => $method_modifier)
903 sub add_around_method_modifier {
904 my ($self, $method_name, $method_modifier) = @_;
905 (defined $method_name && length $method_name)
906 || confess "You must pass in a method name";
907 my $method = $fetch_and_prepare_method->($self, $method_name);
908 $method->add_around_modifier(
909 subname(':around' => $method_modifier)
914 # the methods above used to be named like this:
915 # ${pkg}::${method}:(before|after|around)
916 # but this proved problematic when using one modifier
917 # to wrap multiple methods (something which is likely
918 # to happen pretty regularly IMO). So instead of naming
919 # it like this, I have chosen to just name them purely
920 # with their modifier names, like so:
921 # :(before|after|around)
922 # The fact is that in a stack trace, it will be fairly
923 # evident from the context what method they are attached
924 # to, and so don't need the fully qualified name.
927 sub find_method_by_name {
928 my ($self, $method_name) = @_;
929 (defined $method_name && length $method_name)
930 || confess "You must define a method name to find";
931 foreach my $class ($self->linearized_isa) {
932 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
933 return $method if defined $method;
938 sub get_all_methods {
942 for my $class ( reverse $self->linearized_isa ) {
943 my $meta = Class::MOP::Class->initialize($class);
945 $methods{$_} = $meta->get_method($_)
946 for $meta->get_method_list;
949 return values %methods;
952 sub get_all_method_names {
955 return grep { !$uniq{$_}++ } map { Class::MOP::Class->initialize($_)->get_method_list } $self->linearized_isa;
958 sub find_all_methods_by_name {
959 my ($self, $method_name) = @_;
960 (defined $method_name && length $method_name)
961 || confess "You must define a method name to find";
963 foreach my $class ($self->linearized_isa) {
964 # fetch the meta-class ...
965 my $meta = Class::MOP::Class->initialize($class);
967 name => $method_name,
969 code => $meta->get_method($method_name)
970 } if $meta->has_method($method_name);
975 sub find_next_method_by_name {
976 my ($self, $method_name) = @_;
977 (defined $method_name && length $method_name)
978 || confess "You must define a method name to find";
979 my @cpl = $self->linearized_isa;
980 shift @cpl; # discard ourselves
981 foreach my $class (@cpl) {
982 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
983 return $method if defined $method;
988 sub update_meta_instance_dependencies {
991 if ( $self->{meta_instance_dependencies} ) {
992 return $self->add_meta_instance_dependencies;
996 sub add_meta_instance_dependencies {
999 $self->remove_meta_instance_dependencies;
1001 my @attrs = $self->get_all_attributes();
1004 my @classes = grep { not $seen{ $_->name }++ }
1005 map { $_->associated_class } @attrs;
1007 foreach my $class (@classes) {
1008 $class->add_dependent_meta_instance($self);
1011 $self->{meta_instance_dependencies} = \@classes;
1014 sub remove_meta_instance_dependencies {
1017 if ( my $classes = delete $self->{meta_instance_dependencies} ) {
1018 foreach my $class (@$classes) {
1019 $class->remove_dependent_meta_instance($self);
1029 sub add_dependent_meta_instance {
1030 my ( $self, $metaclass ) = @_;
1031 push @{ $self->{dependent_meta_instances} }, $metaclass;
1034 sub remove_dependent_meta_instance {
1035 my ( $self, $metaclass ) = @_;
1036 my $name = $metaclass->name;
1037 @$_ = grep { $_->name ne $name } @$_
1038 for $self->{dependent_meta_instances};
1041 sub invalidate_meta_instances {
1043 $_->invalidate_meta_instance()
1044 for $self, @{ $self->{dependent_meta_instances} };
1047 sub invalidate_meta_instance {
1049 undef $self->{_meta_instance};
1052 # check if we can reinitialize
1056 # if any local attr is defined
1057 return if $self->get_attribute_list;
1059 # or any non-declared methods
1060 for my $method ( map { $self->get_method($_) } $self->get_method_list ) {
1061 return if $method->isa("Class::MOP::Method::Generated");
1062 # FIXME do we need to enforce this too? return unless $method->isa( $self->method_metaclass );
1070 sub is_mutable { 1 }
1071 sub is_immutable { 0 }
1073 sub immutable_options { %{ $_[0]{__immutable}{options} || {} } }
1075 sub _immutable_options {
1076 my ( $self, @args ) = @_;
1079 inline_accessors => 1,
1080 inline_constructor => 1,
1081 inline_destructor => 0,
1083 immutable_trait => $self->immutable_trait,
1084 constructor_name => $self->constructor_name,
1085 constructor_class => $self->constructor_class,
1086 destructor_class => $self->destructor_class,
1091 sub make_immutable {
1092 my ( $self, @args ) = @_;
1094 if ( $self->is_mutable ) {
1095 $self->_initialize_immutable( $self->_immutable_options(@args) );
1096 $self->_rebless_as_immutable(@args);
1107 if ( $self->is_immutable ) {
1108 my @args = $self->immutable_options;
1109 $self->_rebless_as_mutable();
1110 $self->_remove_inlined_code(@args);
1111 delete $self->{__immutable};
1119 sub _rebless_as_immutable {
1120 my ( $self, @args ) = @_;
1122 $self->{__immutable}{original_class} = ref $self;
1124 bless $self => $self->_immutable_metaclass(@args);
1127 sub _immutable_metaclass {
1128 my ( $self, %args ) = @_;
1130 if ( my $class = $args{immutable_metaclass} ) {
1134 my $trait = $args{immutable_trait} = $self->immutable_trait
1135 || confess "no immutable trait specified for $self";
1137 my $meta = $self->meta;
1138 my $meta_attr = $meta->find_attribute_by_name("immutable_trait");
1142 if ( $meta_attr and $trait eq $meta_attr->default ) {
1143 # if the trait is the same as the default we try and pick a
1144 # predictable name for the immutable metaclass
1145 $class_name = 'Class::MOP::Class::Immutable::' . ref($self);
1148 $class_name = join '::', 'Class::MOP::Class::Immutable::CustomTrait',
1149 $trait, 'ForMetaClass', ref($self);
1153 if Class::MOP::is_class_loaded($class_name);
1155 # If the metaclass is a subclass of CMOP::Class which has had
1156 # metaclass roles applied (via Moose), then we want to make sure
1157 # that we preserve that anonymous class (see Fey::ORM for an
1158 # example of where this matters).
1159 my $meta_name = $meta->_real_ref_name;
1161 my $immutable_meta = $meta_name->create(
1163 superclasses => [ ref $self ],
1166 Class::MOP::load_class($trait);
1167 for my $meth ( Class::MOP::Class->initialize($trait)->get_all_methods ) {
1168 my $meth_name = $meth->name;
1170 if ( $immutable_meta->find_method_by_name( $meth_name ) ) {
1171 $immutable_meta->add_around_method_modifier( $meth_name, $meth->body );
1174 $immutable_meta->add_method( $meth_name, $meth->clone );
1178 $immutable_meta->make_immutable(
1179 inline_constructor => 0,
1180 inline_accessors => 0,
1186 sub _remove_inlined_code {
1189 $self->remove_method( $_->name ) for $self->_inlined_methods;
1191 delete $self->{__immutable}{inlined_methods};
1194 sub _inlined_methods { @{ $_[0]{__immutable}{inlined_methods} || [] } }
1196 sub _add_inlined_method {
1197 my ( $self, $method ) = @_;
1199 push @{ $self->{__immutable}{inlined_methods} ||= [] }, $method;
1202 sub _initialize_immutable {
1203 my ( $self, %args ) = @_;
1205 $self->{__immutable}{options} = \%args;
1206 $self->_install_inlined_code(%args);
1209 sub _install_inlined_code {
1210 my ( $self, %args ) = @_;
1213 $self->_inline_accessors(%args) if $args{inline_accessors};
1214 $self->_inline_constructor(%args) if $args{inline_constructor};
1215 $self->_inline_destructor(%args) if $args{inline_destructor};
1218 sub _rebless_as_mutable {
1221 bless $self, $self->_get_mutable_metaclass_name;
1226 sub _inline_accessors {
1229 foreach my $attr_name ( $self->get_attribute_list ) {
1230 $self->get_attribute($attr_name)->install_accessors(1);
1234 sub _inline_constructor {
1235 my ( $self, %args ) = @_;
1237 my $name = $args{constructor_name};
1238 # A class may not even have a constructor, and that's okay.
1239 return unless defined $name;
1241 if ( $self->has_method($name) && !$args{replace_constructor} ) {
1242 my $class = $self->name;
1243 warn "Not inlining a constructor for $class since it defines"
1244 . " its own constructor.\n"
1245 . "If you are certain you don't need to inline your"
1246 . " constructor, specify inline_constructor => 0 in your"
1247 . " call to $class->meta->make_immutable\n";
1251 my $constructor_class = $args{constructor_class};
1253 Class::MOP::load_class($constructor_class);
1255 my $constructor = $constructor_class->new(
1259 package_name => $self->name,
1263 if ( $args{replace_constructor} or $constructor->can_be_inlined ) {
1264 $self->add_method( $name => $constructor );
1265 $self->_add_inlined_method($constructor);
1269 sub _inline_destructor {
1270 my ( $self, %args ) = @_;
1272 ( exists $args{destructor_class} && defined $args{destructor_class} )
1273 || confess "The 'inline_destructor' option is present, but "
1274 . "no destructor class was specified";
1276 if ( $self->has_method('DESTROY') && ! $args{replace_destructor} ) {
1277 my $class = $self->name;
1278 warn "Not inlining a destructor for $class since it defines"
1279 . " its own destructor.\n";
1283 my $destructor_class = $args{destructor_class};
1285 Class::MOP::load_class($destructor_class);
1287 return unless $destructor_class->is_needed($self);
1289 my $destructor = $destructor_class->new(
1292 package_name => $self->name,
1296 if ( $args{replace_destructor} or $destructor->can_be_inlined ) {
1297 $self->add_method( 'DESTROY' => $destructor );
1298 $self->_add_inlined_method($destructor);
1310 Class::MOP::Class - Class Meta Object
1314 # assuming that class Foo
1315 # has been defined, you can
1317 # use this for introspection ...
1319 # add a method to Foo ...
1320 Foo->meta->add_method( 'bar' => sub {...} )
1322 # get a list of all the classes searched
1323 # the method dispatcher in the correct order
1324 Foo->meta->class_precedence_list()
1326 # remove a method from Foo
1327 Foo->meta->remove_method('bar');
1329 # or use this to actually create classes ...
1331 Class::MOP::Class->create(
1334 superclasses => ['Foo'],
1336 Class::MOP::Attribute->new('$bar'),
1337 Class::MOP::Attribute->new('$baz'),
1340 calculate_bar => sub {...},
1341 construct_baz => sub {...}
1348 The Class Protocol is the largest and most complex part of the
1349 Class::MOP meta-object protocol. It controls the introspection and
1350 manipulation of Perl 5 classes, and it can create them as well. The
1351 best way to understand what this module can do is to read the
1352 documentation for each of its methods.
1356 C<Class::MOP::Class> is a subclass of L<Class::MOP::Module>.
1360 =head2 Class construction
1362 These methods all create new C<Class::MOP::Class> objects. These
1363 objects can represent existing classes or they can be used to create
1364 new classes from scratch.
1366 The metaclass object for a given class is a singleton. If you attempt
1367 to create a metaclass for the same class twice, you will just get the
1372 =item B<< Class::MOP::Class->create($package_name, %options) >>
1374 This method creates a new C<Class::MOP::Class> object with the given
1375 package name. It accepts a number of options:
1381 An optional version number for the newly created package.
1385 An optional authority for the newly created package.
1387 =item * superclasses
1389 An optional array reference of superclass names.
1393 An optional hash reference of methods for the class. The keys of the
1394 hash reference are method names and values are subroutine references.
1398 An optional array reference of L<Class::MOP::Attribute> objects.
1402 =item B<< Class::MOP::Class->create_anon_class(%options) >>
1404 This method works just like C<< Class::MOP::Class->create >> but it
1405 creates an "anonymous" class. In fact, the class does have a name, but
1406 that name is a unique name generated internally by this module.
1408 It accepts the same C<superclasses>, C<methods>, and C<attributes>
1409 parameters that C<create> accepts.
1411 Anonymous classes are destroyed once the metaclass they are attached
1412 to goes out of scope, and will be removed from Perl's internal symbol
1415 All instances of an anonymous class keep a special reference to the
1416 metaclass object, which prevents the metaclass from going out of scope
1417 while any instances exist.
1419 This only works if the instance is based on a hash reference, however.
1421 =item B<< Class::MOP::Class->initialize($package_name, %options) >>
1423 This method will initialize a C<Class::MOP::Class> object for the
1424 named package. Unlike C<create>, this method I<will not> create a new
1427 The purpose of this method is to retrieve a C<Class::MOP::Class>
1428 object for introspecting an existing class.
1430 If an existing C<Class::MOP::Class> object exists for the named
1431 package, it will be returned, and any options provided will be
1434 If the object does not yet exist, it will be created.
1436 The valid options that can be passed to this method are
1437 C<attribute_metaclass>, C<method_metaclass>,
1438 C<wrapped_method_metaclass>, and C<instance_metaclass>. These are all
1439 optional, and default to the appropriate class in the C<Class::MOP>
1444 =head2 Object instance construction and cloning
1446 These methods are all related to creating and/or cloning object
1451 =item B<< $metaclass->clone_object($instance, %params) >>
1453 This method clones an existing object instance. Any parameters you
1454 provide are will override existing attribute values in the object.
1456 This is a convenience method for cloning an object instance, then
1457 blessing it into the appropriate package.
1459 You could implement a clone method in your class, using this method:
1462 my ($self, %params) = @_;
1463 $self->meta->clone_object($self, %params);
1466 =item B<< $metaclass->rebless_instance($instance, %params) >>
1468 This method changes the class of C<$instance> to the metaclass's class.
1470 You can only rebless an instance into a subclass of its current
1471 class. If you pass any additional parameters, these will be treated
1472 like constructor parameters and used to initialize the object's
1473 attributes. Any existing attributes that are already set will be
1476 Before reblessing the instance, this method will call
1477 C<rebless_instance_away> on the instance's current metaclass. This method
1478 will be passed the instance, the new metaclass, and any parameters
1479 specified to C<rebless_instance>. By default, C<rebless_instance_away>
1480 does nothing; it is merely a hook.
1482 =item B<< $metaclass->rebless_instance_back($instance) >>
1484 Does the same thing as C<rebless_instance>, except that you can only
1485 rebless an instance into one of its superclasses. Any attributes that
1486 do not exist in the superclass will be deinitialized.
1488 This is a much more dangerous operation than C<rebless_instance>,
1489 especially when multiple inheritance is involved, so use this carefully!
1491 =item B<< $metaclass->new_object(%params) >>
1493 This method is used to create a new object of the metaclass's
1494 class. Any parameters you provide are used to initialize the
1495 instance's attributes. A special C<__INSTANCE__> key can be passed to
1496 provide an already generated instance, rather than having Class::MOP
1497 generate it for you. This is mostly useful for using Class::MOP with
1498 foreign classes which generate instances using their own constructors.
1500 =item B<< $metaclass->instance_metaclass >>
1502 Returns the class name of the instance metaclass. See
1503 L<Class::MOP::Instance> for more information on the instance
1506 =item B<< $metaclass->get_meta_instance >>
1508 Returns an instance of the C<instance_metaclass> to be used in the
1509 construction of a new instance of the class.
1513 =head2 Informational predicates
1515 These are a few predicate methods for asking information about the
1520 =item B<< $metaclass->is_anon_class >>
1522 This returns true if the class was created by calling C<<
1523 Class::MOP::Class->create_anon_class >>.
1525 =item B<< $metaclass->is_mutable >>
1527 This returns true if the class is still mutable.
1529 =item B<< $metaclass->is_immutable >>
1531 This returns true if the class has been made immutable.
1533 =item B<< $metaclass->is_pristine >>
1535 A class is I<not> pristine if it has non-inherited attributes or if it
1536 has any generated methods.
1540 =head2 Inheritance Relationships
1544 =item B<< $metaclass->superclasses(@superclasses) >>
1546 This is a read-write accessor which represents the superclass
1547 relationships of the metaclass's class.
1549 This is basically sugar around getting and setting C<@ISA>.
1551 =item B<< $metaclass->class_precedence_list >>
1553 This returns a list of all of the class's ancestor classes. The
1554 classes are returned in method dispatch order.
1556 =item B<< $metaclass->linearized_isa >>
1558 This returns a list based on C<class_precedence_list> but with all
1561 =item B<< $metaclass->subclasses >>
1563 This returns a list of all subclasses for this class, even indirect
1566 =item B<< $metaclass->direct_subclasses >>
1568 This returns a list of immediate subclasses for this class, which does not
1569 include indirect subclasses.
1573 =head2 Method introspection and creation
1575 These methods allow you to introspect a class's methods, as well as
1576 add, remove, or change methods.
1578 Determining what is truly a method in a Perl 5 class requires some
1579 heuristics (aka guessing).
1581 Methods defined outside the package with a fully qualified name (C<sub
1582 Package::name { ... }>) will be included. Similarly, methods named
1583 with a fully qualified name using L<Sub::Name> are also included.
1585 However, we attempt to ignore imported functions.
1587 Ultimately, we are using heuristics to determine what truly is a
1588 method in a class, and these heuristics may get the wrong answer in
1589 some edge cases. However, for most "normal" cases the heuristics work
1594 =item B<< $metaclass->get_method($method_name) >>
1596 This will return a L<Class::MOP::Method> for the specified
1597 C<$method_name>. If the class does not have the specified method, it
1600 =item B<< $metaclass->has_method($method_name) >>
1602 Returns a boolean indicating whether or not the class defines the
1603 named method. It does not include methods inherited from parent
1606 =item B<< $metaclass->get_method_list >>
1608 This will return a list of method I<names> for all methods defined in
1611 =item B<< $metaclass->add_method($method_name, $method) >>
1613 This method takes a method name and a subroutine reference, and adds
1614 the method to the class.
1616 The subroutine reference can be a L<Class::MOP::Method>, and you are
1617 strongly encouraged to pass a meta method object instead of a code
1618 reference. If you do so, that object gets stored as part of the
1619 class's method map directly. If not, the meta information will have to
1620 be recreated later, and may be incorrect.
1622 If you provide a method object, this method will clone that object if
1623 the object's package name does not match the class name. This lets us
1624 track the original source of any methods added from other classes
1625 (notably Moose roles).
1627 =item B<< $metaclass->remove_method($method_name) >>
1629 Remove the named method from the class. This method returns the
1630 L<Class::MOP::Method> object for the method.
1632 =item B<< $metaclass->method_metaclass >>
1634 Returns the class name of the method metaclass, see
1635 L<Class::MOP::Method> for more information on the method metaclass.
1637 =item B<< $metaclass->wrapped_method_metaclass >>
1639 Returns the class name of the wrapped method metaclass, see
1640 L<Class::MOP::Method::Wrapped> for more information on the wrapped
1643 =item B<< $metaclass->get_all_methods >>
1645 This will traverse the inheritance hierarchy and return a list of all
1646 the L<Class::MOP::Method> objects for this class and its parents.
1648 =item B<< $metaclass->find_method_by_name($method_name) >>
1650 This will return a L<Class::MOP::Method> for the specified
1651 C<$method_name>. If the class does not have the specified method, it
1654 Unlike C<get_method>, this method I<will> look for the named method in
1657 =item B<< $metaclass->get_all_method_names >>
1659 This will return a list of method I<names> for all of this class's
1660 methods, including inherited methods.
1662 =item B<< $metaclass->find_all_methods_by_name($method_name) >>
1664 This method looks for the named method in the class and all of its
1665 parents. It returns every matching method it finds in the inheritance
1666 tree, so it returns a list of methods.
1668 Each method is returned as a hash reference with three keys. The keys
1669 are C<name>, C<class>, and C<code>. The C<code> key has a
1670 L<Class::MOP::Method> object as its value.
1672 The list of methods is distinct.
1674 =item B<< $metaclass->find_next_method_by_name($method_name) >>
1676 This method returns the first method in any superclass matching the
1677 given name. It is effectively the method that C<SUPER::$method_name>
1682 =head2 Attribute introspection and creation
1684 Because Perl 5 does not have a core concept of attributes in classes,
1685 we can only return information about attributes which have been added
1686 via this class's methods. We cannot discover information about
1687 attributes which are defined in terms of "regular" Perl 5 methods.
1691 =item B<< $metaclass->get_attribute($attribute_name) >>
1693 This will return a L<Class::MOP::Attribute> for the specified
1694 C<$attribute_name>. If the class does not have the specified
1695 attribute, it returns C<undef>.
1697 NOTE that get_attribute does not search superclasses, for that you
1698 need to use C<find_attribute_by_name>.
1700 =item B<< $metaclass->has_attribute($attribute_name) >>
1702 Returns a boolean indicating whether or not the class defines the
1703 named attribute. It does not include attributes inherited from parent
1706 =item B<< $metaclass->get_attribute_list >>
1708 This will return a list of attributes I<names> for all attributes
1709 defined in this class.
1711 =item B<< $metaclass->get_all_attributes >>
1713 This will traverse the inheritance hierarchy and return a list of all
1714 the L<Class::MOP::Attribute> objects for this class and its parents.
1716 =item B<< $metaclass->find_attribute_by_name($attribute_name) >>
1718 This will return a L<Class::MOP::Attribute> for the specified
1719 C<$attribute_name>. If the class does not have the specified
1720 attribute, it returns C<undef>.
1722 Unlike C<get_attribute>, this attribute I<will> look for the named
1723 attribute in superclasses.
1725 =item B<< $metaclass->add_attribute(...) >>
1727 This method accepts either an existing L<Class::MOP::Attribute>
1728 object or parameters suitable for passing to that class's C<new>
1731 The attribute provided will be added to the class.
1733 Any accessor methods defined by the attribute will be added to the
1734 class when the attribute is added.
1736 If an attribute of the same name already exists, the old attribute
1737 will be removed first.
1739 =item B<< $metaclass->remove_attribute($attribute_name) >>
1741 This will remove the named attribute from the class, and
1742 L<Class::MOP::Attribute> object.
1744 Removing an attribute also removes any accessor methods defined by the
1747 However, note that removing an attribute will only affect I<future>
1748 object instances created for this class, not existing instances.
1750 =item B<< $metaclass->attribute_metaclass >>
1752 Returns the class name of the attribute metaclass for this class. By
1753 default, this is L<Class::MOP::Attribute>.
1757 =head2 Class Immutability
1759 Making a class immutable "freezes" the class definition. You can no
1760 longer call methods which alter the class, such as adding or removing
1761 methods or attributes.
1763 Making a class immutable lets us optimize the class by inlining some
1764 methods, and also allows us to optimize some methods on the metaclass
1767 After immutabilization, the metaclass object will cache most informational
1768 methods that returns information about methods or attributes. Methods which
1769 would alter the class, such as C<add_attribute> and C<add_method>, will
1770 throw an error on an immutable metaclass object.
1772 The immutabilization system in L<Moose> takes much greater advantage
1773 of the inlining features than Class::MOP itself does.
1777 =item B<< $metaclass->make_immutable(%options) >>
1779 This method will create an immutable transformer and use it to make
1780 the class and its metaclass object immutable.
1782 This method accepts the following options:
1786 =item * inline_accessors
1788 =item * inline_constructor
1790 =item * inline_destructor
1792 These are all booleans indicating whether the specified method(s)
1795 By default, accessors and the constructor are inlined, but not the
1798 =item * immutable_trait
1800 The name of a class which will be used as a parent class for the
1801 metaclass object being made immutable. This "trait" implements the
1802 post-immutability functionality of the metaclass (but not the
1803 transformation itself).
1805 This defaults to L<Class::MOP::Class::Immutable::Trait>.
1807 =item * constructor_name
1809 This is the constructor method name. This defaults to "new".
1811 =item * constructor_class
1813 The name of the method metaclass for constructors. It will be used to
1814 generate the inlined constructor. This defaults to
1815 "Class::MOP::Method::Constructor".
1817 =item * replace_constructor
1819 This is a boolean indicating whether an existing constructor should be
1820 replaced when inlining a constructor. This defaults to false.
1822 =item * destructor_class
1824 The name of the method metaclass for destructors. It will be used to
1825 generate the inlined destructor. This defaults to
1826 "Class::MOP::Method::Denstructor".
1828 =item * replace_destructor
1830 This is a boolean indicating whether an existing destructor should be
1831 replaced when inlining a destructor. This defaults to false.
1835 =item B<< $metaclass->immutable_options >>
1837 Returns a hash of the options used when making the class immutable, including
1838 both defaults and anything supplied by the user in the call to C<<
1839 $metaclass->make_immutable >>. This is useful if you need to temporarily make
1840 a class mutable and then restore immutability as it was before.
1842 =item B<< $metaclass->make_mutable >>
1844 Calling this method reverse the immutabilization transformation.
1848 =head2 Method Modifiers
1850 Method modifiers are hooks which allow a method to be wrapped with
1851 I<before>, I<after> and I<around> method modifiers. Every time a
1852 method is called, its modifiers are also called.
1854 A class can modify its own methods, as well as methods defined in
1857 =head3 How method modifiers work?
1859 Method modifiers work by wrapping the original method and then
1860 replacing it in the class's symbol table. The wrappers will handle
1861 calling all the modifiers in the appropriate order and preserving the
1862 calling context for the original method.
1864 The return values of C<before> and C<after> modifiers are
1865 ignored. This is because their purpose is B<not> to filter the input
1866 and output of the primary method (this is done with an I<around>
1869 This may seem like an odd restriction to some, but doing this allows
1870 for simple code to be added at the beginning or end of a method call
1871 without altering the function of the wrapped method or placing any
1872 extra responsibility on the code of the modifier.
1874 Of course if you have more complex needs, you can use the C<around>
1875 modifier which allows you to change both the parameters passed to the
1876 wrapped method, as well as its return value.
1878 Before and around modifiers are called in last-defined-first-called
1879 order, while after modifiers are called in first-defined-first-called
1880 order. So the call tree might looks something like this:
1892 =head3 What is the performance impact?
1894 Of course there is a performance cost associated with method
1895 modifiers, but we have made every effort to make that cost directly
1896 proportional to the number of modifier features you use.
1898 The wrapping method does its best to B<only> do as much work as it
1899 absolutely needs to. In order to do this we have moved some of the
1900 performance costs to set-up time, where they are easier to amortize.
1902 All this said, our benchmarks have indicated the following:
1904 simple wrapper with no modifiers 100% slower
1905 simple wrapper with simple before modifier 400% slower
1906 simple wrapper with simple after modifier 450% slower
1907 simple wrapper with simple around modifier 500-550% slower
1908 simple wrapper with all 3 modifiers 1100% slower
1910 These numbers may seem daunting, but you must remember, every feature
1911 comes with some cost. To put things in perspective, just doing a
1912 simple C<AUTOLOAD> which does nothing but extract the name of the
1913 method called and return it costs about 400% over a normal method
1918 =item B<< $metaclass->add_before_method_modifier($method_name, $code) >>
1920 This wraps the specified method with the supplied subroutine
1921 reference. The modifier will be called as a method itself, and will
1922 receive the same arguments as are passed to the method.
1924 When the modifier exits, the wrapped method will be called.
1926 The return value of the modifier will be ignored.
1928 =item B<< $metaclass->add_after_method_modifier($method_name, $code) >>
1930 This wraps the specified method with the supplied subroutine
1931 reference. The modifier will be called as a method itself, and will
1932 receive the same arguments as are passed to the method.
1934 When the wrapped methods exits, the modifier will be called.
1936 The return value of the modifier will be ignored.
1938 =item B<< $metaclass->add_around_method_modifier($method_name, $code) >>
1940 This wraps the specified method with the supplied subroutine
1943 The first argument passed to the modifier will be a subroutine
1944 reference to the wrapped method. The second argument is the object,
1945 and after that come any arguments passed when the method is called.
1947 The around modifier can choose to call the original method, as well as
1948 what arguments to pass if it does so.
1950 The return value of the modifier is what will be seen by the caller.
1954 =head2 Introspection
1958 =item B<< Class::MOP::Class->meta >>
1960 This will return a L<Class::MOP::Class> instance for this class.
1962 It should also be noted that L<Class::MOP> will actually bootstrap
1963 this module by installing a number of attribute meta-objects into its
1970 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1972 =head1 COPYRIGHT AND LICENSE
1974 Copyright 2006-2010 by Infinity Interactive, Inc.
1976 L<http://www.iinteractive.com>
1978 This library is free software; you can redistribute it and/or modify
1979 it under the same terms as Perl itself.