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::Method::Meta;
12 use Class::MOP::MiniTrait;
15 use Scalar::Util 'blessed', 'reftype', 'weaken';
16 use Sub::Name 'subname';
17 use Devel::GlobalDestruction 'in_global_destruction';
19 use List::MoreUtils 'all';
21 our $VERSION = '1.09';
22 $VERSION = eval $VERSION;
23 our $AUTHORITY = 'cpan:STEVAN';
25 use base 'Class::MOP::Module',
26 'Class::MOP::Mixin::HasAttributes',
27 'Class::MOP::Mixin::HasMethods';
37 $package_name = shift;
40 $package_name = $options{package};
43 ($package_name && !ref($package_name))
44 || confess "You must pass a package name and it cannot be blessed";
46 return Class::MOP::get_metaclass_by_name($package_name)
47 || $class->_construct_class_instance(package => $package_name, @_);
51 my ( $class, @args ) = @_;
52 unshift @args, "package" if @args % 2;
54 my $old_metaclass = blessed($options{package})
56 : Class::MOP::get_metaclass_by_name($options{package});
57 $old_metaclass->_remove_generated_metaobjects
58 if $old_metaclass && $old_metaclass->isa('Class::MOP::Class');
59 my $new_metaclass = $class->SUPER::reinitialize(@args);
60 $new_metaclass->_restore_metaobjects_from($old_metaclass)
61 if $old_metaclass && $old_metaclass->isa('Class::MOP::Class');
62 return $new_metaclass;
65 # NOTE: (meta-circularity)
66 # this is a special form of _construct_instance
67 # (see below), which is used to construct class
68 # meta-object instances for any Class::MOP::*
69 # class. All other classes will use the more
70 # normal &construct_instance.
71 sub _construct_class_instance {
73 my $options = @_ == 1 ? $_[0] : {@_};
74 my $package_name = $options->{package};
75 (defined $package_name && $package_name)
76 || confess "You must pass a package name";
78 # return the metaclass if we have it cached,
79 # and it is still defined (it has not been
80 # reaped by DESTROY yet, which can happen
81 # annoyingly enough during global destruction)
83 if (defined(my $meta = Class::MOP::get_metaclass_by_name($package_name))) {
89 ? $class->_real_ref_name
92 # now create the metaclass
94 if ($class eq 'Class::MOP::Class') {
95 $meta = $class->_new($options);
99 # it is safe to use meta here because
100 # class will always be a subclass of
101 # Class::MOP::Class, which defines meta
102 $meta = $class->meta->_construct_instance($options)
105 # and check the metaclass compatibility
106 $meta->_check_metaclass_compatibility();
108 Class::MOP::store_metaclass_by_name($package_name, $meta);
111 # we need to weaken any anon classes
112 # so that they can call DESTROY properly
113 Class::MOP::weaken_metaclass($package_name) if $meta->is_anon_class;
121 # NOTE: we need to deal with the possibility of class immutability here,
122 # and then get the name of the class appropriately
123 return $self->is_immutable
124 ? $self->_get_mutable_metaclass_name()
128 sub _add_meta_method {
131 'meta' => Class::MOP::Method::Meta->wrap(
133 package_name => $self->name,
134 associated_metaclass => $self,
142 return Class::MOP::Class->initialize($class)->new_object(@_)
143 if $class ne __PACKAGE__;
145 my $options = @_ == 1 ? $_[0] : {@_};
148 # inherited from Class::MOP::Package
149 'package' => $options->{package},
152 # since the following attributes will
153 # actually be loaded from the symbol
154 # table, and actually bypass the instance
155 # entirely, we can just leave these things
156 # listed here for reference, because they
157 # should not actually have a value associated
159 'namespace' => \undef,
162 # inherited from Class::MOP::Module
164 'authority' => \undef,
166 # defined in Class::MOP::Class
167 'superclasses' => \undef,
170 'attribute_metaclass' =>
171 ( $options->{'attribute_metaclass'} || 'Class::MOP::Attribute' ),
172 'method_metaclass' =>
173 ( $options->{'method_metaclass'} || 'Class::MOP::Method' ),
174 'wrapped_method_metaclass' => (
175 $options->{'wrapped_method_metaclass'}
176 || 'Class::MOP::Method::Wrapped'
178 'instance_metaclass' =>
179 ( $options->{'instance_metaclass'} || 'Class::MOP::Instance' ),
180 'immutable_trait' => (
181 $options->{'immutable_trait'}
182 || 'Class::MOP::Class::Immutable::Trait'
184 'constructor_name' => ( $options->{constructor_name} || 'new' ),
185 'constructor_class' => (
186 $options->{constructor_class} || 'Class::MOP::Method::Constructor'
188 'destructor_class' => $options->{destructor_class},
192 sub reset_package_cache_flag { (shift)->{'_package_cache_flag'} = undef }
193 sub update_package_cache_flag {
196 # we can manually update the cache number
197 # since we are actually adding the method
198 # to our cache as well. This avoids us
199 # having to regenerate the method_map.
201 $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);
204 ## Metaclass compatibility
206 my %base_metaclass = (
207 attribute_metaclass => 'Class::MOP::Attribute',
208 method_metaclass => 'Class::MOP::Method',
209 wrapped_method_metaclass => 'Class::MOP::Method::Wrapped',
210 instance_metaclass => 'Class::MOP::Instance',
211 constructor_class => 'Class::MOP::Method::Constructor',
212 destructor_class => 'Class::MOP::Method::Destructor',
215 sub _base_metaclasses { %base_metaclass }
218 sub _check_metaclass_compatibility {
221 my @superclasses = $self->superclasses
224 $self->_fix_metaclass_incompatibility(@superclasses);
226 my %base_metaclass = $self->_base_metaclasses;
228 # this is always okay ...
230 if ref($self) eq 'Class::MOP::Class'
232 my $meta = $self->$_;
233 !defined($meta) || $meta eq $base_metaclass{$_};
235 keys %base_metaclass;
237 for my $superclass (@superclasses) {
238 $self->_check_class_metaclass_compatibility($superclass);
241 for my $metaclass_type ( keys %base_metaclass ) {
242 next unless defined $self->$metaclass_type;
243 for my $superclass (@superclasses) {
244 $self->_check_single_metaclass_compatibility( $metaclass_type,
250 sub _check_class_metaclass_compatibility {
252 my ( $superclass_name ) = @_;
254 if (!$self->_class_metaclass_is_compatible($superclass_name)) {
255 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
257 my $super_meta_type = $super_meta->_real_ref_name;
259 confess "The metaclass of " . $self->name . " ("
260 . (ref($self)) . ")" . " is not compatible with "
261 . "the metaclass of its superclass, "
262 . $superclass_name . " (" . ($super_meta_type) . ")";
266 sub _class_metaclass_is_compatible {
268 my ( $superclass_name ) = @_;
270 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
273 my $super_meta_name = $super_meta->_real_ref_name;
275 return $self->_is_compatible_with($super_meta_name);
278 sub _check_single_metaclass_compatibility {
280 my ( $metaclass_type, $superclass_name ) = @_;
282 if (!$self->_single_metaclass_is_compatible($metaclass_type, $superclass_name)) {
283 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
284 my $metaclass_type_name = $metaclass_type;
285 $metaclass_type_name =~ s/_(?:meta)?class$//;
286 $metaclass_type_name =~ s/_/ /g;
287 confess "The $metaclass_type_name metaclass for "
288 . $self->name . " (" . ($self->$metaclass_type)
289 . ")" . " is not compatible with the "
290 . "$metaclass_type_name metaclass of its "
291 . "superclass, $superclass_name ("
292 . ($super_meta->$metaclass_type) . ")";
296 sub _single_metaclass_is_compatible {
298 my ( $metaclass_type, $superclass_name ) = @_;
300 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
303 # for instance, Moose::Meta::Class has a error_class attribute, but
304 # Class::MOP::Class doesn't - this shouldn't be an error
305 return 1 unless $super_meta->can($metaclass_type);
306 # for instance, Moose::Meta::Class has a destructor_class, but
307 # Class::MOP::Class doesn't - this shouldn't be an error
308 return 1 unless defined $super_meta->$metaclass_type;
309 # if metaclass is defined in superclass but not here, it's not compatible
310 # this is a really odd case
311 return 0 unless defined $self->$metaclass_type;
313 return $self->$metaclass_type->_is_compatible_with($super_meta->$metaclass_type);
316 sub _fix_metaclass_incompatibility {
318 my @supers = map { Class::MOP::Class->initialize($_) } @_;
321 for my $super (@supers) {
323 if $self->_can_fix_metaclass_incompatibility($super);
325 return unless $necessary;
327 for my $super (@supers) {
328 if (!$self->_class_metaclass_is_compatible($super->name)) {
329 $self->_fix_class_metaclass_incompatibility($super);
333 my %base_metaclass = $self->_base_metaclasses;
334 for my $metaclass_type (keys %base_metaclass) {
335 for my $super (@supers) {
336 if (!$self->_single_metaclass_is_compatible($metaclass_type, $super->name)) {
337 $self->_fix_single_metaclass_incompatibility(
338 $metaclass_type, $super
345 sub _can_fix_metaclass_incompatibility {
347 my ($super_meta) = @_;
349 return 1 if $self->_class_metaclass_can_be_made_compatible($super_meta);
351 my %base_metaclass = $self->_base_metaclasses;
352 for my $metaclass_type (keys %base_metaclass) {
353 return 1 if $self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type);
359 sub _class_metaclass_can_be_made_compatible {
361 my ($super_meta) = @_;
363 return $self->_can_be_made_compatible_with($super_meta->_real_ref_name);
366 sub _single_metaclass_can_be_made_compatible {
368 my ($super_meta, $metaclass_type) = @_;
370 my $specific_meta = $self->$metaclass_type;
372 return unless $super_meta->can($metaclass_type);
373 my $super_specific_meta = $super_meta->$metaclass_type;
375 # for instance, Moose::Meta::Class has a destructor_class, but
376 # Class::MOP::Class doesn't - this shouldn't be an error
377 return unless defined $super_specific_meta;
379 # if metaclass is defined in superclass but not here, it's fixable
380 # this is a really odd case
381 return 1 unless defined $specific_meta;
383 return 1 if $specific_meta->_can_be_made_compatible_with($super_specific_meta);
386 sub _fix_class_metaclass_incompatibility {
388 my ( $super_meta ) = @_;
390 if ($self->_class_metaclass_can_be_made_compatible($super_meta)) {
392 || confess "Can't fix metaclass incompatibility for "
394 . " because it is not pristine.";
396 my $super_meta_name = $super_meta->_real_ref_name;
398 $self->_make_compatible_with($super_meta_name);
402 sub _fix_single_metaclass_incompatibility {
404 my ( $metaclass_type, $super_meta ) = @_;
406 if ($self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type)) {
408 || confess "Can't fix metaclass incompatibility for "
410 . " because it is not pristine.";
412 my $new_metaclass = $self->$metaclass_type
413 ? $self->$metaclass_type->_get_compatible_metaclass($super_meta->$metaclass_type)
414 : $super_meta->$metaclass_type;
415 $self->{$metaclass_type} = $new_metaclass;
419 sub _restore_metaobjects_from {
423 $self->_restore_metamethods_from($old_meta);
424 $self->_restore_metaattributes_from($old_meta);
427 sub _remove_generated_metaobjects {
430 for my $attr (map { $self->get_attribute($_) } $self->get_attribute_list) {
431 $attr->remove_accessors;
439 # this should be sufficient, if you have a
440 # use case where it is not, write a test and
442 my $ANON_CLASS_SERIAL = 0;
445 # we need a sufficiently annoying prefix
446 # this should suffice for now, this is
447 # used in a couple of places below, so
448 # need to put it up here for now.
449 my $ANON_CLASS_PREFIX = 'Class::MOP::Class::__ANON__::SERIAL::';
453 no warnings 'uninitialized';
454 $self->name =~ /^$ANON_CLASS_PREFIX/o;
457 sub create_anon_class {
458 my ($class, %options) = @_;
459 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
460 return $class->create($package_name, %options);
464 # this will only get called for
465 # anon-classes, all other calls
466 # are assumed to occur during
467 # global destruction and so don't
468 # really need to be handled explicitly
472 return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated
474 no warnings 'uninitialized';
475 my $name = $self->name;
476 return unless $name =~ /^$ANON_CLASS_PREFIX/o;
478 # Moose does a weird thing where it replaces the metaclass for
479 # class when fixing metaclass incompatibility. In that case,
480 # we don't want to clean out the namespace now. We can detect
481 # that because Moose will explicitly update the singleton
482 # cache in Class::MOP.
483 my $current_meta = Class::MOP::get_metaclass_by_name($name);
484 return if $current_meta ne $self;
486 my ($serial_id) = ($name =~ /^$ANON_CLASS_PREFIX(\d+)/o);
488 @{$name . '::ISA'} = ();
489 %{$name . '::'} = ();
490 delete ${$ANON_CLASS_PREFIX}{$serial_id . '::'};
492 Class::MOP::remove_metaclass_by_name($name);
497 # creating classes with MOP ...
500 my ( $class, @args ) = @_;
502 unshift @args, 'package' if @args % 2 == 1;
504 my (%options) = @args;
505 my $package_name = $options{package};
507 (ref $options{superclasses} eq 'ARRAY')
508 || confess "You must pass an ARRAY ref of superclasses"
509 if exists $options{superclasses};
511 (ref $options{attributes} eq 'ARRAY')
512 || confess "You must pass an ARRAY ref of attributes"
513 if exists $options{attributes};
515 (ref $options{methods} eq 'HASH')
516 || confess "You must pass a HASH ref of methods"
517 if exists $options{methods};
519 my (%initialize_options) = @args;
520 delete @initialize_options{qw(
529 my $meta = $class->initialize( $package_name => %initialize_options );
531 $meta->_instantiate_module( $options{version}, $options{authority} );
533 $meta->_add_meta_method unless $options{no_meta};
535 $meta->superclasses(@{$options{superclasses}})
536 if exists $options{superclasses};
538 # process attributes first, so that they can
539 # install accessors, but locally defined methods
540 # can then overwrite them. It is maybe a little odd, but
541 # I think this should be the order of things.
542 if (exists $options{attributes}) {
543 foreach my $attr (@{$options{attributes}}) {
544 $meta->add_attribute($attr);
547 if (exists $options{methods}) {
548 foreach my $method_name (keys %{$options{methods}}) {
549 $meta->add_method($method_name, $options{methods}->{$method_name});
558 # all these attribute readers will be bootstrapped
559 # away in the Class::MOP bootstrap section
561 sub instance_metaclass { $_[0]->{'instance_metaclass'} }
562 sub immutable_trait { $_[0]->{'immutable_trait'} }
563 sub constructor_class { $_[0]->{'constructor_class'} }
564 sub constructor_name { $_[0]->{'constructor_name'} }
565 sub destructor_class { $_[0]->{'destructor_class'} }
567 # Instance Construction & Cloning
573 # we need to protect the integrity of the
574 # Class::MOP::Class singletons here, so we
575 # delegate this to &construct_class_instance
576 # which will deal with the singletons
577 return $class->_construct_class_instance(@_)
578 if $class->name->isa('Class::MOP::Class');
579 return $class->_construct_instance(@_);
582 sub _construct_instance {
584 my $params = @_ == 1 ? $_[0] : {@_};
585 my $meta_instance = $class->get_meta_instance();
587 # the code below is almost certainly incorrect
588 # but this is foreign inheritance, so we might
589 # have to kludge it in the end.
591 if (my $instance_class = blessed($params->{__INSTANCE__})) {
592 ($instance_class eq $class->name)
593 || confess "Objects passed as the __INSTANCE__ parameter must "
594 . "already be blessed into the correct class, but "
595 . "$params->{__INSTANCE__} is not a " . $class->name;
596 $instance = $params->{__INSTANCE__};
598 elsif (exists $params->{__INSTANCE__}) {
599 confess "The __INSTANCE__ parameter must be a blessed reference, not "
600 . $params->{__INSTANCE__};
603 $instance = $meta_instance->create_instance();
605 foreach my $attr ($class->get_all_attributes()) {
606 $attr->initialize_instance_slot($meta_instance, $instance, $params);
609 # this will only work for a HASH instance type
610 if ($class->is_anon_class) {
611 (reftype($instance) eq 'HASH')
612 || confess "Currently only HASH based instances are supported with instance of anon-classes";
614 # At some point we should make this official
615 # as a reserved slot name, but right now I am
616 # going to keep it here.
617 # my $RESERVED_MOP_SLOT = '__MOP__';
618 $instance->{'__MOP__'} = $class;
624 sub get_meta_instance {
626 $self->{'_meta_instance'} ||= $self->_create_meta_instance();
629 sub _create_meta_instance {
632 my $instance = $self->instance_metaclass->new(
633 associated_metaclass => $self,
634 attributes => [ $self->get_all_attributes() ],
637 $self->add_meta_instance_dependencies()
638 if $instance->is_dependent_on_superclasses();
643 sub inline_create_instance {
646 return $self->get_meta_instance->inline_create_instance(@_);
649 sub inline_rebless_instance {
652 return $self->get_meta_instance->inline_rebless_instance_structure(@_);
657 my $instance = shift;
658 (blessed($instance) && $instance->isa($class->name))
659 || confess "You must pass an instance of the metaclass (" . (ref $class ? $class->name : $class) . "), not ($instance)";
662 # we need to protect the integrity of the
663 # Class::MOP::Class singletons here, they
664 # should not be cloned.
665 return $instance if $instance->isa('Class::MOP::Class');
666 $class->_clone_instance($instance, @_);
669 sub _clone_instance {
670 my ($class, $instance, %params) = @_;
672 || confess "You can only clone instances, ($instance) is not a blessed instance";
673 my $meta_instance = $class->get_meta_instance();
674 my $clone = $meta_instance->clone_instance($instance);
675 foreach my $attr ($class->get_all_attributes()) {
676 if ( defined( my $init_arg = $attr->init_arg ) ) {
677 if (exists $params{$init_arg}) {
678 $attr->set_value($clone, $params{$init_arg});
685 sub _force_rebless_instance {
686 my ($self, $instance, %params) = @_;
687 my $old_metaclass = Class::MOP::class_of($instance);
689 $old_metaclass->rebless_instance_away($instance, $self, %params)
692 my $meta_instance = $self->get_meta_instance;
695 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
696 $meta_instance->rebless_instance_structure($_[1], $self);
698 $self->_fixup_attributes_after_rebless($instance, $old_metaclass, %params);
701 sub rebless_instance {
702 my ($self, $instance, %params) = @_;
703 my $old_metaclass = Class::MOP::class_of($instance);
705 my $old_class = $old_metaclass ? $old_metaclass->name : blessed($instance);
706 $self->name->isa($old_class)
707 || confess "You may rebless only into a subclass of ($old_class), of which (". $self->name .") isn't.";
709 $self->_force_rebless_instance($_[1], %params);
714 sub rebless_instance_back {
715 my ( $self, $instance ) = @_;
716 my $old_metaclass = Class::MOP::class_of($instance);
719 = $old_metaclass ? $old_metaclass->name : blessed($instance);
720 $old_class->isa( $self->name )
722 "You may rebless only into a superclass of ($old_class), of which ("
726 $self->_force_rebless_instance($_[1]);
731 sub rebless_instance_away {
732 # this intentionally does nothing, it is just a hook
735 sub _fixup_attributes_after_rebless {
737 my ($instance, $rebless_from, %params) = @_;
738 my $meta_instance = $self->get_meta_instance;
740 for my $attr ( $rebless_from->get_all_attributes ) {
741 next if $self->find_attribute_by_name( $attr->name );
742 $meta_instance->deinitialize_slot( $instance, $_ ) for $attr->slots;
745 foreach my $attr ( $self->get_all_attributes ) {
746 if ( $attr->has_value($instance) ) {
747 if ( defined( my $init_arg = $attr->init_arg ) ) {
748 $params{$init_arg} = $attr->get_value($instance)
749 unless exists $params{$init_arg};
752 $attr->set_value($instance, $attr->get_value($instance));
757 foreach my $attr ($self->get_all_attributes) {
758 $attr->initialize_instance_slot($meta_instance, $instance, \%params);
762 sub _attach_attribute {
763 my ($self, $attribute) = @_;
764 $attribute->attach_to_class($self);
767 sub _post_add_attribute {
768 my ( $self, $attribute ) = @_;
770 $self->invalidate_meta_instances;
772 # invalidate package flag here
775 $attribute->install_accessors;
778 $self->remove_attribute( $attribute->name );
783 sub remove_attribute {
786 my $removed_attribute = $self->SUPER::remove_attribute(@_)
789 $self->invalidate_meta_instances;
791 $removed_attribute->remove_accessors;
792 $removed_attribute->detach_from_class;
794 return$removed_attribute;
797 sub find_attribute_by_name {
798 my ( $self, $attr_name ) = @_;
800 foreach my $class ( $self->linearized_isa ) {
801 # fetch the meta-class ...
802 my $meta = Class::MOP::Class->initialize($class);
803 return $meta->get_attribute($attr_name)
804 if $meta->has_attribute($attr_name);
810 sub get_all_attributes {
812 my %attrs = map { %{ Class::MOP::Class->initialize($_)->_attribute_map } }
813 reverse $self->linearized_isa;
814 return values %attrs;
822 my $isa = $self->get_or_add_package_symbol(
823 { sigil => '@', type => 'ARRAY', name => 'ISA' } );
830 # on 5.8 and below, we need to call
831 # a method to get Perl to detect
832 # a cycle in the class hierarchy
833 my $class = $self->name;
837 # we need to check the metaclass
838 # compatibility here so that we can
839 # be sure that the superclass is
840 # not potentially creating an issues
841 # we don't know about
843 $self->_check_metaclass_compatibility();
844 $self->_superclasses_updated();
850 sub _superclasses_updated {
852 $self->update_meta_instance_dependencies();
857 my $super_class = $self->name;
859 return @{ $super_class->mro::get_isarev() };
862 sub direct_subclasses {
864 my $super_class = $self->name;
869 } Class::MOP::Class->initialize($_)->superclasses
874 return @{ mro::get_linear_isa( (shift)->name ) };
877 sub class_precedence_list {
879 my $name = $self->name;
881 unless (Class::MOP::IS_RUNNING_ON_5_10()) {
883 # We need to check for circular inheritance here
884 # if we are are not on 5.10, cause 5.8 detects it
885 # late. This will do nothing if all is well, and
886 # blow up otherwise. Yes, it's an ugly hack, better
887 # suggestions are welcome.
889 ($name || return)->isa('This is a test for circular inheritance')
892 # if our mro is c3, we can
893 # just grab the linear_isa
894 if (mro::get_mro($name) eq 'c3') {
895 return @{ mro::get_linear_isa($name) }
899 # we can't grab the linear_isa for dfs
900 # since it has all the duplicates
905 Class::MOP::Class->initialize($_)->class_precedence_list()
906 } $self->superclasses()
914 my $fetch_and_prepare_method = sub {
915 my ($self, $method_name) = @_;
916 my $wrapped_metaclass = $self->wrapped_method_metaclass;
918 my $method = $self->get_method($method_name);
919 # if we dont have local ...
921 # try to find the next method
922 $method = $self->find_next_method_by_name($method_name);
923 # die if it does not exist
925 || confess "The method '$method_name' was not found in the inheritance hierarchy for " . $self->name;
926 # and now make sure to wrap it
927 # even if it is already wrapped
928 # because we need a new sub ref
929 $method = $wrapped_metaclass->wrap($method,
930 package_name => $self->name,
931 name => $method_name,
935 # now make sure we wrap it properly
936 $method = $wrapped_metaclass->wrap($method,
937 package_name => $self->name,
938 name => $method_name,
939 ) unless $method->isa($wrapped_metaclass);
941 $self->add_method($method_name => $method);
945 sub add_before_method_modifier {
946 my ($self, $method_name, $method_modifier) = @_;
947 (defined $method_name && length $method_name)
948 || confess "You must pass in a method name";
949 my $method = $fetch_and_prepare_method->($self, $method_name);
950 $method->add_before_modifier(
951 subname(':before' => $method_modifier)
955 sub add_after_method_modifier {
956 my ($self, $method_name, $method_modifier) = @_;
957 (defined $method_name && length $method_name)
958 || confess "You must pass in a method name";
959 my $method = $fetch_and_prepare_method->($self, $method_name);
960 $method->add_after_modifier(
961 subname(':after' => $method_modifier)
965 sub add_around_method_modifier {
966 my ($self, $method_name, $method_modifier) = @_;
967 (defined $method_name && length $method_name)
968 || confess "You must pass in a method name";
969 my $method = $fetch_and_prepare_method->($self, $method_name);
970 $method->add_around_modifier(
971 subname(':around' => $method_modifier)
976 # the methods above used to be named like this:
977 # ${pkg}::${method}:(before|after|around)
978 # but this proved problematic when using one modifier
979 # to wrap multiple methods (something which is likely
980 # to happen pretty regularly IMO). So instead of naming
981 # it like this, I have chosen to just name them purely
982 # with their modifier names, like so:
983 # :(before|after|around)
984 # The fact is that in a stack trace, it will be fairly
985 # evident from the context what method they are attached
986 # to, and so don't need the fully qualified name.
989 sub find_method_by_name {
990 my ($self, $method_name) = @_;
991 (defined $method_name && length $method_name)
992 || confess "You must define a method name to find";
993 foreach my $class ($self->linearized_isa) {
994 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
995 return $method if defined $method;
1000 sub get_all_methods {
1004 for my $class ( reverse $self->linearized_isa ) {
1005 my $meta = Class::MOP::Class->initialize($class);
1007 $methods{ $_->name } = $_ for $meta->_get_local_methods;
1010 return values %methods;
1013 sub get_all_method_names {
1016 return grep { !$uniq{$_}++ } map { Class::MOP::Class->initialize($_)->get_method_list } $self->linearized_isa;
1019 sub find_all_methods_by_name {
1020 my ($self, $method_name) = @_;
1021 (defined $method_name && length $method_name)
1022 || confess "You must define a method name to find";
1024 foreach my $class ($self->linearized_isa) {
1025 # fetch the meta-class ...
1026 my $meta = Class::MOP::Class->initialize($class);
1028 name => $method_name,
1030 code => $meta->get_method($method_name)
1031 } if $meta->has_method($method_name);
1036 sub find_next_method_by_name {
1037 my ($self, $method_name) = @_;
1038 (defined $method_name && length $method_name)
1039 || confess "You must define a method name to find";
1040 my @cpl = $self->linearized_isa;
1041 shift @cpl; # discard ourselves
1042 foreach my $class (@cpl) {
1043 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
1044 return $method if defined $method;
1049 sub update_meta_instance_dependencies {
1052 if ( $self->{meta_instance_dependencies} ) {
1053 return $self->add_meta_instance_dependencies;
1057 sub add_meta_instance_dependencies {
1060 $self->remove_meta_instance_dependencies;
1062 my @attrs = $self->get_all_attributes();
1065 my @classes = grep { not $seen{ $_->name }++ }
1066 map { $_->associated_class } @attrs;
1068 foreach my $class (@classes) {
1069 $class->add_dependent_meta_instance($self);
1072 $self->{meta_instance_dependencies} = \@classes;
1075 sub remove_meta_instance_dependencies {
1078 if ( my $classes = delete $self->{meta_instance_dependencies} ) {
1079 foreach my $class (@$classes) {
1080 $class->remove_dependent_meta_instance($self);
1090 sub add_dependent_meta_instance {
1091 my ( $self, $metaclass ) = @_;
1092 push @{ $self->{dependent_meta_instances} }, $metaclass;
1095 sub remove_dependent_meta_instance {
1096 my ( $self, $metaclass ) = @_;
1097 my $name = $metaclass->name;
1098 @$_ = grep { $_->name ne $name } @$_
1099 for $self->{dependent_meta_instances};
1102 sub invalidate_meta_instances {
1104 $_->invalidate_meta_instance()
1105 for $self, @{ $self->{dependent_meta_instances} };
1108 sub invalidate_meta_instance {
1110 undef $self->{_meta_instance};
1113 # check if we can reinitialize
1117 # if any local attr is defined
1118 return if $self->get_attribute_list;
1120 # or any non-declared methods
1121 for my $method ( map { $self->get_method($_) } $self->get_method_list ) {
1122 return if $method->isa("Class::MOP::Method::Generated");
1123 # FIXME do we need to enforce this too? return unless $method->isa( $self->method_metaclass );
1131 sub is_mutable { 1 }
1132 sub is_immutable { 0 }
1134 sub immutable_options { %{ $_[0]{__immutable}{options} || {} } }
1136 sub _immutable_options {
1137 my ( $self, @args ) = @_;
1140 inline_accessors => 1,
1141 inline_constructor => 1,
1142 inline_destructor => 0,
1144 immutable_trait => $self->immutable_trait,
1145 constructor_name => $self->constructor_name,
1146 constructor_class => $self->constructor_class,
1147 destructor_class => $self->destructor_class,
1152 sub make_immutable {
1153 my ( $self, @args ) = @_;
1155 if ( $self->is_mutable ) {
1156 $self->_initialize_immutable( $self->_immutable_options(@args) );
1157 $self->_rebless_as_immutable(@args);
1168 if ( $self->is_immutable ) {
1169 my @args = $self->immutable_options;
1170 $self->_rebless_as_mutable();
1171 $self->_remove_inlined_code(@args);
1172 delete $self->{__immutable};
1180 sub _rebless_as_immutable {
1181 my ( $self, @args ) = @_;
1183 $self->{__immutable}{original_class} = ref $self;
1185 bless $self => $self->_immutable_metaclass(@args);
1188 sub _immutable_metaclass {
1189 my ( $self, %args ) = @_;
1191 if ( my $class = $args{immutable_metaclass} ) {
1195 my $trait = $args{immutable_trait} = $self->immutable_trait
1196 || confess "no immutable trait specified for $self";
1198 my $meta = $self->meta;
1199 my $meta_attr = $meta->find_attribute_by_name("immutable_trait");
1203 if ( $meta_attr and $trait eq $meta_attr->default ) {
1204 # if the trait is the same as the default we try and pick a
1205 # predictable name for the immutable metaclass
1206 $class_name = 'Class::MOP::Class::Immutable::' . ref($self);
1209 $class_name = join '::', 'Class::MOP::Class::Immutable::CustomTrait',
1210 $trait, 'ForMetaClass', ref($self);
1214 if Class::MOP::is_class_loaded($class_name);
1216 # If the metaclass is a subclass of CMOP::Class which has had
1217 # metaclass roles applied (via Moose), then we want to make sure
1218 # that we preserve that anonymous class (see Fey::ORM for an
1219 # example of where this matters).
1220 my $meta_name = $meta->_real_ref_name;
1222 my $immutable_meta = $meta_name->create(
1224 superclasses => [ ref $self ],
1227 Class::MOP::MiniTrait::apply( $immutable_meta, $trait );
1229 $immutable_meta->make_immutable(
1230 inline_constructor => 0,
1231 inline_accessors => 0,
1237 sub _remove_inlined_code {
1240 $self->remove_method( $_->name ) for $self->_inlined_methods;
1242 delete $self->{__immutable}{inlined_methods};
1245 sub _inlined_methods { @{ $_[0]{__immutable}{inlined_methods} || [] } }
1247 sub _add_inlined_method {
1248 my ( $self, $method ) = @_;
1250 push @{ $self->{__immutable}{inlined_methods} ||= [] }, $method;
1253 sub _initialize_immutable {
1254 my ( $self, %args ) = @_;
1256 $self->{__immutable}{options} = \%args;
1257 $self->_install_inlined_code(%args);
1260 sub _install_inlined_code {
1261 my ( $self, %args ) = @_;
1264 $self->_inline_accessors(%args) if $args{inline_accessors};
1265 $self->_inline_constructor(%args) if $args{inline_constructor};
1266 $self->_inline_destructor(%args) if $args{inline_destructor};
1269 sub _rebless_as_mutable {
1272 bless $self, $self->_get_mutable_metaclass_name;
1277 sub _inline_accessors {
1280 foreach my $attr_name ( $self->get_attribute_list ) {
1281 $self->get_attribute($attr_name)->install_accessors(1);
1285 sub _inline_constructor {
1286 my ( $self, %args ) = @_;
1288 my $name = $args{constructor_name};
1289 # A class may not even have a constructor, and that's okay.
1290 return unless defined $name;
1292 if ( $self->has_method($name) && !$args{replace_constructor} ) {
1293 my $class = $self->name;
1294 warn "Not inlining a constructor for $class since it defines"
1295 . " its own constructor.\n"
1296 . "If you are certain you don't need to inline your"
1297 . " constructor, specify inline_constructor => 0 in your"
1298 . " call to $class->meta->make_immutable\n";
1302 my $constructor_class = $args{constructor_class};
1304 Class::MOP::load_class($constructor_class);
1306 my $constructor = $constructor_class->new(
1310 package_name => $self->name,
1314 if ( $args{replace_constructor} or $constructor->can_be_inlined ) {
1315 $self->add_method( $name => $constructor );
1316 $self->_add_inlined_method($constructor);
1320 sub _inline_destructor {
1321 my ( $self, %args ) = @_;
1323 ( exists $args{destructor_class} && defined $args{destructor_class} )
1324 || confess "The 'inline_destructor' option is present, but "
1325 . "no destructor class was specified";
1327 if ( $self->has_method('DESTROY') && ! $args{replace_destructor} ) {
1328 my $class = $self->name;
1329 warn "Not inlining a destructor for $class since it defines"
1330 . " its own destructor.\n";
1334 my $destructor_class = $args{destructor_class};
1336 Class::MOP::load_class($destructor_class);
1338 return unless $destructor_class->is_needed($self);
1340 my $destructor = $destructor_class->new(
1343 package_name => $self->name,
1347 if ( $args{replace_destructor} or $destructor->can_be_inlined ) {
1348 $self->add_method( 'DESTROY' => $destructor );
1349 $self->_add_inlined_method($destructor);
1361 Class::MOP::Class - Class Meta Object
1365 # assuming that class Foo
1366 # has been defined, you can
1368 # use this for introspection ...
1370 # add a method to Foo ...
1371 Foo->meta->add_method( 'bar' => sub {...} )
1373 # get a list of all the classes searched
1374 # the method dispatcher in the correct order
1375 Foo->meta->class_precedence_list()
1377 # remove a method from Foo
1378 Foo->meta->remove_method('bar');
1380 # or use this to actually create classes ...
1382 Class::MOP::Class->create(
1385 superclasses => ['Foo'],
1387 Class::MOP::Attribute->new('$bar'),
1388 Class::MOP::Attribute->new('$baz'),
1391 calculate_bar => sub {...},
1392 construct_baz => sub {...}
1399 The Class Protocol is the largest and most complex part of the
1400 Class::MOP meta-object protocol. It controls the introspection and
1401 manipulation of Perl 5 classes, and it can create them as well. The
1402 best way to understand what this module can do is to read the
1403 documentation for each of its methods.
1407 C<Class::MOP::Class> is a subclass of L<Class::MOP::Module>.
1411 =head2 Class construction
1413 These methods all create new C<Class::MOP::Class> objects. These
1414 objects can represent existing classes or they can be used to create
1415 new classes from scratch.
1417 The metaclass object for a given class is a singleton. If you attempt
1418 to create a metaclass for the same class twice, you will just get the
1423 =item B<< Class::MOP::Class->create($package_name, %options) >>
1425 This method creates a new C<Class::MOP::Class> object with the given
1426 package name. It accepts a number of options:
1432 An optional version number for the newly created package.
1436 An optional authority for the newly created package.
1438 =item * superclasses
1440 An optional array reference of superclass names.
1444 An optional hash reference of methods for the class. The keys of the
1445 hash reference are method names and values are subroutine references.
1449 An optional array reference of L<Class::MOP::Attribute> objects.
1453 If true, a C<meta> method will not be installed into the class.
1457 =item B<< Class::MOP::Class->create_anon_class(%options) >>
1459 This method works just like C<< Class::MOP::Class->create >> but it
1460 creates an "anonymous" class. In fact, the class does have a name, but
1461 that name is a unique name generated internally by this module.
1463 It accepts the same C<superclasses>, C<methods>, and C<attributes>
1464 parameters that C<create> accepts.
1466 Anonymous classes are destroyed once the metaclass they are attached
1467 to goes out of scope, and will be removed from Perl's internal symbol
1470 All instances of an anonymous class keep a special reference to the
1471 metaclass object, which prevents the metaclass from going out of scope
1472 while any instances exist.
1474 This only works if the instance is based on a hash reference, however.
1476 =item B<< Class::MOP::Class->initialize($package_name, %options) >>
1478 This method will initialize a C<Class::MOP::Class> object for the
1479 named package. Unlike C<create>, this method I<will not> create a new
1482 The purpose of this method is to retrieve a C<Class::MOP::Class>
1483 object for introspecting an existing class.
1485 If an existing C<Class::MOP::Class> object exists for the named
1486 package, it will be returned, and any options provided will be
1489 If the object does not yet exist, it will be created.
1491 The valid options that can be passed to this method are
1492 C<attribute_metaclass>, C<method_metaclass>,
1493 C<wrapped_method_metaclass>, and C<instance_metaclass>. These are all
1494 optional, and default to the appropriate class in the C<Class::MOP>
1499 =head2 Object instance construction and cloning
1501 These methods are all related to creating and/or cloning object
1506 =item B<< $metaclass->clone_object($instance, %params) >>
1508 This method clones an existing object instance. Any parameters you
1509 provide are will override existing attribute values in the object.
1511 This is a convenience method for cloning an object instance, then
1512 blessing it into the appropriate package.
1514 You could implement a clone method in your class, using this method:
1517 my ($self, %params) = @_;
1518 $self->meta->clone_object($self, %params);
1521 =item B<< $metaclass->rebless_instance($instance, %params) >>
1523 This method changes the class of C<$instance> to the metaclass's class.
1525 You can only rebless an instance into a subclass of its current
1526 class. If you pass any additional parameters, these will be treated
1527 like constructor parameters and used to initialize the object's
1528 attributes. Any existing attributes that are already set will be
1531 Before reblessing the instance, this method will call
1532 C<rebless_instance_away> on the instance's current metaclass. This method
1533 will be passed the instance, the new metaclass, and any parameters
1534 specified to C<rebless_instance>. By default, C<rebless_instance_away>
1535 does nothing; it is merely a hook.
1537 =item B<< $metaclass->rebless_instance_back($instance) >>
1539 Does the same thing as C<rebless_instance>, except that you can only
1540 rebless an instance into one of its superclasses. Any attributes that
1541 do not exist in the superclass will be deinitialized.
1543 This is a much more dangerous operation than C<rebless_instance>,
1544 especially when multiple inheritance is involved, so use this carefully!
1546 =item B<< $metaclass->new_object(%params) >>
1548 This method is used to create a new object of the metaclass's
1549 class. Any parameters you provide are used to initialize the
1550 instance's attributes. A special C<__INSTANCE__> key can be passed to
1551 provide an already generated instance, rather than having Class::MOP
1552 generate it for you. This is mostly useful for using Class::MOP with
1553 foreign classes which generate instances using their own constructors.
1555 =item B<< $metaclass->instance_metaclass >>
1557 Returns the class name of the instance metaclass. See
1558 L<Class::MOP::Instance> for more information on the instance
1561 =item B<< $metaclass->get_meta_instance >>
1563 Returns an instance of the C<instance_metaclass> to be used in the
1564 construction of a new instance of the class.
1566 =item B<< $metaclass->inline_create_instance($class_var) >>
1568 =item B<< $metaclass->inline_rebless_instance($instance_var, $class_var) >>
1570 These methods takes variable names, and use them to create an inline snippet
1571 of code that will create a new instance of the class.
1575 =head2 Informational predicates
1577 These are a few predicate methods for asking information about the
1582 =item B<< $metaclass->is_anon_class >>
1584 This returns true if the class was created by calling C<<
1585 Class::MOP::Class->create_anon_class >>.
1587 =item B<< $metaclass->is_mutable >>
1589 This returns true if the class is still mutable.
1591 =item B<< $metaclass->is_immutable >>
1593 This returns true if the class has been made immutable.
1595 =item B<< $metaclass->is_pristine >>
1597 A class is I<not> pristine if it has non-inherited attributes or if it
1598 has any generated methods.
1602 =head2 Inheritance Relationships
1606 =item B<< $metaclass->superclasses(@superclasses) >>
1608 This is a read-write accessor which represents the superclass
1609 relationships of the metaclass's class.
1611 This is basically sugar around getting and setting C<@ISA>.
1613 =item B<< $metaclass->class_precedence_list >>
1615 This returns a list of all of the class's ancestor classes. The
1616 classes are returned in method dispatch order.
1618 =item B<< $metaclass->linearized_isa >>
1620 This returns a list based on C<class_precedence_list> but with all
1623 =item B<< $metaclass->subclasses >>
1625 This returns a list of all subclasses for this class, even indirect
1628 =item B<< $metaclass->direct_subclasses >>
1630 This returns a list of immediate subclasses for this class, which does not
1631 include indirect subclasses.
1635 =head2 Method introspection and creation
1637 These methods allow you to introspect a class's methods, as well as
1638 add, remove, or change methods.
1640 Determining what is truly a method in a Perl 5 class requires some
1641 heuristics (aka guessing).
1643 Methods defined outside the package with a fully qualified name (C<sub
1644 Package::name { ... }>) will be included. Similarly, methods named
1645 with a fully qualified name using L<Sub::Name> are also included.
1647 However, we attempt to ignore imported functions.
1649 Ultimately, we are using heuristics to determine what truly is a
1650 method in a class, and these heuristics may get the wrong answer in
1651 some edge cases. However, for most "normal" cases the heuristics work
1656 =item B<< $metaclass->get_method($method_name) >>
1658 This will return a L<Class::MOP::Method> for the specified
1659 C<$method_name>. If the class does not have the specified method, it
1662 =item B<< $metaclass->has_method($method_name) >>
1664 Returns a boolean indicating whether or not the class defines the
1665 named method. It does not include methods inherited from parent
1668 =item B<< $metaclass->get_method_list >>
1670 This will return a list of method I<names> for all methods defined in
1673 =item B<< $metaclass->add_method($method_name, $method) >>
1675 This method takes a method name and a subroutine reference, and adds
1676 the method to the class.
1678 The subroutine reference can be a L<Class::MOP::Method>, and you are
1679 strongly encouraged to pass a meta method object instead of a code
1680 reference. If you do so, that object gets stored as part of the
1681 class's method map directly. If not, the meta information will have to
1682 be recreated later, and may be incorrect.
1684 If you provide a method object, this method will clone that object if
1685 the object's package name does not match the class name. This lets us
1686 track the original source of any methods added from other classes
1687 (notably Moose roles).
1689 =item B<< $metaclass->remove_method($method_name) >>
1691 Remove the named method from the class. This method returns the
1692 L<Class::MOP::Method> object for the method.
1694 =item B<< $metaclass->method_metaclass >>
1696 Returns the class name of the method metaclass, see
1697 L<Class::MOP::Method> for more information on the method metaclass.
1699 =item B<< $metaclass->wrapped_method_metaclass >>
1701 Returns the class name of the wrapped method metaclass, see
1702 L<Class::MOP::Method::Wrapped> for more information on the wrapped
1705 =item B<< $metaclass->get_all_methods >>
1707 This will traverse the inheritance hierarchy and return a list of all
1708 the L<Class::MOP::Method> objects for this class and its parents.
1710 =item B<< $metaclass->find_method_by_name($method_name) >>
1712 This will return a L<Class::MOP::Method> for the specified
1713 C<$method_name>. If the class does not have the specified method, it
1716 Unlike C<get_method>, this method I<will> look for the named method in
1719 =item B<< $metaclass->get_all_method_names >>
1721 This will return a list of method I<names> for all of this class's
1722 methods, including inherited methods.
1724 =item B<< $metaclass->find_all_methods_by_name($method_name) >>
1726 This method looks for the named method in the class and all of its
1727 parents. It returns every matching method it finds in the inheritance
1728 tree, so it returns a list of methods.
1730 Each method is returned as a hash reference with three keys. The keys
1731 are C<name>, C<class>, and C<code>. The C<code> key has a
1732 L<Class::MOP::Method> object as its value.
1734 The list of methods is distinct.
1736 =item B<< $metaclass->find_next_method_by_name($method_name) >>
1738 This method returns the first method in any superclass matching the
1739 given name. It is effectively the method that C<SUPER::$method_name>
1744 =head2 Attribute introspection and creation
1746 Because Perl 5 does not have a core concept of attributes in classes,
1747 we can only return information about attributes which have been added
1748 via this class's methods. We cannot discover information about
1749 attributes which are defined in terms of "regular" Perl 5 methods.
1753 =item B<< $metaclass->get_attribute($attribute_name) >>
1755 This will return a L<Class::MOP::Attribute> for the specified
1756 C<$attribute_name>. If the class does not have the specified
1757 attribute, it returns C<undef>.
1759 NOTE that get_attribute does not search superclasses, for that you
1760 need to use C<find_attribute_by_name>.
1762 =item B<< $metaclass->has_attribute($attribute_name) >>
1764 Returns a boolean indicating whether or not the class defines the
1765 named attribute. It does not include attributes inherited from parent
1768 =item B<< $metaclass->get_attribute_list >>
1770 This will return a list of attributes I<names> for all attributes
1771 defined in this class. Note that this operates on the current class
1772 only, it does not traverse the inheritance hierarchy.
1774 =item B<< $metaclass->get_all_attributes >>
1776 This will traverse the inheritance hierarchy and return a list of all
1777 the L<Class::MOP::Attribute> objects for this class and its parents.
1779 =item B<< $metaclass->find_attribute_by_name($attribute_name) >>
1781 This will return a L<Class::MOP::Attribute> for the specified
1782 C<$attribute_name>. If the class does not have the specified
1783 attribute, it returns C<undef>.
1785 Unlike C<get_attribute>, this attribute I<will> look for the named
1786 attribute in superclasses.
1788 =item B<< $metaclass->add_attribute(...) >>
1790 This method accepts either an existing L<Class::MOP::Attribute>
1791 object or parameters suitable for passing to that class's C<new>
1794 The attribute provided will be added to the class.
1796 Any accessor methods defined by the attribute will be added to the
1797 class when the attribute is added.
1799 If an attribute of the same name already exists, the old attribute
1800 will be removed first.
1802 =item B<< $metaclass->remove_attribute($attribute_name) >>
1804 This will remove the named attribute from the class, and
1805 L<Class::MOP::Attribute> object.
1807 Removing an attribute also removes any accessor methods defined by the
1810 However, note that removing an attribute will only affect I<future>
1811 object instances created for this class, not existing instances.
1813 =item B<< $metaclass->attribute_metaclass >>
1815 Returns the class name of the attribute metaclass for this class. By
1816 default, this is L<Class::MOP::Attribute>.
1820 =head2 Class Immutability
1822 Making a class immutable "freezes" the class definition. You can no
1823 longer call methods which alter the class, such as adding or removing
1824 methods or attributes.
1826 Making a class immutable lets us optimize the class by inlining some
1827 methods, and also allows us to optimize some methods on the metaclass
1830 After immutabilization, the metaclass object will cache most informational
1831 methods that returns information about methods or attributes. Methods which
1832 would alter the class, such as C<add_attribute> and C<add_method>, will
1833 throw an error on an immutable metaclass object.
1835 The immutabilization system in L<Moose> takes much greater advantage
1836 of the inlining features than Class::MOP itself does.
1840 =item B<< $metaclass->make_immutable(%options) >>
1842 This method will create an immutable transformer and use it to make
1843 the class and its metaclass object immutable.
1845 This method accepts the following options:
1849 =item * inline_accessors
1851 =item * inline_constructor
1853 =item * inline_destructor
1855 These are all booleans indicating whether the specified method(s)
1858 By default, accessors and the constructor are inlined, but not the
1861 =item * immutable_trait
1863 The name of a class which will be used as a parent class for the
1864 metaclass object being made immutable. This "trait" implements the
1865 post-immutability functionality of the metaclass (but not the
1866 transformation itself).
1868 This defaults to L<Class::MOP::Class::Immutable::Trait>.
1870 =item * constructor_name
1872 This is the constructor method name. This defaults to "new".
1874 =item * constructor_class
1876 The name of the method metaclass for constructors. It will be used to
1877 generate the inlined constructor. This defaults to
1878 "Class::MOP::Method::Constructor".
1880 =item * replace_constructor
1882 This is a boolean indicating whether an existing constructor should be
1883 replaced when inlining a constructor. This defaults to false.
1885 =item * destructor_class
1887 The name of the method metaclass for destructors. It will be used to
1888 generate the inlined destructor. This defaults to
1889 "Class::MOP::Method::Denstructor".
1891 =item * replace_destructor
1893 This is a boolean indicating whether an existing destructor should be
1894 replaced when inlining a destructor. This defaults to false.
1898 =item B<< $metaclass->immutable_options >>
1900 Returns a hash of the options used when making the class immutable, including
1901 both defaults and anything supplied by the user in the call to C<<
1902 $metaclass->make_immutable >>. This is useful if you need to temporarily make
1903 a class mutable and then restore immutability as it was before.
1905 =item B<< $metaclass->make_mutable >>
1907 Calling this method reverse the immutabilization transformation.
1911 =head2 Method Modifiers
1913 Method modifiers are hooks which allow a method to be wrapped with
1914 I<before>, I<after> and I<around> method modifiers. Every time a
1915 method is called, its modifiers are also called.
1917 A class can modify its own methods, as well as methods defined in
1920 =head3 How method modifiers work?
1922 Method modifiers work by wrapping the original method and then
1923 replacing it in the class's symbol table. The wrappers will handle
1924 calling all the modifiers in the appropriate order and preserving the
1925 calling context for the original method.
1927 The return values of C<before> and C<after> modifiers are
1928 ignored. This is because their purpose is B<not> to filter the input
1929 and output of the primary method (this is done with an I<around>
1932 This may seem like an odd restriction to some, but doing this allows
1933 for simple code to be added at the beginning or end of a method call
1934 without altering the function of the wrapped method or placing any
1935 extra responsibility on the code of the modifier.
1937 Of course if you have more complex needs, you can use the C<around>
1938 modifier which allows you to change both the parameters passed to the
1939 wrapped method, as well as its return value.
1941 Before and around modifiers are called in last-defined-first-called
1942 order, while after modifiers are called in first-defined-first-called
1943 order. So the call tree might looks something like this:
1955 =head3 What is the performance impact?
1957 Of course there is a performance cost associated with method
1958 modifiers, but we have made every effort to make that cost directly
1959 proportional to the number of modifier features you use.
1961 The wrapping method does its best to B<only> do as much work as it
1962 absolutely needs to. In order to do this we have moved some of the
1963 performance costs to set-up time, where they are easier to amortize.
1965 All this said, our benchmarks have indicated the following:
1967 simple wrapper with no modifiers 100% slower
1968 simple wrapper with simple before modifier 400% slower
1969 simple wrapper with simple after modifier 450% slower
1970 simple wrapper with simple around modifier 500-550% slower
1971 simple wrapper with all 3 modifiers 1100% slower
1973 These numbers may seem daunting, but you must remember, every feature
1974 comes with some cost. To put things in perspective, just doing a
1975 simple C<AUTOLOAD> which does nothing but extract the name of the
1976 method called and return it costs about 400% over a normal method
1981 =item B<< $metaclass->add_before_method_modifier($method_name, $code) >>
1983 This wraps the specified method with the supplied subroutine
1984 reference. The modifier will be called as a method itself, and will
1985 receive the same arguments as are passed to the method.
1987 When the modifier exits, the wrapped method will be called.
1989 The return value of the modifier will be ignored.
1991 =item B<< $metaclass->add_after_method_modifier($method_name, $code) >>
1993 This wraps the specified method with the supplied subroutine
1994 reference. The modifier will be called as a method itself, and will
1995 receive the same arguments as are passed to the method.
1997 When the wrapped methods exits, the modifier will be called.
1999 The return value of the modifier will be ignored.
2001 =item B<< $metaclass->add_around_method_modifier($method_name, $code) >>
2003 This wraps the specified method with the supplied subroutine
2006 The first argument passed to the modifier will be a subroutine
2007 reference to the wrapped method. The second argument is the object,
2008 and after that come any arguments passed when the method is called.
2010 The around modifier can choose to call the original method, as well as
2011 what arguments to pass if it does so.
2013 The return value of the modifier is what will be seen by the caller.
2017 =head2 Introspection
2021 =item B<< Class::MOP::Class->meta >>
2023 This will return a L<Class::MOP::Class> instance for this class.
2025 It should also be noted that L<Class::MOP> will actually bootstrap
2026 this module by installing a number of attribute meta-objects into its
2033 Stevan Little E<lt>stevan@iinteractive.comE<gt>
2035 =head1 COPYRIGHT AND LICENSE
2037 Copyright 2006-2010 by Infinity Interactive, Inc.
2039 L<http://www.iinteractive.com>
2041 This library is free software; you can redistribute it and/or modify
2042 it under the same terms as Perl itself.