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 {
130 my $existing_method = $self->find_method_by_name('meta');
131 return if $existing_method
132 && $existing_method->isa('Class::MOP::Method::Meta');
134 'meta' => Class::MOP::Method::Meta->wrap(
136 package_name => $self->name,
137 associated_metaclass => $self,
145 return Class::MOP::Class->initialize($class)->new_object(@_)
146 if $class ne __PACKAGE__;
148 my $options = @_ == 1 ? $_[0] : {@_};
151 # inherited from Class::MOP::Package
152 'package' => $options->{package},
155 # since the following attributes will
156 # actually be loaded from the symbol
157 # table, and actually bypass the instance
158 # entirely, we can just leave these things
159 # listed here for reference, because they
160 # should not actually have a value associated
162 'namespace' => \undef,
165 # inherited from Class::MOP::Module
167 'authority' => \undef,
169 # defined in Class::MOP::Class
170 'superclasses' => \undef,
173 'attribute_metaclass' =>
174 ( $options->{'attribute_metaclass'} || 'Class::MOP::Attribute' ),
175 'method_metaclass' =>
176 ( $options->{'method_metaclass'} || 'Class::MOP::Method' ),
177 'wrapped_method_metaclass' => (
178 $options->{'wrapped_method_metaclass'}
179 || 'Class::MOP::Method::Wrapped'
181 'instance_metaclass' =>
182 ( $options->{'instance_metaclass'} || 'Class::MOP::Instance' ),
183 'immutable_trait' => (
184 $options->{'immutable_trait'}
185 || 'Class::MOP::Class::Immutable::Trait'
187 'constructor_name' => ( $options->{constructor_name} || 'new' ),
188 'constructor_class' => (
189 $options->{constructor_class} || 'Class::MOP::Method::Constructor'
191 'destructor_class' => $options->{destructor_class},
195 sub reset_package_cache_flag { (shift)->{'_package_cache_flag'} = undef }
196 sub update_package_cache_flag {
199 # we can manually update the cache number
200 # since we are actually adding the method
201 # to our cache as well. This avoids us
202 # having to regenerate the method_map.
204 $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);
207 ## Metaclass compatibility
209 my %base_metaclass = (
210 attribute_metaclass => 'Class::MOP::Attribute',
211 method_metaclass => 'Class::MOP::Method',
212 wrapped_method_metaclass => 'Class::MOP::Method::Wrapped',
213 instance_metaclass => 'Class::MOP::Instance',
214 constructor_class => 'Class::MOP::Method::Constructor',
215 destructor_class => 'Class::MOP::Method::Destructor',
218 sub _base_metaclasses { %base_metaclass }
221 sub _check_metaclass_compatibility {
224 my @superclasses = $self->superclasses
227 $self->_fix_metaclass_incompatibility(@superclasses);
229 my %base_metaclass = $self->_base_metaclasses;
231 # this is always okay ...
233 if ref($self) eq 'Class::MOP::Class'
235 my $meta = $self->$_;
236 !defined($meta) || $meta eq $base_metaclass{$_};
238 keys %base_metaclass;
240 for my $superclass (@superclasses) {
241 $self->_check_class_metaclass_compatibility($superclass);
244 for my $metaclass_type ( keys %base_metaclass ) {
245 next unless defined $self->$metaclass_type;
246 for my $superclass (@superclasses) {
247 $self->_check_single_metaclass_compatibility( $metaclass_type,
253 sub _check_class_metaclass_compatibility {
255 my ( $superclass_name ) = @_;
257 if (!$self->_class_metaclass_is_compatible($superclass_name)) {
258 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
260 my $super_meta_type = $super_meta->_real_ref_name;
262 confess "The metaclass of " . $self->name . " ("
263 . (ref($self)) . ")" . " is not compatible with "
264 . "the metaclass of its superclass, "
265 . $superclass_name . " (" . ($super_meta_type) . ")";
269 sub _class_metaclass_is_compatible {
271 my ( $superclass_name ) = @_;
273 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
276 my $super_meta_name = $super_meta->_real_ref_name;
278 return $self->_is_compatible_with($super_meta_name);
281 sub _check_single_metaclass_compatibility {
283 my ( $metaclass_type, $superclass_name ) = @_;
285 if (!$self->_single_metaclass_is_compatible($metaclass_type, $superclass_name)) {
286 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
287 my $metaclass_type_name = $metaclass_type;
288 $metaclass_type_name =~ s/_(?:meta)?class$//;
289 $metaclass_type_name =~ s/_/ /g;
290 confess "The $metaclass_type_name metaclass for "
291 . $self->name . " (" . ($self->$metaclass_type)
292 . ")" . " is not compatible with the "
293 . "$metaclass_type_name metaclass of its "
294 . "superclass, $superclass_name ("
295 . ($super_meta->$metaclass_type) . ")";
299 sub _single_metaclass_is_compatible {
301 my ( $metaclass_type, $superclass_name ) = @_;
303 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
306 # for instance, Moose::Meta::Class has a error_class attribute, but
307 # Class::MOP::Class doesn't - this shouldn't be an error
308 return 1 unless $super_meta->can($metaclass_type);
309 # for instance, Moose::Meta::Class has a destructor_class, but
310 # Class::MOP::Class doesn't - this shouldn't be an error
311 return 1 unless defined $super_meta->$metaclass_type;
312 # if metaclass is defined in superclass but not here, it's not compatible
313 # this is a really odd case
314 return 0 unless defined $self->$metaclass_type;
316 return $self->$metaclass_type->_is_compatible_with($super_meta->$metaclass_type);
319 sub _fix_metaclass_incompatibility {
321 my @supers = map { Class::MOP::Class->initialize($_) } @_;
324 for my $super (@supers) {
326 if $self->_can_fix_metaclass_incompatibility($super);
328 return unless $necessary;
330 for my $super (@supers) {
331 if (!$self->_class_metaclass_is_compatible($super->name)) {
332 $self->_fix_class_metaclass_incompatibility($super);
336 my %base_metaclass = $self->_base_metaclasses;
337 for my $metaclass_type (keys %base_metaclass) {
338 for my $super (@supers) {
339 if (!$self->_single_metaclass_is_compatible($metaclass_type, $super->name)) {
340 $self->_fix_single_metaclass_incompatibility(
341 $metaclass_type, $super
348 sub _can_fix_metaclass_incompatibility {
350 my ($super_meta) = @_;
352 return 1 if $self->_class_metaclass_can_be_made_compatible($super_meta);
354 my %base_metaclass = $self->_base_metaclasses;
355 for my $metaclass_type (keys %base_metaclass) {
356 return 1 if $self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type);
362 sub _class_metaclass_can_be_made_compatible {
364 my ($super_meta) = @_;
366 return $self->_can_be_made_compatible_with($super_meta->_real_ref_name);
369 sub _single_metaclass_can_be_made_compatible {
371 my ($super_meta, $metaclass_type) = @_;
373 my $specific_meta = $self->$metaclass_type;
375 return unless $super_meta->can($metaclass_type);
376 my $super_specific_meta = $super_meta->$metaclass_type;
378 # for instance, Moose::Meta::Class has a destructor_class, but
379 # Class::MOP::Class doesn't - this shouldn't be an error
380 return unless defined $super_specific_meta;
382 # if metaclass is defined in superclass but not here, it's fixable
383 # this is a really odd case
384 return 1 unless defined $specific_meta;
386 return 1 if $specific_meta->_can_be_made_compatible_with($super_specific_meta);
389 sub _fix_class_metaclass_incompatibility {
391 my ( $super_meta ) = @_;
393 if ($self->_class_metaclass_can_be_made_compatible($super_meta)) {
395 || confess "Can't fix metaclass incompatibility for "
397 . " because it is not pristine.";
399 my $super_meta_name = $super_meta->_real_ref_name;
401 $self->_make_compatible_with($super_meta_name);
405 sub _fix_single_metaclass_incompatibility {
407 my ( $metaclass_type, $super_meta ) = @_;
409 if ($self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type)) {
411 || confess "Can't fix metaclass incompatibility for "
413 . " because it is not pristine.";
415 my $new_metaclass = $self->$metaclass_type
416 ? $self->$metaclass_type->_get_compatible_metaclass($super_meta->$metaclass_type)
417 : $super_meta->$metaclass_type;
418 $self->{$metaclass_type} = $new_metaclass;
422 sub _restore_metaobjects_from {
426 $self->_restore_metamethods_from($old_meta);
427 $self->_restore_metaattributes_from($old_meta);
430 sub _remove_generated_metaobjects {
433 for my $attr (map { $self->get_attribute($_) } $self->get_attribute_list) {
434 $attr->remove_accessors;
442 # this should be sufficient, if you have a
443 # use case where it is not, write a test and
445 my $ANON_CLASS_SERIAL = 0;
448 # we need a sufficiently annoying prefix
449 # this should suffice for now, this is
450 # used in a couple of places below, so
451 # need to put it up here for now.
452 my $ANON_CLASS_PREFIX = 'Class::MOP::Class::__ANON__::SERIAL::';
456 no warnings 'uninitialized';
457 $self->name =~ /^$ANON_CLASS_PREFIX/o;
460 sub create_anon_class {
461 my ($class, %options) = @_;
462 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
463 return $class->create($package_name, %options);
467 # this will only get called for
468 # anon-classes, all other calls
469 # are assumed to occur during
470 # global destruction and so don't
471 # really need to be handled explicitly
475 return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated
477 no warnings 'uninitialized';
478 my $name = $self->name;
479 return unless $name =~ /^$ANON_CLASS_PREFIX/o;
481 # Moose does a weird thing where it replaces the metaclass for
482 # class when fixing metaclass incompatibility. In that case,
483 # we don't want to clean out the namespace now. We can detect
484 # that because Moose will explicitly update the singleton
485 # cache in Class::MOP.
486 my $current_meta = Class::MOP::get_metaclass_by_name($name);
487 return if $current_meta ne $self;
489 my ($serial_id) = ($name =~ /^$ANON_CLASS_PREFIX(\d+)/o);
491 @{$name . '::ISA'} = ();
492 %{$name . '::'} = ();
493 delete ${$ANON_CLASS_PREFIX}{$serial_id . '::'};
495 Class::MOP::remove_metaclass_by_name($name);
500 # creating classes with MOP ...
503 my ( $class, @args ) = @_;
505 unshift @args, 'package' if @args % 2 == 1;
507 my (%options) = @args;
508 my $package_name = $options{package};
510 (ref $options{superclasses} eq 'ARRAY')
511 || confess "You must pass an ARRAY ref of superclasses"
512 if exists $options{superclasses};
514 (ref $options{attributes} eq 'ARRAY')
515 || confess "You must pass an ARRAY ref of attributes"
516 if exists $options{attributes};
518 (ref $options{methods} eq 'HASH')
519 || confess "You must pass a HASH ref of methods"
520 if exists $options{methods};
522 my (%initialize_options) = @args;
523 delete @initialize_options{qw(
532 my $meta = $class->initialize( $package_name => %initialize_options );
534 $meta->_instantiate_module( $options{version}, $options{authority} );
536 $meta->_add_meta_method unless $options{no_meta};
538 $meta->superclasses(@{$options{superclasses}})
539 if exists $options{superclasses};
541 # process attributes first, so that they can
542 # install accessors, but locally defined methods
543 # can then overwrite them. It is maybe a little odd, but
544 # I think this should be the order of things.
545 if (exists $options{attributes}) {
546 foreach my $attr (@{$options{attributes}}) {
547 $meta->add_attribute($attr);
550 if (exists $options{methods}) {
551 foreach my $method_name (keys %{$options{methods}}) {
552 $meta->add_method($method_name, $options{methods}->{$method_name});
561 # all these attribute readers will be bootstrapped
562 # away in the Class::MOP bootstrap section
564 sub instance_metaclass { $_[0]->{'instance_metaclass'} }
565 sub immutable_trait { $_[0]->{'immutable_trait'} }
566 sub constructor_class { $_[0]->{'constructor_class'} }
567 sub constructor_name { $_[0]->{'constructor_name'} }
568 sub destructor_class { $_[0]->{'destructor_class'} }
570 # Instance Construction & Cloning
576 # we need to protect the integrity of the
577 # Class::MOP::Class singletons here, so we
578 # delegate this to &construct_class_instance
579 # which will deal with the singletons
580 return $class->_construct_class_instance(@_)
581 if $class->name->isa('Class::MOP::Class');
582 return $class->_construct_instance(@_);
585 sub _construct_instance {
587 my $params = @_ == 1 ? $_[0] : {@_};
588 my $meta_instance = $class->get_meta_instance();
590 # the code below is almost certainly incorrect
591 # but this is foreign inheritance, so we might
592 # have to kludge it in the end.
594 if (my $instance_class = blessed($params->{__INSTANCE__})) {
595 ($instance_class eq $class->name)
596 || confess "Objects passed as the __INSTANCE__ parameter must "
597 . "already be blessed into the correct class, but "
598 . "$params->{__INSTANCE__} is not a " . $class->name;
599 $instance = $params->{__INSTANCE__};
601 elsif (exists $params->{__INSTANCE__}) {
602 confess "The __INSTANCE__ parameter must be a blessed reference, not "
603 . $params->{__INSTANCE__};
606 $instance = $meta_instance->create_instance();
608 foreach my $attr ($class->get_all_attributes()) {
609 $attr->initialize_instance_slot($meta_instance, $instance, $params);
612 # this will only work for a HASH instance type
613 if ($class->is_anon_class) {
614 (reftype($instance) eq 'HASH')
615 || confess "Currently only HASH based instances are supported with instance of anon-classes";
617 # At some point we should make this official
618 # as a reserved slot name, but right now I am
619 # going to keep it here.
620 # my $RESERVED_MOP_SLOT = '__MOP__';
621 $instance->{'__MOP__'} = $class;
627 sub get_meta_instance {
629 $self->{'_meta_instance'} ||= $self->_create_meta_instance();
632 sub _create_meta_instance {
635 my $instance = $self->instance_metaclass->new(
636 associated_metaclass => $self,
637 attributes => [ $self->get_all_attributes() ],
640 $self->add_meta_instance_dependencies()
641 if $instance->is_dependent_on_superclasses();
646 sub inline_create_instance {
649 return $self->get_meta_instance->inline_create_instance(@_);
652 sub inline_rebless_instance {
655 return $self->get_meta_instance->inline_rebless_instance_structure(@_);
660 my $instance = shift;
661 (blessed($instance) && $instance->isa($class->name))
662 || confess "You must pass an instance of the metaclass (" . (ref $class ? $class->name : $class) . "), not ($instance)";
665 # we need to protect the integrity of the
666 # Class::MOP::Class singletons here, they
667 # should not be cloned.
668 return $instance if $instance->isa('Class::MOP::Class');
669 $class->_clone_instance($instance, @_);
672 sub _clone_instance {
673 my ($class, $instance, %params) = @_;
675 || confess "You can only clone instances, ($instance) is not a blessed instance";
676 my $meta_instance = $class->get_meta_instance();
677 my $clone = $meta_instance->clone_instance($instance);
678 foreach my $attr ($class->get_all_attributes()) {
679 if ( defined( my $init_arg = $attr->init_arg ) ) {
680 if (exists $params{$init_arg}) {
681 $attr->set_value($clone, $params{$init_arg});
688 sub _force_rebless_instance {
689 my ($self, $instance, %params) = @_;
690 my $old_metaclass = Class::MOP::class_of($instance);
692 $old_metaclass->rebless_instance_away($instance, $self, %params)
695 my $meta_instance = $self->get_meta_instance;
698 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
699 $meta_instance->rebless_instance_structure($_[1], $self);
701 $self->_fixup_attributes_after_rebless($instance, $old_metaclass, %params);
704 sub rebless_instance {
705 my ($self, $instance, %params) = @_;
706 my $old_metaclass = Class::MOP::class_of($instance);
708 my $old_class = $old_metaclass ? $old_metaclass->name : blessed($instance);
709 $self->name->isa($old_class)
710 || confess "You may rebless only into a subclass of ($old_class), of which (". $self->name .") isn't.";
712 $self->_force_rebless_instance($_[1], %params);
717 sub rebless_instance_back {
718 my ( $self, $instance ) = @_;
719 my $old_metaclass = Class::MOP::class_of($instance);
722 = $old_metaclass ? $old_metaclass->name : blessed($instance);
723 $old_class->isa( $self->name )
725 "You may rebless only into a superclass of ($old_class), of which ("
729 $self->_force_rebless_instance($_[1]);
734 sub rebless_instance_away {
735 # this intentionally does nothing, it is just a hook
738 sub _fixup_attributes_after_rebless {
740 my ($instance, $rebless_from, %params) = @_;
741 my $meta_instance = $self->get_meta_instance;
743 for my $attr ( $rebless_from->get_all_attributes ) {
744 next if $self->find_attribute_by_name( $attr->name );
745 $meta_instance->deinitialize_slot( $instance, $_ ) for $attr->slots;
748 foreach my $attr ( $self->get_all_attributes ) {
749 if ( $attr->has_value($instance) ) {
750 if ( defined( my $init_arg = $attr->init_arg ) ) {
751 $params{$init_arg} = $attr->get_value($instance)
752 unless exists $params{$init_arg};
755 $attr->set_value($instance, $attr->get_value($instance));
760 foreach my $attr ($self->get_all_attributes) {
761 $attr->initialize_instance_slot($meta_instance, $instance, \%params);
765 sub _attach_attribute {
766 my ($self, $attribute) = @_;
767 $attribute->attach_to_class($self);
770 sub _post_add_attribute {
771 my ( $self, $attribute ) = @_;
773 $self->invalidate_meta_instances;
775 # invalidate package flag here
778 $attribute->install_accessors;
781 $self->remove_attribute( $attribute->name );
786 sub remove_attribute {
789 my $removed_attribute = $self->SUPER::remove_attribute(@_)
792 $self->invalidate_meta_instances;
794 $removed_attribute->remove_accessors;
795 $removed_attribute->detach_from_class;
797 return$removed_attribute;
800 sub find_attribute_by_name {
801 my ( $self, $attr_name ) = @_;
803 foreach my $class ( $self->linearized_isa ) {
804 # fetch the meta-class ...
805 my $meta = Class::MOP::Class->initialize($class);
806 return $meta->get_attribute($attr_name)
807 if $meta->has_attribute($attr_name);
813 sub get_all_attributes {
815 my %attrs = map { %{ Class::MOP::Class->initialize($_)->_attribute_map } }
816 reverse $self->linearized_isa;
817 return values %attrs;
825 my $isa = $self->get_or_add_package_symbol(
826 { sigil => '@', type => 'ARRAY', name => 'ISA' } );
833 # on 5.8 and below, we need to call
834 # a method to get Perl to detect
835 # a cycle in the class hierarchy
836 my $class = $self->name;
840 # we need to check the metaclass
841 # compatibility here so that we can
842 # be sure that the superclass is
843 # not potentially creating an issues
844 # we don't know about
846 $self->_check_metaclass_compatibility();
847 $self->_superclasses_updated();
853 sub _superclasses_updated {
855 $self->update_meta_instance_dependencies();
860 my $super_class = $self->name;
862 return @{ $super_class->mro::get_isarev() };
865 sub direct_subclasses {
867 my $super_class = $self->name;
872 } Class::MOP::Class->initialize($_)->superclasses
877 return @{ mro::get_linear_isa( (shift)->name ) };
880 sub class_precedence_list {
882 my $name = $self->name;
884 unless (Class::MOP::IS_RUNNING_ON_5_10()) {
886 # We need to check for circular inheritance here
887 # if we are are not on 5.10, cause 5.8 detects it
888 # late. This will do nothing if all is well, and
889 # blow up otherwise. Yes, it's an ugly hack, better
890 # suggestions are welcome.
892 ($name || return)->isa('This is a test for circular inheritance')
895 # if our mro is c3, we can
896 # just grab the linear_isa
897 if (mro::get_mro($name) eq 'c3') {
898 return @{ mro::get_linear_isa($name) }
902 # we can't grab the linear_isa for dfs
903 # since it has all the duplicates
908 Class::MOP::Class->initialize($_)->class_precedence_list()
909 } $self->superclasses()
917 my $fetch_and_prepare_method = sub {
918 my ($self, $method_name) = @_;
919 my $wrapped_metaclass = $self->wrapped_method_metaclass;
921 my $method = $self->get_method($method_name);
922 # if we dont have local ...
924 # try to find the next method
925 $method = $self->find_next_method_by_name($method_name);
926 # die if it does not exist
928 || confess "The method '$method_name' was not found in the inheritance hierarchy for " . $self->name;
929 # and now make sure to wrap it
930 # even if it is already wrapped
931 # because we need a new sub ref
932 $method = $wrapped_metaclass->wrap($method,
933 package_name => $self->name,
934 name => $method_name,
938 # now make sure we wrap it properly
939 $method = $wrapped_metaclass->wrap($method,
940 package_name => $self->name,
941 name => $method_name,
942 ) unless $method->isa($wrapped_metaclass);
944 $self->add_method($method_name => $method);
948 sub add_before_method_modifier {
949 my ($self, $method_name, $method_modifier) = @_;
950 (defined $method_name && length $method_name)
951 || confess "You must pass in a method name";
952 my $method = $fetch_and_prepare_method->($self, $method_name);
953 $method->add_before_modifier(
954 subname(':before' => $method_modifier)
958 sub add_after_method_modifier {
959 my ($self, $method_name, $method_modifier) = @_;
960 (defined $method_name && length $method_name)
961 || confess "You must pass in a method name";
962 my $method = $fetch_and_prepare_method->($self, $method_name);
963 $method->add_after_modifier(
964 subname(':after' => $method_modifier)
968 sub add_around_method_modifier {
969 my ($self, $method_name, $method_modifier) = @_;
970 (defined $method_name && length $method_name)
971 || confess "You must pass in a method name";
972 my $method = $fetch_and_prepare_method->($self, $method_name);
973 $method->add_around_modifier(
974 subname(':around' => $method_modifier)
979 # the methods above used to be named like this:
980 # ${pkg}::${method}:(before|after|around)
981 # but this proved problematic when using one modifier
982 # to wrap multiple methods (something which is likely
983 # to happen pretty regularly IMO). So instead of naming
984 # it like this, I have chosen to just name them purely
985 # with their modifier names, like so:
986 # :(before|after|around)
987 # The fact is that in a stack trace, it will be fairly
988 # evident from the context what method they are attached
989 # to, and so don't need the fully qualified name.
992 sub find_method_by_name {
993 my ($self, $method_name) = @_;
994 (defined $method_name && length $method_name)
995 || confess "You must define a method name to find";
996 foreach my $class ($self->linearized_isa) {
997 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
998 return $method if defined $method;
1003 sub get_all_methods {
1007 for my $class ( reverse $self->linearized_isa ) {
1008 my $meta = Class::MOP::Class->initialize($class);
1010 $methods{ $_->name } = $_ for $meta->_get_local_methods;
1013 return values %methods;
1016 sub get_all_method_names {
1019 return grep { !$uniq{$_}++ } map { Class::MOP::Class->initialize($_)->get_method_list } $self->linearized_isa;
1022 sub find_all_methods_by_name {
1023 my ($self, $method_name) = @_;
1024 (defined $method_name && length $method_name)
1025 || confess "You must define a method name to find";
1027 foreach my $class ($self->linearized_isa) {
1028 # fetch the meta-class ...
1029 my $meta = Class::MOP::Class->initialize($class);
1031 name => $method_name,
1033 code => $meta->get_method($method_name)
1034 } if $meta->has_method($method_name);
1039 sub find_next_method_by_name {
1040 my ($self, $method_name) = @_;
1041 (defined $method_name && length $method_name)
1042 || confess "You must define a method name to find";
1043 my @cpl = $self->linearized_isa;
1044 shift @cpl; # discard ourselves
1045 foreach my $class (@cpl) {
1046 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
1047 return $method if defined $method;
1052 sub update_meta_instance_dependencies {
1055 if ( $self->{meta_instance_dependencies} ) {
1056 return $self->add_meta_instance_dependencies;
1060 sub add_meta_instance_dependencies {
1063 $self->remove_meta_instance_dependencies;
1065 my @attrs = $self->get_all_attributes();
1068 my @classes = grep { not $seen{ $_->name }++ }
1069 map { $_->associated_class } @attrs;
1071 foreach my $class (@classes) {
1072 $class->add_dependent_meta_instance($self);
1075 $self->{meta_instance_dependencies} = \@classes;
1078 sub remove_meta_instance_dependencies {
1081 if ( my $classes = delete $self->{meta_instance_dependencies} ) {
1082 foreach my $class (@$classes) {
1083 $class->remove_dependent_meta_instance($self);
1093 sub add_dependent_meta_instance {
1094 my ( $self, $metaclass ) = @_;
1095 push @{ $self->{dependent_meta_instances} }, $metaclass;
1098 sub remove_dependent_meta_instance {
1099 my ( $self, $metaclass ) = @_;
1100 my $name = $metaclass->name;
1101 @$_ = grep { $_->name ne $name } @$_
1102 for $self->{dependent_meta_instances};
1105 sub invalidate_meta_instances {
1107 $_->invalidate_meta_instance()
1108 for $self, @{ $self->{dependent_meta_instances} };
1111 sub invalidate_meta_instance {
1113 undef $self->{_meta_instance};
1116 # check if we can reinitialize
1120 # if any local attr is defined
1121 return if $self->get_attribute_list;
1123 # or any non-declared methods
1124 for my $method ( map { $self->get_method($_) } $self->get_method_list ) {
1125 return if $method->isa("Class::MOP::Method::Generated");
1126 # FIXME do we need to enforce this too? return unless $method->isa( $self->method_metaclass );
1134 sub is_mutable { 1 }
1135 sub is_immutable { 0 }
1137 sub immutable_options { %{ $_[0]{__immutable}{options} || {} } }
1139 sub _immutable_options {
1140 my ( $self, @args ) = @_;
1143 inline_accessors => 1,
1144 inline_constructor => 1,
1145 inline_destructor => 0,
1147 immutable_trait => $self->immutable_trait,
1148 constructor_name => $self->constructor_name,
1149 constructor_class => $self->constructor_class,
1150 destructor_class => $self->destructor_class,
1155 sub make_immutable {
1156 my ( $self, @args ) = @_;
1158 if ( $self->is_mutable ) {
1159 $self->_initialize_immutable( $self->_immutable_options(@args) );
1160 $self->_rebless_as_immutable(@args);
1171 if ( $self->is_immutable ) {
1172 my @args = $self->immutable_options;
1173 $self->_rebless_as_mutable();
1174 $self->_remove_inlined_code(@args);
1175 delete $self->{__immutable};
1183 sub _rebless_as_immutable {
1184 my ( $self, @args ) = @_;
1186 $self->{__immutable}{original_class} = ref $self;
1188 bless $self => $self->_immutable_metaclass(@args);
1191 sub _immutable_metaclass {
1192 my ( $self, %args ) = @_;
1194 if ( my $class = $args{immutable_metaclass} ) {
1198 my $trait = $args{immutable_trait} = $self->immutable_trait
1199 || confess "no immutable trait specified for $self";
1201 my $meta = $self->meta;
1202 my $meta_attr = $meta->find_attribute_by_name("immutable_trait");
1206 if ( $meta_attr and $trait eq $meta_attr->default ) {
1207 # if the trait is the same as the default we try and pick a
1208 # predictable name for the immutable metaclass
1209 $class_name = 'Class::MOP::Class::Immutable::' . ref($self);
1212 $class_name = join '::', 'Class::MOP::Class::Immutable::CustomTrait',
1213 $trait, 'ForMetaClass', ref($self);
1217 if Class::MOP::is_class_loaded($class_name);
1219 # If the metaclass is a subclass of CMOP::Class which has had
1220 # metaclass roles applied (via Moose), then we want to make sure
1221 # that we preserve that anonymous class (see Fey::ORM for an
1222 # example of where this matters).
1223 my $meta_name = $meta->_real_ref_name;
1225 my $immutable_meta = $meta_name->create(
1227 superclasses => [ ref $self ],
1230 Class::MOP::MiniTrait::apply( $immutable_meta, $trait );
1232 $immutable_meta->make_immutable(
1233 inline_constructor => 0,
1234 inline_accessors => 0,
1240 sub _remove_inlined_code {
1243 $self->remove_method( $_->name ) for $self->_inlined_methods;
1245 delete $self->{__immutable}{inlined_methods};
1248 sub _inlined_methods { @{ $_[0]{__immutable}{inlined_methods} || [] } }
1250 sub _add_inlined_method {
1251 my ( $self, $method ) = @_;
1253 push @{ $self->{__immutable}{inlined_methods} ||= [] }, $method;
1256 sub _initialize_immutable {
1257 my ( $self, %args ) = @_;
1259 $self->{__immutable}{options} = \%args;
1260 $self->_install_inlined_code(%args);
1263 sub _install_inlined_code {
1264 my ( $self, %args ) = @_;
1267 $self->_inline_accessors(%args) if $args{inline_accessors};
1268 $self->_inline_constructor(%args) if $args{inline_constructor};
1269 $self->_inline_destructor(%args) if $args{inline_destructor};
1272 sub _rebless_as_mutable {
1275 bless $self, $self->_get_mutable_metaclass_name;
1280 sub _inline_accessors {
1283 foreach my $attr_name ( $self->get_attribute_list ) {
1284 $self->get_attribute($attr_name)->install_accessors(1);
1288 sub _inline_constructor {
1289 my ( $self, %args ) = @_;
1291 my $name = $args{constructor_name};
1292 # A class may not even have a constructor, and that's okay.
1293 return unless defined $name;
1295 if ( $self->has_method($name) && !$args{replace_constructor} ) {
1296 my $class = $self->name;
1297 warn "Not inlining a constructor for $class since it defines"
1298 . " its own constructor.\n"
1299 . "If you are certain you don't need to inline your"
1300 . " constructor, specify inline_constructor => 0 in your"
1301 . " call to $class->meta->make_immutable\n";
1305 my $constructor_class = $args{constructor_class};
1307 Class::MOP::load_class($constructor_class);
1309 my $constructor = $constructor_class->new(
1313 package_name => $self->name,
1317 if ( $args{replace_constructor} or $constructor->can_be_inlined ) {
1318 $self->add_method( $name => $constructor );
1319 $self->_add_inlined_method($constructor);
1323 sub _inline_destructor {
1324 my ( $self, %args ) = @_;
1326 ( exists $args{destructor_class} && defined $args{destructor_class} )
1327 || confess "The 'inline_destructor' option is present, but "
1328 . "no destructor class was specified";
1330 if ( $self->has_method('DESTROY') && ! $args{replace_destructor} ) {
1331 my $class = $self->name;
1332 warn "Not inlining a destructor for $class since it defines"
1333 . " its own destructor.\n";
1337 my $destructor_class = $args{destructor_class};
1339 Class::MOP::load_class($destructor_class);
1341 return unless $destructor_class->is_needed($self);
1343 my $destructor = $destructor_class->new(
1346 package_name => $self->name,
1350 if ( $args{replace_destructor} or $destructor->can_be_inlined ) {
1351 $self->add_method( 'DESTROY' => $destructor );
1352 $self->_add_inlined_method($destructor);
1364 Class::MOP::Class - Class Meta Object
1368 # assuming that class Foo
1369 # has been defined, you can
1371 # use this for introspection ...
1373 # add a method to Foo ...
1374 Foo->meta->add_method( 'bar' => sub {...} )
1376 # get a list of all the classes searched
1377 # the method dispatcher in the correct order
1378 Foo->meta->class_precedence_list()
1380 # remove a method from Foo
1381 Foo->meta->remove_method('bar');
1383 # or use this to actually create classes ...
1385 Class::MOP::Class->create(
1388 superclasses => ['Foo'],
1390 Class::MOP::Attribute->new('$bar'),
1391 Class::MOP::Attribute->new('$baz'),
1394 calculate_bar => sub {...},
1395 construct_baz => sub {...}
1402 The Class Protocol is the largest and most complex part of the
1403 Class::MOP meta-object protocol. It controls the introspection and
1404 manipulation of Perl 5 classes, and it can create them as well. The
1405 best way to understand what this module can do is to read the
1406 documentation for each of its methods.
1410 C<Class::MOP::Class> is a subclass of L<Class::MOP::Module>.
1414 =head2 Class construction
1416 These methods all create new C<Class::MOP::Class> objects. These
1417 objects can represent existing classes or they can be used to create
1418 new classes from scratch.
1420 The metaclass object for a given class is a singleton. If you attempt
1421 to create a metaclass for the same class twice, you will just get the
1426 =item B<< Class::MOP::Class->create($package_name, %options) >>
1428 This method creates a new C<Class::MOP::Class> object with the given
1429 package name. It accepts a number of options:
1435 An optional version number for the newly created package.
1439 An optional authority for the newly created package.
1441 =item * superclasses
1443 An optional array reference of superclass names.
1447 An optional hash reference of methods for the class. The keys of the
1448 hash reference are method names and values are subroutine references.
1452 An optional array reference of L<Class::MOP::Attribute> objects.
1456 If true, a C<meta> method will not be installed into the class.
1460 =item B<< Class::MOP::Class->create_anon_class(%options) >>
1462 This method works just like C<< Class::MOP::Class->create >> but it
1463 creates an "anonymous" class. In fact, the class does have a name, but
1464 that name is a unique name generated internally by this module.
1466 It accepts the same C<superclasses>, C<methods>, and C<attributes>
1467 parameters that C<create> accepts.
1469 Anonymous classes are destroyed once the metaclass they are attached
1470 to goes out of scope, and will be removed from Perl's internal symbol
1473 All instances of an anonymous class keep a special reference to the
1474 metaclass object, which prevents the metaclass from going out of scope
1475 while any instances exist.
1477 This only works if the instance is based on a hash reference, however.
1479 =item B<< Class::MOP::Class->initialize($package_name, %options) >>
1481 This method will initialize a C<Class::MOP::Class> object for the
1482 named package. Unlike C<create>, this method I<will not> create a new
1485 The purpose of this method is to retrieve a C<Class::MOP::Class>
1486 object for introspecting an existing class.
1488 If an existing C<Class::MOP::Class> object exists for the named
1489 package, it will be returned, and any options provided will be
1492 If the object does not yet exist, it will be created.
1494 The valid options that can be passed to this method are
1495 C<attribute_metaclass>, C<method_metaclass>,
1496 C<wrapped_method_metaclass>, and C<instance_metaclass>. These are all
1497 optional, and default to the appropriate class in the C<Class::MOP>
1502 =head2 Object instance construction and cloning
1504 These methods are all related to creating and/or cloning object
1509 =item B<< $metaclass->clone_object($instance, %params) >>
1511 This method clones an existing object instance. Any parameters you
1512 provide are will override existing attribute values in the object.
1514 This is a convenience method for cloning an object instance, then
1515 blessing it into the appropriate package.
1517 You could implement a clone method in your class, using this method:
1520 my ($self, %params) = @_;
1521 $self->meta->clone_object($self, %params);
1524 =item B<< $metaclass->rebless_instance($instance, %params) >>
1526 This method changes the class of C<$instance> to the metaclass's class.
1528 You can only rebless an instance into a subclass of its current
1529 class. If you pass any additional parameters, these will be treated
1530 like constructor parameters and used to initialize the object's
1531 attributes. Any existing attributes that are already set will be
1534 Before reblessing the instance, this method will call
1535 C<rebless_instance_away> on the instance's current metaclass. This method
1536 will be passed the instance, the new metaclass, and any parameters
1537 specified to C<rebless_instance>. By default, C<rebless_instance_away>
1538 does nothing; it is merely a hook.
1540 =item B<< $metaclass->rebless_instance_back($instance) >>
1542 Does the same thing as C<rebless_instance>, except that you can only
1543 rebless an instance into one of its superclasses. Any attributes that
1544 do not exist in the superclass will be deinitialized.
1546 This is a much more dangerous operation than C<rebless_instance>,
1547 especially when multiple inheritance is involved, so use this carefully!
1549 =item B<< $metaclass->new_object(%params) >>
1551 This method is used to create a new object of the metaclass's
1552 class. Any parameters you provide are used to initialize the
1553 instance's attributes. A special C<__INSTANCE__> key can be passed to
1554 provide an already generated instance, rather than having Class::MOP
1555 generate it for you. This is mostly useful for using Class::MOP with
1556 foreign classes which generate instances using their own constructors.
1558 =item B<< $metaclass->instance_metaclass >>
1560 Returns the class name of the instance metaclass. See
1561 L<Class::MOP::Instance> for more information on the instance
1564 =item B<< $metaclass->get_meta_instance >>
1566 Returns an instance of the C<instance_metaclass> to be used in the
1567 construction of a new instance of the class.
1569 =item B<< $metaclass->inline_create_instance($class_var) >>
1571 =item B<< $metaclass->inline_rebless_instance($instance_var, $class_var) >>
1573 These methods takes variable names, and use them to create an inline snippet
1574 of code that will create a new instance of the class.
1578 =head2 Informational predicates
1580 These are a few predicate methods for asking information about the
1585 =item B<< $metaclass->is_anon_class >>
1587 This returns true if the class was created by calling C<<
1588 Class::MOP::Class->create_anon_class >>.
1590 =item B<< $metaclass->is_mutable >>
1592 This returns true if the class is still mutable.
1594 =item B<< $metaclass->is_immutable >>
1596 This returns true if the class has been made immutable.
1598 =item B<< $metaclass->is_pristine >>
1600 A class is I<not> pristine if it has non-inherited attributes or if it
1601 has any generated methods.
1605 =head2 Inheritance Relationships
1609 =item B<< $metaclass->superclasses(@superclasses) >>
1611 This is a read-write accessor which represents the superclass
1612 relationships of the metaclass's class.
1614 This is basically sugar around getting and setting C<@ISA>.
1616 =item B<< $metaclass->class_precedence_list >>
1618 This returns a list of all of the class's ancestor classes. The
1619 classes are returned in method dispatch order.
1621 =item B<< $metaclass->linearized_isa >>
1623 This returns a list based on C<class_precedence_list> but with all
1626 =item B<< $metaclass->subclasses >>
1628 This returns a list of all subclasses for this class, even indirect
1631 =item B<< $metaclass->direct_subclasses >>
1633 This returns a list of immediate subclasses for this class, which does not
1634 include indirect subclasses.
1638 =head2 Method introspection and creation
1640 These methods allow you to introspect a class's methods, as well as
1641 add, remove, or change methods.
1643 Determining what is truly a method in a Perl 5 class requires some
1644 heuristics (aka guessing).
1646 Methods defined outside the package with a fully qualified name (C<sub
1647 Package::name { ... }>) will be included. Similarly, methods named
1648 with a fully qualified name using L<Sub::Name> are also included.
1650 However, we attempt to ignore imported functions.
1652 Ultimately, we are using heuristics to determine what truly is a
1653 method in a class, and these heuristics may get the wrong answer in
1654 some edge cases. However, for most "normal" cases the heuristics work
1659 =item B<< $metaclass->get_method($method_name) >>
1661 This will return a L<Class::MOP::Method> for the specified
1662 C<$method_name>. If the class does not have the specified method, it
1665 =item B<< $metaclass->has_method($method_name) >>
1667 Returns a boolean indicating whether or not the class defines the
1668 named method. It does not include methods inherited from parent
1671 =item B<< $metaclass->get_method_list >>
1673 This will return a list of method I<names> for all methods defined in
1676 =item B<< $metaclass->add_method($method_name, $method) >>
1678 This method takes a method name and a subroutine reference, and adds
1679 the method to the class.
1681 The subroutine reference can be a L<Class::MOP::Method>, and you are
1682 strongly encouraged to pass a meta method object instead of a code
1683 reference. If you do so, that object gets stored as part of the
1684 class's method map directly. If not, the meta information will have to
1685 be recreated later, and may be incorrect.
1687 If you provide a method object, this method will clone that object if
1688 the object's package name does not match the class name. This lets us
1689 track the original source of any methods added from other classes
1690 (notably Moose roles).
1692 =item B<< $metaclass->remove_method($method_name) >>
1694 Remove the named method from the class. This method returns the
1695 L<Class::MOP::Method> object for the method.
1697 =item B<< $metaclass->method_metaclass >>
1699 Returns the class name of the method metaclass, see
1700 L<Class::MOP::Method> for more information on the method metaclass.
1702 =item B<< $metaclass->wrapped_method_metaclass >>
1704 Returns the class name of the wrapped method metaclass, see
1705 L<Class::MOP::Method::Wrapped> for more information on the wrapped
1708 =item B<< $metaclass->get_all_methods >>
1710 This will traverse the inheritance hierarchy and return a list of all
1711 the L<Class::MOP::Method> objects for this class and its parents.
1713 =item B<< $metaclass->find_method_by_name($method_name) >>
1715 This will return a L<Class::MOP::Method> for the specified
1716 C<$method_name>. If the class does not have the specified method, it
1719 Unlike C<get_method>, this method I<will> look for the named method in
1722 =item B<< $metaclass->get_all_method_names >>
1724 This will return a list of method I<names> for all of this class's
1725 methods, including inherited methods.
1727 =item B<< $metaclass->find_all_methods_by_name($method_name) >>
1729 This method looks for the named method in the class and all of its
1730 parents. It returns every matching method it finds in the inheritance
1731 tree, so it returns a list of methods.
1733 Each method is returned as a hash reference with three keys. The keys
1734 are C<name>, C<class>, and C<code>. The C<code> key has a
1735 L<Class::MOP::Method> object as its value.
1737 The list of methods is distinct.
1739 =item B<< $metaclass->find_next_method_by_name($method_name) >>
1741 This method returns the first method in any superclass matching the
1742 given name. It is effectively the method that C<SUPER::$method_name>
1747 =head2 Attribute introspection and creation
1749 Because Perl 5 does not have a core concept of attributes in classes,
1750 we can only return information about attributes which have been added
1751 via this class's methods. We cannot discover information about
1752 attributes which are defined in terms of "regular" Perl 5 methods.
1756 =item B<< $metaclass->get_attribute($attribute_name) >>
1758 This will return a L<Class::MOP::Attribute> for the specified
1759 C<$attribute_name>. If the class does not have the specified
1760 attribute, it returns C<undef>.
1762 NOTE that get_attribute does not search superclasses, for that you
1763 need to use C<find_attribute_by_name>.
1765 =item B<< $metaclass->has_attribute($attribute_name) >>
1767 Returns a boolean indicating whether or not the class defines the
1768 named attribute. It does not include attributes inherited from parent
1771 =item B<< $metaclass->get_attribute_list >>
1773 This will return a list of attributes I<names> for all attributes
1774 defined in this class. Note that this operates on the current class
1775 only, it does not traverse the inheritance hierarchy.
1777 =item B<< $metaclass->get_all_attributes >>
1779 This will traverse the inheritance hierarchy and return a list of all
1780 the L<Class::MOP::Attribute> objects for this class and its parents.
1782 =item B<< $metaclass->find_attribute_by_name($attribute_name) >>
1784 This will return a L<Class::MOP::Attribute> for the specified
1785 C<$attribute_name>. If the class does not have the specified
1786 attribute, it returns C<undef>.
1788 Unlike C<get_attribute>, this attribute I<will> look for the named
1789 attribute in superclasses.
1791 =item B<< $metaclass->add_attribute(...) >>
1793 This method accepts either an existing L<Class::MOP::Attribute>
1794 object or parameters suitable for passing to that class's C<new>
1797 The attribute provided will be added to the class.
1799 Any accessor methods defined by the attribute will be added to the
1800 class when the attribute is added.
1802 If an attribute of the same name already exists, the old attribute
1803 will be removed first.
1805 =item B<< $metaclass->remove_attribute($attribute_name) >>
1807 This will remove the named attribute from the class, and
1808 L<Class::MOP::Attribute> object.
1810 Removing an attribute also removes any accessor methods defined by the
1813 However, note that removing an attribute will only affect I<future>
1814 object instances created for this class, not existing instances.
1816 =item B<< $metaclass->attribute_metaclass >>
1818 Returns the class name of the attribute metaclass for this class. By
1819 default, this is L<Class::MOP::Attribute>.
1823 =head2 Class Immutability
1825 Making a class immutable "freezes" the class definition. You can no
1826 longer call methods which alter the class, such as adding or removing
1827 methods or attributes.
1829 Making a class immutable lets us optimize the class by inlining some
1830 methods, and also allows us to optimize some methods on the metaclass
1833 After immutabilization, the metaclass object will cache most informational
1834 methods that returns information about methods or attributes. Methods which
1835 would alter the class, such as C<add_attribute> and C<add_method>, will
1836 throw an error on an immutable metaclass object.
1838 The immutabilization system in L<Moose> takes much greater advantage
1839 of the inlining features than Class::MOP itself does.
1843 =item B<< $metaclass->make_immutable(%options) >>
1845 This method will create an immutable transformer and use it to make
1846 the class and its metaclass object immutable.
1848 This method accepts the following options:
1852 =item * inline_accessors
1854 =item * inline_constructor
1856 =item * inline_destructor
1858 These are all booleans indicating whether the specified method(s)
1861 By default, accessors and the constructor are inlined, but not the
1864 =item * immutable_trait
1866 The name of a class which will be used as a parent class for the
1867 metaclass object being made immutable. This "trait" implements the
1868 post-immutability functionality of the metaclass (but not the
1869 transformation itself).
1871 This defaults to L<Class::MOP::Class::Immutable::Trait>.
1873 =item * constructor_name
1875 This is the constructor method name. This defaults to "new".
1877 =item * constructor_class
1879 The name of the method metaclass for constructors. It will be used to
1880 generate the inlined constructor. This defaults to
1881 "Class::MOP::Method::Constructor".
1883 =item * replace_constructor
1885 This is a boolean indicating whether an existing constructor should be
1886 replaced when inlining a constructor. This defaults to false.
1888 =item * destructor_class
1890 The name of the method metaclass for destructors. It will be used to
1891 generate the inlined destructor. This defaults to
1892 "Class::MOP::Method::Denstructor".
1894 =item * replace_destructor
1896 This is a boolean indicating whether an existing destructor should be
1897 replaced when inlining a destructor. This defaults to false.
1901 =item B<< $metaclass->immutable_options >>
1903 Returns a hash of the options used when making the class immutable, including
1904 both defaults and anything supplied by the user in the call to C<<
1905 $metaclass->make_immutable >>. This is useful if you need to temporarily make
1906 a class mutable and then restore immutability as it was before.
1908 =item B<< $metaclass->make_mutable >>
1910 Calling this method reverse the immutabilization transformation.
1914 =head2 Method Modifiers
1916 Method modifiers are hooks which allow a method to be wrapped with
1917 I<before>, I<after> and I<around> method modifiers. Every time a
1918 method is called, its modifiers are also called.
1920 A class can modify its own methods, as well as methods defined in
1923 =head3 How method modifiers work?
1925 Method modifiers work by wrapping the original method and then
1926 replacing it in the class's symbol table. The wrappers will handle
1927 calling all the modifiers in the appropriate order and preserving the
1928 calling context for the original method.
1930 The return values of C<before> and C<after> modifiers are
1931 ignored. This is because their purpose is B<not> to filter the input
1932 and output of the primary method (this is done with an I<around>
1935 This may seem like an odd restriction to some, but doing this allows
1936 for simple code to be added at the beginning or end of a method call
1937 without altering the function of the wrapped method or placing any
1938 extra responsibility on the code of the modifier.
1940 Of course if you have more complex needs, you can use the C<around>
1941 modifier which allows you to change both the parameters passed to the
1942 wrapped method, as well as its return value.
1944 Before and around modifiers are called in last-defined-first-called
1945 order, while after modifiers are called in first-defined-first-called
1946 order. So the call tree might looks something like this:
1958 =head3 What is the performance impact?
1960 Of course there is a performance cost associated with method
1961 modifiers, but we have made every effort to make that cost directly
1962 proportional to the number of modifier features you use.
1964 The wrapping method does its best to B<only> do as much work as it
1965 absolutely needs to. In order to do this we have moved some of the
1966 performance costs to set-up time, where they are easier to amortize.
1968 All this said, our benchmarks have indicated the following:
1970 simple wrapper with no modifiers 100% slower
1971 simple wrapper with simple before modifier 400% slower
1972 simple wrapper with simple after modifier 450% slower
1973 simple wrapper with simple around modifier 500-550% slower
1974 simple wrapper with all 3 modifiers 1100% slower
1976 These numbers may seem daunting, but you must remember, every feature
1977 comes with some cost. To put things in perspective, just doing a
1978 simple C<AUTOLOAD> which does nothing but extract the name of the
1979 method called and return it costs about 400% over a normal method
1984 =item B<< $metaclass->add_before_method_modifier($method_name, $code) >>
1986 This wraps the specified method with the supplied subroutine
1987 reference. The modifier will be called as a method itself, and will
1988 receive the same arguments as are passed to the method.
1990 When the modifier exits, the wrapped method will be called.
1992 The return value of the modifier will be ignored.
1994 =item B<< $metaclass->add_after_method_modifier($method_name, $code) >>
1996 This wraps the specified method with the supplied subroutine
1997 reference. The modifier will be called as a method itself, and will
1998 receive the same arguments as are passed to the method.
2000 When the wrapped methods exits, the modifier will be called.
2002 The return value of the modifier will be ignored.
2004 =item B<< $metaclass->add_around_method_modifier($method_name, $code) >>
2006 This wraps the specified method with the supplied subroutine
2009 The first argument passed to the modifier will be a subroutine
2010 reference to the wrapped method. The second argument is the object,
2011 and after that come any arguments passed when the method is called.
2013 The around modifier can choose to call the original method, as well as
2014 what arguments to pass if it does so.
2016 The return value of the modifier is what will be seen by the caller.
2020 =head2 Introspection
2024 =item B<< Class::MOP::Class->meta >>
2026 This will return a L<Class::MOP::Class> instance for this class.
2028 It should also be noted that L<Class::MOP> will actually bootstrap
2029 this module by installing a number of attribute meta-objects into its
2036 Stevan Little E<lt>stevan@iinteractive.comE<gt>
2038 =head1 COPYRIGHT AND LICENSE
2040 Copyright 2006-2010 by Infinity Interactive, Inc.
2042 L<http://www.iinteractive.com>
2044 This library is free software; you can redistribute it and/or modify
2045 it under the same terms as Perl itself.