2 package Class::MOP::Class;
7 use Class::MOP::Instance;
8 use Class::MOP::Method::Wrapped;
9 use Class::MOP::Method::Accessor;
10 use Class::MOP::Method::Constructor;
11 use Class::MOP::MiniTrait;
14 use Scalar::Util 'blessed', 'reftype', 'weaken';
15 use Sub::Name 'subname';
16 use Devel::GlobalDestruction 'in_global_destruction';
18 use List::MoreUtils 'all';
20 our $VERSION = '1.09';
21 $VERSION = eval $VERSION;
22 our $AUTHORITY = 'cpan:STEVAN';
24 use base 'Class::MOP::Module',
25 'Class::MOP::Mixin::HasAttributes',
26 'Class::MOP::Mixin::HasMethods';
36 $package_name = shift;
39 $package_name = $options{package};
42 ($package_name && !ref($package_name))
43 || confess "You must pass a package name and it cannot be blessed";
45 return Class::MOP::get_metaclass_by_name($package_name)
46 || $class->_construct_class_instance(package => $package_name, @_);
50 my ( $class, @args ) = @_;
51 unshift @args, "package" if @args % 2;
53 my $old_metaclass = blessed($options{package})
55 : Class::MOP::get_metaclass_by_name($options{package});
56 $old_metaclass->_remove_generated_metaobjects
57 if $old_metaclass && $old_metaclass->isa('Class::MOP::Class');
58 my $new_metaclass = $class->SUPER::reinitialize(@args);
59 $new_metaclass->_restore_metaobjects_from($old_metaclass)
60 if $old_metaclass && $old_metaclass->isa('Class::MOP::Class');
61 return $new_metaclass;
64 # NOTE: (meta-circularity)
65 # this is a special form of _construct_instance
66 # (see below), which is used to construct class
67 # meta-object instances for any Class::MOP::*
68 # class. All other classes will use the more
69 # normal &construct_instance.
70 sub _construct_class_instance {
72 my $options = @_ == 1 ? $_[0] : {@_};
73 my $package_name = $options->{package};
74 (defined $package_name && $package_name)
75 || confess "You must pass a package name";
77 # return the metaclass if we have it cached,
78 # and it is still defined (it has not been
79 # reaped by DESTROY yet, which can happen
80 # annoyingly enough during global destruction)
82 if (defined(my $meta = Class::MOP::get_metaclass_by_name($package_name))) {
88 ? $class->_real_ref_name
91 # now create the metaclass
93 if ($class eq 'Class::MOP::Class') {
94 $meta = $class->_new($options);
98 # it is safe to use meta here because
99 # class will always be a subclass of
100 # Class::MOP::Class, which defines meta
101 $meta = $class->meta->_construct_instance($options)
104 # and check the metaclass compatibility
105 $meta->_check_metaclass_compatibility();
107 Class::MOP::store_metaclass_by_name($package_name, $meta);
110 # we need to weaken any anon classes
111 # so that they can call DESTROY properly
112 Class::MOP::weaken_metaclass($package_name) if $options->{weaken};
120 # NOTE: we need to deal with the possibility of class immutability here,
121 # and then get the name of the class appropriately
122 return $self->is_immutable
123 ? $self->_get_mutable_metaclass_name()
130 return Class::MOP::Class->initialize($class)->new_object(@_)
131 if $class ne __PACKAGE__;
133 my $options = @_ == 1 ? $_[0] : {@_};
136 # inherited from Class::MOP::Package
137 'package' => $options->{package},
140 # since the following attributes will
141 # actually be loaded from the symbol
142 # table, and actually bypass the instance
143 # entirely, we can just leave these things
144 # listed here for reference, because they
145 # should not actually have a value associated
147 'namespace' => \undef,
150 # inherited from Class::MOP::Module
152 'authority' => \undef,
154 # defined in Class::MOP::Class
155 'superclasses' => \undef,
158 'attribute_metaclass' =>
159 ( $options->{'attribute_metaclass'} || 'Class::MOP::Attribute' ),
160 'method_metaclass' =>
161 ( $options->{'method_metaclass'} || 'Class::MOP::Method' ),
162 'wrapped_method_metaclass' => (
163 $options->{'wrapped_method_metaclass'}
164 || 'Class::MOP::Method::Wrapped'
166 'instance_metaclass' =>
167 ( $options->{'instance_metaclass'} || 'Class::MOP::Instance' ),
168 'immutable_trait' => (
169 $options->{'immutable_trait'}
170 || 'Class::MOP::Class::Immutable::Trait'
172 'constructor_name' => ( $options->{constructor_name} || 'new' ),
173 'constructor_class' => (
174 $options->{constructor_class} || 'Class::MOP::Method::Constructor'
176 'destructor_class' => $options->{destructor_class},
180 sub reset_package_cache_flag { (shift)->{'_package_cache_flag'} = undef }
181 sub update_package_cache_flag {
184 # we can manually update the cache number
185 # since we are actually adding the method
186 # to our cache as well. This avoids us
187 # having to regenerate the method_map.
189 $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);
192 ## Metaclass compatibility
194 my %base_metaclass = (
195 attribute_metaclass => 'Class::MOP::Attribute',
196 method_metaclass => 'Class::MOP::Method',
197 wrapped_method_metaclass => 'Class::MOP::Method::Wrapped',
198 instance_metaclass => 'Class::MOP::Instance',
199 constructor_class => 'Class::MOP::Method::Constructor',
200 destructor_class => 'Class::MOP::Method::Destructor',
203 sub _base_metaclasses { %base_metaclass }
206 sub _check_metaclass_compatibility {
209 my @superclasses = $self->superclasses
212 $self->_fix_metaclass_incompatibility(@superclasses);
214 my %base_metaclass = $self->_base_metaclasses;
216 # this is always okay ...
218 if ref($self) eq 'Class::MOP::Class'
220 my $meta = $self->$_;
221 !defined($meta) || $meta eq $base_metaclass{$_};
223 keys %base_metaclass;
225 for my $superclass (@superclasses) {
226 $self->_check_class_metaclass_compatibility($superclass);
229 for my $metaclass_type ( keys %base_metaclass ) {
230 next unless defined $self->$metaclass_type;
231 for my $superclass (@superclasses) {
232 $self->_check_single_metaclass_compatibility( $metaclass_type,
238 sub _check_class_metaclass_compatibility {
240 my ( $superclass_name ) = @_;
242 if (!$self->_class_metaclass_is_compatible($superclass_name)) {
243 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
245 my $super_meta_type = $super_meta->_real_ref_name;
247 confess "The metaclass of " . $self->name . " ("
248 . (ref($self)) . ")" . " is not compatible with "
249 . "the metaclass of its superclass, "
250 . $superclass_name . " (" . ($super_meta_type) . ")";
254 sub _class_metaclass_is_compatible {
256 my ( $superclass_name ) = @_;
258 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
261 my $super_meta_name = $super_meta->_real_ref_name;
263 return $self->_is_compatible_with($super_meta_name);
266 sub _check_single_metaclass_compatibility {
268 my ( $metaclass_type, $superclass_name ) = @_;
270 if (!$self->_single_metaclass_is_compatible($metaclass_type, $superclass_name)) {
271 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
272 my $metaclass_type_name = $metaclass_type;
273 $metaclass_type_name =~ s/_(?:meta)?class$//;
274 $metaclass_type_name =~ s/_/ /g;
275 confess "The $metaclass_type_name metaclass for "
276 . $self->name . " (" . ($self->$metaclass_type)
277 . ")" . " is not compatible with the "
278 . "$metaclass_type_name metaclass of its "
279 . "superclass, $superclass_name ("
280 . ($super_meta->$metaclass_type) . ")";
284 sub _single_metaclass_is_compatible {
286 my ( $metaclass_type, $superclass_name ) = @_;
288 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
291 # for instance, Moose::Meta::Class has a error_class attribute, but
292 # Class::MOP::Class doesn't - this shouldn't be an error
293 return 1 unless $super_meta->can($metaclass_type);
294 # for instance, Moose::Meta::Class has a destructor_class, but
295 # Class::MOP::Class doesn't - this shouldn't be an error
296 return 1 unless defined $super_meta->$metaclass_type;
297 # if metaclass is defined in superclass but not here, it's not compatible
298 # this is a really odd case
299 return 0 unless defined $self->$metaclass_type;
301 return $self->$metaclass_type->_is_compatible_with($super_meta->$metaclass_type);
304 sub _fix_metaclass_incompatibility {
306 my @supers = map { Class::MOP::Class->initialize($_) } @_;
309 for my $super (@supers) {
311 if $self->_can_fix_metaclass_incompatibility($super);
313 return unless $necessary;
315 for my $super (@supers) {
316 if (!$self->_class_metaclass_is_compatible($super->name)) {
317 $self->_fix_class_metaclass_incompatibility($super);
321 my %base_metaclass = $self->_base_metaclasses;
322 for my $metaclass_type (keys %base_metaclass) {
323 for my $super (@supers) {
324 if (!$self->_single_metaclass_is_compatible($metaclass_type, $super->name)) {
325 $self->_fix_single_metaclass_incompatibility(
326 $metaclass_type, $super
333 sub _can_fix_metaclass_incompatibility {
335 my ($super_meta) = @_;
337 return 1 if $self->_class_metaclass_can_be_made_compatible($super_meta);
339 my %base_metaclass = $self->_base_metaclasses;
340 for my $metaclass_type (keys %base_metaclass) {
341 return 1 if $self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type);
347 sub _class_metaclass_can_be_made_compatible {
349 my ($super_meta) = @_;
351 return $self->_can_be_made_compatible_with($super_meta->_real_ref_name);
354 sub _single_metaclass_can_be_made_compatible {
356 my ($super_meta, $metaclass_type) = @_;
358 my $specific_meta = $self->$metaclass_type;
360 return unless $super_meta->can($metaclass_type);
361 my $super_specific_meta = $super_meta->$metaclass_type;
363 # for instance, Moose::Meta::Class has a destructor_class, but
364 # Class::MOP::Class doesn't - this shouldn't be an error
365 return unless defined $super_specific_meta;
367 # if metaclass is defined in superclass but not here, it's fixable
368 # this is a really odd case
369 return 1 unless defined $specific_meta;
371 return 1 if $specific_meta->_can_be_made_compatible_with($super_specific_meta);
374 sub _fix_class_metaclass_incompatibility {
376 my ( $super_meta ) = @_;
378 if ($self->_class_metaclass_can_be_made_compatible($super_meta)) {
380 || confess "Can't fix metaclass incompatibility for "
382 . " because it is not pristine.";
384 my $super_meta_name = $super_meta->_real_ref_name;
386 $self->_make_compatible_with($super_meta_name);
390 sub _fix_single_metaclass_incompatibility {
392 my ( $metaclass_type, $super_meta ) = @_;
394 if ($self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type)) {
396 || confess "Can't fix metaclass incompatibility for "
398 . " because it is not pristine.";
400 my $new_metaclass = $self->$metaclass_type
401 ? $self->$metaclass_type->_get_compatible_metaclass($super_meta->$metaclass_type)
402 : $super_meta->$metaclass_type;
403 $self->{$metaclass_type} = $new_metaclass;
407 sub _restore_metaobjects_from {
411 $self->_restore_metamethods_from($old_meta);
412 $self->_restore_metaattributes_from($old_meta);
415 sub _remove_generated_metaobjects {
418 for my $attr (map { $self->get_attribute($_) } $self->get_attribute_list) {
419 $attr->remove_accessors;
427 # this should be sufficient, if you have a
428 # use case where it is not, write a test and
430 my $ANON_CLASS_SERIAL = 0;
433 # we need a sufficiently annoying prefix
434 # this should suffice for now, this is
435 # used in a couple of places below, so
436 # need to put it up here for now.
437 my $ANON_CLASS_PREFIX = 'Class::MOP::Class::__ANON__::SERIAL::';
441 no warnings 'uninitialized';
442 $self->name =~ /^$ANON_CLASS_PREFIX/o;
445 sub create_anon_class {
446 my ($class, %options) = @_;
447 $options{weaken} = 1 unless exists $options{weaken};
448 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
449 return $class->create($package_name, %options);
453 # this will only get called for
454 # anon-classes, all other calls
455 # are assumed to occur during
456 # global destruction and so don't
457 # really need to be handled explicitly
461 return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated
463 no warnings 'uninitialized';
464 my $name = $self->name;
465 return unless $name =~ /^$ANON_CLASS_PREFIX/o;
467 # Moose does a weird thing where it replaces the metaclass for
468 # class when fixing metaclass incompatibility. In that case,
469 # we don't want to clean out the namespace now. We can detect
470 # that because Moose will explicitly update the singleton
471 # cache in Class::MOP.
472 my $current_meta = Class::MOP::get_metaclass_by_name($name);
473 return if $current_meta ne $self;
475 my ($serial_id) = ($name =~ /^$ANON_CLASS_PREFIX(\d+)/o);
477 @{$name . '::ISA'} = ();
478 %{$name . '::'} = ();
479 delete ${$ANON_CLASS_PREFIX}{$serial_id . '::'};
481 Class::MOP::remove_metaclass_by_name($name);
486 # creating classes with MOP ...
489 my ( $class, @args ) = @_;
491 unshift @args, 'package' if @args % 2 == 1;
493 my (%options) = @args;
494 my $package_name = $options{package};
496 (ref $options{superclasses} eq 'ARRAY')
497 || confess "You must pass an ARRAY ref of superclasses"
498 if exists $options{superclasses};
500 (ref $options{attributes} eq 'ARRAY')
501 || confess "You must pass an ARRAY ref of attributes"
502 if exists $options{attributes};
504 (ref $options{methods} eq 'HASH')
505 || confess "You must pass a HASH ref of methods"
506 if exists $options{methods};
508 $options{meta_name} = 'meta'
509 unless exists $options{meta_name};
511 my (%initialize_options) = @args;
512 delete @initialize_options{qw(
521 my $meta = $class->initialize( $package_name => %initialize_options );
523 $meta->_instantiate_module( $options{version}, $options{authority} );
525 $meta->_add_meta_method($options{meta_name})
526 if defined $options{meta_name};
528 $meta->superclasses(@{$options{superclasses}})
529 if exists $options{superclasses};
531 # process attributes first, so that they can
532 # install accessors, but locally defined methods
533 # can then overwrite them. It is maybe a little odd, but
534 # I think this should be the order of things.
535 if (exists $options{attributes}) {
536 foreach my $attr (@{$options{attributes}}) {
537 $meta->add_attribute($attr);
540 if (exists $options{methods}) {
541 foreach my $method_name (keys %{$options{methods}}) {
542 $meta->add_method($method_name, $options{methods}->{$method_name});
551 # all these attribute readers will be bootstrapped
552 # away in the Class::MOP bootstrap section
554 sub instance_metaclass { $_[0]->{'instance_metaclass'} }
555 sub immutable_trait { $_[0]->{'immutable_trait'} }
556 sub constructor_class { $_[0]->{'constructor_class'} }
557 sub constructor_name { $_[0]->{'constructor_name'} }
558 sub destructor_class { $_[0]->{'destructor_class'} }
560 # Instance Construction & Cloning
566 # we need to protect the integrity of the
567 # Class::MOP::Class singletons here, so we
568 # delegate this to &construct_class_instance
569 # which will deal with the singletons
570 return $class->_construct_class_instance(@_)
571 if $class->name->isa('Class::MOP::Class');
572 return $class->_construct_instance(@_);
575 sub _construct_instance {
577 my $params = @_ == 1 ? $_[0] : {@_};
578 my $meta_instance = $class->get_meta_instance();
580 # the code below is almost certainly incorrect
581 # but this is foreign inheritance, so we might
582 # have to kludge it in the end.
584 if (my $instance_class = blessed($params->{__INSTANCE__})) {
585 ($instance_class eq $class->name)
586 || confess "Objects passed as the __INSTANCE__ parameter must "
587 . "already be blessed into the correct class, but "
588 . "$params->{__INSTANCE__} is not a " . $class->name;
589 $instance = $params->{__INSTANCE__};
591 elsif (exists $params->{__INSTANCE__}) {
592 confess "The __INSTANCE__ parameter must be a blessed reference, not "
593 . $params->{__INSTANCE__};
596 $instance = $meta_instance->create_instance();
598 foreach my $attr ($class->get_all_attributes()) {
599 $attr->initialize_instance_slot($meta_instance, $instance, $params);
602 # this will only work for a HASH instance type
603 if ($class->is_anon_class) {
604 (reftype($instance) eq 'HASH')
605 || confess "Currently only HASH based instances are supported with instance of anon-classes";
607 # At some point we should make this official
608 # as a reserved slot name, but right now I am
609 # going to keep it here.
610 # my $RESERVED_MOP_SLOT = '__MOP__';
611 $instance->{'__MOP__'} = $class;
617 sub get_meta_instance {
619 $self->{'_meta_instance'} ||= $self->_create_meta_instance();
622 sub _create_meta_instance {
625 my $instance = $self->instance_metaclass->new(
626 associated_metaclass => $self,
627 attributes => [ $self->get_all_attributes() ],
630 $self->add_meta_instance_dependencies()
631 if $instance->is_dependent_on_superclasses();
636 sub inline_create_instance {
639 return $self->get_meta_instance->inline_create_instance(@_);
642 sub inline_rebless_instance {
645 return $self->get_meta_instance->inline_rebless_instance_structure(@_);
650 my $instance = shift;
651 (blessed($instance) && $instance->isa($class->name))
652 || confess "You must pass an instance of the metaclass (" . (ref $class ? $class->name : $class) . "), not ($instance)";
655 # we need to protect the integrity of the
656 # Class::MOP::Class singletons here, they
657 # should not be cloned.
658 return $instance if $instance->isa('Class::MOP::Class');
659 $class->_clone_instance($instance, @_);
662 sub _clone_instance {
663 my ($class, $instance, %params) = @_;
665 || confess "You can only clone instances, ($instance) is not a blessed instance";
666 my $meta_instance = $class->get_meta_instance();
667 my $clone = $meta_instance->clone_instance($instance);
668 foreach my $attr ($class->get_all_attributes()) {
669 if ( defined( my $init_arg = $attr->init_arg ) ) {
670 if (exists $params{$init_arg}) {
671 $attr->set_value($clone, $params{$init_arg});
678 sub _force_rebless_instance {
679 my ($self, $instance, %params) = @_;
680 my $old_metaclass = Class::MOP::class_of($instance);
682 $old_metaclass->rebless_instance_away($instance, $self, %params)
685 if ($old_metaclass->is_anon_class) {
686 delete $instance->{__MOP__};
689 my $meta_instance = $self->get_meta_instance;
692 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
693 $meta_instance->rebless_instance_structure($_[1], $self);
695 $self->_fixup_attributes_after_rebless($instance, $old_metaclass, %params);
698 # this will only work for a HASH instance type
699 if ($self->is_anon_class) {
700 (reftype($instance) eq 'HASH')
701 || confess "Currently only HASH based instances are supported with instance of anon-classes";
703 # At some point we should make this official
704 # as a reserved slot name, but right now I am
705 # going to keep it here.
706 # my $RESERVED_MOP_SLOT = '__MOP__';
707 $instance->{'__MOP__'} = $self;
711 sub rebless_instance {
712 my ($self, $instance, %params) = @_;
713 my $old_metaclass = Class::MOP::class_of($instance);
715 my $old_class = $old_metaclass ? $old_metaclass->name : blessed($instance);
716 $self->name->isa($old_class)
717 || confess "You may rebless only into a subclass of ($old_class), of which (". $self->name .") isn't.";
719 $self->_force_rebless_instance($_[1], %params);
724 sub rebless_instance_back {
725 my ( $self, $instance ) = @_;
726 my $old_metaclass = Class::MOP::class_of($instance);
729 = $old_metaclass ? $old_metaclass->name : blessed($instance);
730 $old_class->isa( $self->name )
732 "You may rebless only into a superclass of ($old_class), of which ("
736 $self->_force_rebless_instance($_[1]);
741 sub rebless_instance_away {
742 # this intentionally does nothing, it is just a hook
745 sub _fixup_attributes_after_rebless {
747 my ($instance, $rebless_from, %params) = @_;
748 my $meta_instance = $self->get_meta_instance;
750 for my $attr ( $rebless_from->get_all_attributes ) {
751 next if $self->find_attribute_by_name( $attr->name );
752 $meta_instance->deinitialize_slot( $instance, $_ ) for $attr->slots;
755 foreach my $attr ( $self->get_all_attributes ) {
756 if ( $attr->has_value($instance) ) {
757 if ( defined( my $init_arg = $attr->init_arg ) ) {
758 $params{$init_arg} = $attr->get_value($instance)
759 unless exists $params{$init_arg};
762 $attr->set_value($instance, $attr->get_value($instance));
767 foreach my $attr ($self->get_all_attributes) {
768 $attr->initialize_instance_slot($meta_instance, $instance, \%params);
772 sub _attach_attribute {
773 my ($self, $attribute) = @_;
774 $attribute->attach_to_class($self);
777 sub _post_add_attribute {
778 my ( $self, $attribute ) = @_;
780 $self->invalidate_meta_instances;
782 # invalidate package flag here
785 $attribute->install_accessors;
788 $self->remove_attribute( $attribute->name );
793 sub remove_attribute {
796 my $removed_attribute = $self->SUPER::remove_attribute(@_)
799 $self->invalidate_meta_instances;
801 $removed_attribute->remove_accessors;
802 $removed_attribute->detach_from_class;
804 return$removed_attribute;
807 sub find_attribute_by_name {
808 my ( $self, $attr_name ) = @_;
810 foreach my $class ( $self->linearized_isa ) {
811 # fetch the meta-class ...
812 my $meta = Class::MOP::Class->initialize($class);
813 return $meta->get_attribute($attr_name)
814 if $meta->has_attribute($attr_name);
820 sub get_all_attributes {
822 my %attrs = map { %{ Class::MOP::Class->initialize($_)->_attribute_map } }
823 reverse $self->linearized_isa;
824 return values %attrs;
832 my $isa = $self->get_or_add_package_symbol(
833 { sigil => '@', type => 'ARRAY', name => 'ISA' } );
840 # on 5.8 and below, we need to call
841 # a method to get Perl to detect
842 # a cycle in the class hierarchy
843 my $class = $self->name;
847 # we need to check the metaclass
848 # compatibility here so that we can
849 # be sure that the superclass is
850 # not potentially creating an issues
851 # we don't know about
853 $self->_check_metaclass_compatibility();
854 $self->_superclasses_updated();
860 sub _superclasses_updated {
862 $self->update_meta_instance_dependencies();
863 # keep strong references to all our parents, so they don't disappear if
864 # they are anon classes and don't have any direct instances
865 $self->_superclass_metas(
866 map { Class::MOP::class_of($_) } $self->superclasses
870 sub _superclass_metas {
872 $self->{_superclass_metas} = [@_];
877 my $super_class = $self->name;
879 return @{ $super_class->mro::get_isarev() };
882 sub direct_subclasses {
884 my $super_class = $self->name;
889 } Class::MOP::Class->initialize($_)->superclasses
894 return @{ mro::get_linear_isa( (shift)->name ) };
897 sub class_precedence_list {
899 my $name = $self->name;
901 unless (Class::MOP::IS_RUNNING_ON_5_10()) {
903 # We need to check for circular inheritance here
904 # if we are are not on 5.10, cause 5.8 detects it
905 # late. This will do nothing if all is well, and
906 # blow up otherwise. Yes, it's an ugly hack, better
907 # suggestions are welcome.
909 ($name || return)->isa('This is a test for circular inheritance')
912 # if our mro is c3, we can
913 # just grab the linear_isa
914 if (mro::get_mro($name) eq 'c3') {
915 return @{ mro::get_linear_isa($name) }
919 # we can't grab the linear_isa for dfs
920 # since it has all the duplicates
925 Class::MOP::Class->initialize($_)->class_precedence_list()
926 } $self->superclasses()
934 my $fetch_and_prepare_method = sub {
935 my ($self, $method_name) = @_;
936 my $wrapped_metaclass = $self->wrapped_method_metaclass;
938 my $method = $self->get_method($method_name);
939 # if we dont have local ...
941 # try to find the next method
942 $method = $self->find_next_method_by_name($method_name);
943 # die if it does not exist
945 || confess "The method '$method_name' was not found in the inheritance hierarchy for " . $self->name;
946 # and now make sure to wrap it
947 # even if it is already wrapped
948 # because we need a new sub ref
949 $method = $wrapped_metaclass->wrap($method,
950 package_name => $self->name,
951 name => $method_name,
955 # now make sure we wrap it properly
956 $method = $wrapped_metaclass->wrap($method,
957 package_name => $self->name,
958 name => $method_name,
959 ) unless $method->isa($wrapped_metaclass);
961 $self->add_method($method_name => $method);
965 sub add_before_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_before_modifier(
971 subname(':before' => $method_modifier)
975 sub add_after_method_modifier {
976 my ($self, $method_name, $method_modifier) = @_;
977 (defined $method_name && length $method_name)
978 || confess "You must pass in a method name";
979 my $method = $fetch_and_prepare_method->($self, $method_name);
980 $method->add_after_modifier(
981 subname(':after' => $method_modifier)
985 sub add_around_method_modifier {
986 my ($self, $method_name, $method_modifier) = @_;
987 (defined $method_name && length $method_name)
988 || confess "You must pass in a method name";
989 my $method = $fetch_and_prepare_method->($self, $method_name);
990 $method->add_around_modifier(
991 subname(':around' => $method_modifier)
996 # the methods above used to be named like this:
997 # ${pkg}::${method}:(before|after|around)
998 # but this proved problematic when using one modifier
999 # to wrap multiple methods (something which is likely
1000 # to happen pretty regularly IMO). So instead of naming
1001 # it like this, I have chosen to just name them purely
1002 # with their modifier names, like so:
1003 # :(before|after|around)
1004 # The fact is that in a stack trace, it will be fairly
1005 # evident from the context what method they are attached
1006 # to, and so don't need the fully qualified name.
1009 sub find_method_by_name {
1010 my ($self, $method_name) = @_;
1011 (defined $method_name && length $method_name)
1012 || confess "You must define a method name to find";
1013 foreach my $class ($self->linearized_isa) {
1014 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
1015 return $method if defined $method;
1020 sub get_all_methods {
1024 for my $class ( reverse $self->linearized_isa ) {
1025 my $meta = Class::MOP::Class->initialize($class);
1027 $methods{ $_->name } = $_ for $meta->_get_local_methods;
1030 return values %methods;
1033 sub get_all_method_names {
1036 return grep { !$uniq{$_}++ } map { Class::MOP::Class->initialize($_)->get_method_list } $self->linearized_isa;
1039 sub find_all_methods_by_name {
1040 my ($self, $method_name) = @_;
1041 (defined $method_name && length $method_name)
1042 || confess "You must define a method name to find";
1044 foreach my $class ($self->linearized_isa) {
1045 # fetch the meta-class ...
1046 my $meta = Class::MOP::Class->initialize($class);
1048 name => $method_name,
1050 code => $meta->get_method($method_name)
1051 } if $meta->has_method($method_name);
1056 sub find_next_method_by_name {
1057 my ($self, $method_name) = @_;
1058 (defined $method_name && length $method_name)
1059 || confess "You must define a method name to find";
1060 my @cpl = $self->linearized_isa;
1061 shift @cpl; # discard ourselves
1062 foreach my $class (@cpl) {
1063 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
1064 return $method if defined $method;
1069 sub update_meta_instance_dependencies {
1072 if ( $self->{meta_instance_dependencies} ) {
1073 return $self->add_meta_instance_dependencies;
1077 sub add_meta_instance_dependencies {
1080 $self->remove_meta_instance_dependencies;
1082 my @attrs = $self->get_all_attributes();
1085 my @classes = grep { not $seen{ $_->name }++ }
1086 map { $_->associated_class } @attrs;
1088 foreach my $class (@classes) {
1089 $class->add_dependent_meta_instance($self);
1092 $self->{meta_instance_dependencies} = \@classes;
1095 sub remove_meta_instance_dependencies {
1098 if ( my $classes = delete $self->{meta_instance_dependencies} ) {
1099 foreach my $class (@$classes) {
1100 $class->remove_dependent_meta_instance($self);
1110 sub add_dependent_meta_instance {
1111 my ( $self, $metaclass ) = @_;
1112 push @{ $self->{dependent_meta_instances} }, $metaclass;
1115 sub remove_dependent_meta_instance {
1116 my ( $self, $metaclass ) = @_;
1117 my $name = $metaclass->name;
1118 @$_ = grep { $_->name ne $name } @$_
1119 for $self->{dependent_meta_instances};
1122 sub invalidate_meta_instances {
1124 $_->invalidate_meta_instance()
1125 for $self, @{ $self->{dependent_meta_instances} };
1128 sub invalidate_meta_instance {
1130 undef $self->{_meta_instance};
1133 # check if we can reinitialize
1137 # if any local attr is defined
1138 return if $self->get_attribute_list;
1140 # or any non-declared methods
1141 for my $method ( map { $self->get_method($_) } $self->get_method_list ) {
1142 return if $method->isa("Class::MOP::Method::Generated");
1143 # FIXME do we need to enforce this too? return unless $method->isa( $self->method_metaclass );
1151 sub is_mutable { 1 }
1152 sub is_immutable { 0 }
1154 sub immutable_options { %{ $_[0]{__immutable}{options} || {} } }
1156 sub _immutable_options {
1157 my ( $self, @args ) = @_;
1160 inline_accessors => 1,
1161 inline_constructor => 1,
1162 inline_destructor => 0,
1164 immutable_trait => $self->immutable_trait,
1165 constructor_name => $self->constructor_name,
1166 constructor_class => $self->constructor_class,
1167 destructor_class => $self->destructor_class,
1172 sub make_immutable {
1173 my ( $self, @args ) = @_;
1175 if ( $self->is_mutable ) {
1176 $self->_initialize_immutable( $self->_immutable_options(@args) );
1177 $self->_rebless_as_immutable(@args);
1188 if ( $self->is_immutable ) {
1189 my @args = $self->immutable_options;
1190 $self->_rebless_as_mutable();
1191 $self->_remove_inlined_code(@args);
1192 delete $self->{__immutable};
1200 sub _rebless_as_immutable {
1201 my ( $self, @args ) = @_;
1203 $self->{__immutable}{original_class} = ref $self;
1205 bless $self => $self->_immutable_metaclass(@args);
1208 sub _immutable_metaclass {
1209 my ( $self, %args ) = @_;
1211 if ( my $class = $args{immutable_metaclass} ) {
1215 my $trait = $args{immutable_trait} = $self->immutable_trait
1216 || confess "no immutable trait specified for $self";
1218 my $meta = $self->meta;
1219 my $meta_attr = $meta->find_attribute_by_name("immutable_trait");
1223 if ( $meta_attr and $trait eq $meta_attr->default ) {
1224 # if the trait is the same as the default we try and pick a
1225 # predictable name for the immutable metaclass
1226 $class_name = 'Class::MOP::Class::Immutable::' . ref($self);
1229 $class_name = join '::', 'Class::MOP::Class::Immutable::CustomTrait',
1230 $trait, 'ForMetaClass', ref($self);
1234 if Class::MOP::is_class_loaded($class_name);
1236 # If the metaclass is a subclass of CMOP::Class which has had
1237 # metaclass roles applied (via Moose), then we want to make sure
1238 # that we preserve that anonymous class (see Fey::ORM for an
1239 # example of where this matters).
1240 my $meta_name = $meta->_real_ref_name;
1242 my $immutable_meta = $meta_name->create(
1244 superclasses => [ ref $self ],
1247 Class::MOP::MiniTrait::apply( $immutable_meta, $trait );
1249 $immutable_meta->make_immutable(
1250 inline_constructor => 0,
1251 inline_accessors => 0,
1257 sub _remove_inlined_code {
1260 $self->remove_method( $_->name ) for $self->_inlined_methods;
1262 delete $self->{__immutable}{inlined_methods};
1265 sub _inlined_methods { @{ $_[0]{__immutable}{inlined_methods} || [] } }
1267 sub _add_inlined_method {
1268 my ( $self, $method ) = @_;
1270 push @{ $self->{__immutable}{inlined_methods} ||= [] }, $method;
1273 sub _initialize_immutable {
1274 my ( $self, %args ) = @_;
1276 $self->{__immutable}{options} = \%args;
1277 $self->_install_inlined_code(%args);
1280 sub _install_inlined_code {
1281 my ( $self, %args ) = @_;
1284 $self->_inline_accessors(%args) if $args{inline_accessors};
1285 $self->_inline_constructor(%args) if $args{inline_constructor};
1286 $self->_inline_destructor(%args) if $args{inline_destructor};
1289 sub _rebless_as_mutable {
1292 bless $self, $self->_get_mutable_metaclass_name;
1297 sub _inline_accessors {
1300 foreach my $attr_name ( $self->get_attribute_list ) {
1301 $self->get_attribute($attr_name)->install_accessors(1);
1305 sub _inline_constructor {
1306 my ( $self, %args ) = @_;
1308 my $name = $args{constructor_name};
1309 # A class may not even have a constructor, and that's okay.
1310 return unless defined $name;
1312 if ( $self->has_method($name) && !$args{replace_constructor} ) {
1313 my $class = $self->name;
1314 warn "Not inlining a constructor for $class since it defines"
1315 . " its own constructor.\n"
1316 . "If you are certain you don't need to inline your"
1317 . " constructor, specify inline_constructor => 0 in your"
1318 . " call to $class->meta->make_immutable\n";
1322 my $constructor_class = $args{constructor_class};
1324 Class::MOP::load_class($constructor_class);
1326 my $constructor = $constructor_class->new(
1330 package_name => $self->name,
1334 if ( $args{replace_constructor} or $constructor->can_be_inlined ) {
1335 $self->add_method( $name => $constructor );
1336 $self->_add_inlined_method($constructor);
1340 sub _inline_destructor {
1341 my ( $self, %args ) = @_;
1343 ( exists $args{destructor_class} && defined $args{destructor_class} )
1344 || confess "The 'inline_destructor' option is present, but "
1345 . "no destructor class was specified";
1347 if ( $self->has_method('DESTROY') && ! $args{replace_destructor} ) {
1348 my $class = $self->name;
1349 warn "Not inlining a destructor for $class since it defines"
1350 . " its own destructor.\n";
1354 my $destructor_class = $args{destructor_class};
1356 Class::MOP::load_class($destructor_class);
1358 return unless $destructor_class->is_needed($self);
1360 my $destructor = $destructor_class->new(
1363 package_name => $self->name,
1367 if ( $args{replace_destructor} or $destructor->can_be_inlined ) {
1368 $self->add_method( 'DESTROY' => $destructor );
1369 $self->_add_inlined_method($destructor);
1381 Class::MOP::Class - Class Meta Object
1385 # assuming that class Foo
1386 # has been defined, you can
1388 # use this for introspection ...
1390 # add a method to Foo ...
1391 Foo->meta->add_method( 'bar' => sub {...} )
1393 # get a list of all the classes searched
1394 # the method dispatcher in the correct order
1395 Foo->meta->class_precedence_list()
1397 # remove a method from Foo
1398 Foo->meta->remove_method('bar');
1400 # or use this to actually create classes ...
1402 Class::MOP::Class->create(
1405 superclasses => ['Foo'],
1407 Class::MOP::Attribute->new('$bar'),
1408 Class::MOP::Attribute->new('$baz'),
1411 calculate_bar => sub {...},
1412 construct_baz => sub {...}
1419 The Class Protocol is the largest and most complex part of the
1420 Class::MOP meta-object protocol. It controls the introspection and
1421 manipulation of Perl 5 classes, and it can create them as well. The
1422 best way to understand what this module can do is to read the
1423 documentation for each of its methods.
1427 C<Class::MOP::Class> is a subclass of L<Class::MOP::Module>.
1431 =head2 Class construction
1433 These methods all create new C<Class::MOP::Class> objects. These
1434 objects can represent existing classes or they can be used to create
1435 new classes from scratch.
1437 The metaclass object for a given class is a singleton. If you attempt
1438 to create a metaclass for the same class twice, you will just get the
1443 =item B<< Class::MOP::Class->create($package_name, %options) >>
1445 This method creates a new C<Class::MOP::Class> object with the given
1446 package name. It accepts a number of options:
1452 An optional version number for the newly created package.
1456 An optional authority for the newly created package.
1458 =item * superclasses
1460 An optional array reference of superclass names.
1464 An optional hash reference of methods for the class. The keys of the
1465 hash reference are method names and values are subroutine references.
1469 An optional array reference of L<Class::MOP::Attribute> objects.
1473 Specifies the name to install the C<meta> method for this class under.
1474 If it is not passed, C<meta> is assumed, and if C<undef> is explicitly
1475 given, no meta method will be installed.
1479 =item B<< Class::MOP::Class->create_anon_class(%options) >>
1481 This method works just like C<< Class::MOP::Class->create >> but it
1482 creates an "anonymous" class. In fact, the class does have a name, but
1483 that name is a unique name generated internally by this module.
1485 It accepts the same C<superclasses>, C<methods>, and C<attributes>
1486 parameters that C<create> accepts.
1488 Anonymous classes are destroyed once the metaclass they are attached
1489 to goes out of scope, and will be removed from Perl's internal symbol
1492 All instances of an anonymous class keep a special reference to the
1493 metaclass object, which prevents the metaclass from going out of scope
1494 while any instances exist.
1496 This only works if the instance is based on a hash reference, however.
1498 =item B<< Class::MOP::Class->initialize($package_name, %options) >>
1500 This method will initialize a C<Class::MOP::Class> object for the
1501 named package. Unlike C<create>, this method I<will not> create a new
1504 The purpose of this method is to retrieve a C<Class::MOP::Class>
1505 object for introspecting an existing class.
1507 If an existing C<Class::MOP::Class> object exists for the named
1508 package, it will be returned, and any options provided will be
1511 If the object does not yet exist, it will be created.
1513 The valid options that can be passed to this method are
1514 C<attribute_metaclass>, C<method_metaclass>,
1515 C<wrapped_method_metaclass>, and C<instance_metaclass>. These are all
1516 optional, and default to the appropriate class in the C<Class::MOP>
1521 =head2 Object instance construction and cloning
1523 These methods are all related to creating and/or cloning object
1528 =item B<< $metaclass->clone_object($instance, %params) >>
1530 This method clones an existing object instance. Any parameters you
1531 provide are will override existing attribute values in the object.
1533 This is a convenience method for cloning an object instance, then
1534 blessing it into the appropriate package.
1536 You could implement a clone method in your class, using this method:
1539 my ($self, %params) = @_;
1540 $self->meta->clone_object($self, %params);
1543 =item B<< $metaclass->rebless_instance($instance, %params) >>
1545 This method changes the class of C<$instance> to the metaclass's class.
1547 You can only rebless an instance into a subclass of its current
1548 class. If you pass any additional parameters, these will be treated
1549 like constructor parameters and used to initialize the object's
1550 attributes. Any existing attributes that are already set will be
1553 Before reblessing the instance, this method will call
1554 C<rebless_instance_away> on the instance's current metaclass. This method
1555 will be passed the instance, the new metaclass, and any parameters
1556 specified to C<rebless_instance>. By default, C<rebless_instance_away>
1557 does nothing; it is merely a hook.
1559 =item B<< $metaclass->rebless_instance_back($instance) >>
1561 Does the same thing as C<rebless_instance>, except that you can only
1562 rebless an instance into one of its superclasses. Any attributes that
1563 do not exist in the superclass will be deinitialized.
1565 This is a much more dangerous operation than C<rebless_instance>,
1566 especially when multiple inheritance is involved, so use this carefully!
1568 =item B<< $metaclass->new_object(%params) >>
1570 This method is used to create a new object of the metaclass's
1571 class. Any parameters you provide are used to initialize the
1572 instance's attributes. A special C<__INSTANCE__> key can be passed to
1573 provide an already generated instance, rather than having Class::MOP
1574 generate it for you. This is mostly useful for using Class::MOP with
1575 foreign classes which generate instances using their own constructors.
1577 =item B<< $metaclass->instance_metaclass >>
1579 Returns the class name of the instance metaclass. See
1580 L<Class::MOP::Instance> for more information on the instance
1583 =item B<< $metaclass->get_meta_instance >>
1585 Returns an instance of the C<instance_metaclass> to be used in the
1586 construction of a new instance of the class.
1588 =item B<< $metaclass->inline_create_instance($class_var) >>
1590 =item B<< $metaclass->inline_rebless_instance($instance_var, $class_var) >>
1592 These methods takes variable names, and use them to create an inline snippet
1593 of code that will create a new instance of the class.
1597 =head2 Informational predicates
1599 These are a few predicate methods for asking information about the
1604 =item B<< $metaclass->is_anon_class >>
1606 This returns true if the class was created by calling C<<
1607 Class::MOP::Class->create_anon_class >>.
1609 =item B<< $metaclass->is_mutable >>
1611 This returns true if the class is still mutable.
1613 =item B<< $metaclass->is_immutable >>
1615 This returns true if the class has been made immutable.
1617 =item B<< $metaclass->is_pristine >>
1619 A class is I<not> pristine if it has non-inherited attributes or if it
1620 has any generated methods.
1624 =head2 Inheritance Relationships
1628 =item B<< $metaclass->superclasses(@superclasses) >>
1630 This is a read-write accessor which represents the superclass
1631 relationships of the metaclass's class.
1633 This is basically sugar around getting and setting C<@ISA>.
1635 =item B<< $metaclass->class_precedence_list >>
1637 This returns a list of all of the class's ancestor classes. The
1638 classes are returned in method dispatch order.
1640 =item B<< $metaclass->linearized_isa >>
1642 This returns a list based on C<class_precedence_list> but with all
1645 =item B<< $metaclass->subclasses >>
1647 This returns a list of all subclasses for this class, even indirect
1650 =item B<< $metaclass->direct_subclasses >>
1652 This returns a list of immediate subclasses for this class, which does not
1653 include indirect subclasses.
1657 =head2 Method introspection and creation
1659 These methods allow you to introspect a class's methods, as well as
1660 add, remove, or change methods.
1662 Determining what is truly a method in a Perl 5 class requires some
1663 heuristics (aka guessing).
1665 Methods defined outside the package with a fully qualified name (C<sub
1666 Package::name { ... }>) will be included. Similarly, methods named
1667 with a fully qualified name using L<Sub::Name> are also included.
1669 However, we attempt to ignore imported functions.
1671 Ultimately, we are using heuristics to determine what truly is a
1672 method in a class, and these heuristics may get the wrong answer in
1673 some edge cases. However, for most "normal" cases the heuristics work
1678 =item B<< $metaclass->get_method($method_name) >>
1680 This will return a L<Class::MOP::Method> for the specified
1681 C<$method_name>. If the class does not have the specified method, it
1684 =item B<< $metaclass->has_method($method_name) >>
1686 Returns a boolean indicating whether or not the class defines the
1687 named method. It does not include methods inherited from parent
1690 =item B<< $metaclass->get_method_list >>
1692 This will return a list of method I<names> for all methods defined in
1695 =item B<< $metaclass->add_method($method_name, $method) >>
1697 This method takes a method name and a subroutine reference, and adds
1698 the method to the class.
1700 The subroutine reference can be a L<Class::MOP::Method>, and you are
1701 strongly encouraged to pass a meta method object instead of a code
1702 reference. If you do so, that object gets stored as part of the
1703 class's method map directly. If not, the meta information will have to
1704 be recreated later, and may be incorrect.
1706 If you provide a method object, this method will clone that object if
1707 the object's package name does not match the class name. This lets us
1708 track the original source of any methods added from other classes
1709 (notably Moose roles).
1711 =item B<< $metaclass->remove_method($method_name) >>
1713 Remove the named method from the class. This method returns the
1714 L<Class::MOP::Method> object for the method.
1716 =item B<< $metaclass->method_metaclass >>
1718 Returns the class name of the method metaclass, see
1719 L<Class::MOP::Method> for more information on the method metaclass.
1721 =item B<< $metaclass->wrapped_method_metaclass >>
1723 Returns the class name of the wrapped method metaclass, see
1724 L<Class::MOP::Method::Wrapped> for more information on the wrapped
1727 =item B<< $metaclass->get_all_methods >>
1729 This will traverse the inheritance hierarchy and return a list of all
1730 the L<Class::MOP::Method> objects for this class and its parents.
1732 =item B<< $metaclass->find_method_by_name($method_name) >>
1734 This will return a L<Class::MOP::Method> for the specified
1735 C<$method_name>. If the class does not have the specified method, it
1738 Unlike C<get_method>, this method I<will> look for the named method in
1741 =item B<< $metaclass->get_all_method_names >>
1743 This will return a list of method I<names> for all of this class's
1744 methods, including inherited methods.
1746 =item B<< $metaclass->find_all_methods_by_name($method_name) >>
1748 This method looks for the named method in the class and all of its
1749 parents. It returns every matching method it finds in the inheritance
1750 tree, so it returns a list of methods.
1752 Each method is returned as a hash reference with three keys. The keys
1753 are C<name>, C<class>, and C<code>. The C<code> key has a
1754 L<Class::MOP::Method> object as its value.
1756 The list of methods is distinct.
1758 =item B<< $metaclass->find_next_method_by_name($method_name) >>
1760 This method returns the first method in any superclass matching the
1761 given name. It is effectively the method that C<SUPER::$method_name>
1766 =head2 Attribute introspection and creation
1768 Because Perl 5 does not have a core concept of attributes in classes,
1769 we can only return information about attributes which have been added
1770 via this class's methods. We cannot discover information about
1771 attributes which are defined in terms of "regular" Perl 5 methods.
1775 =item B<< $metaclass->get_attribute($attribute_name) >>
1777 This will return a L<Class::MOP::Attribute> for the specified
1778 C<$attribute_name>. If the class does not have the specified
1779 attribute, it returns C<undef>.
1781 NOTE that get_attribute does not search superclasses, for that you
1782 need to use C<find_attribute_by_name>.
1784 =item B<< $metaclass->has_attribute($attribute_name) >>
1786 Returns a boolean indicating whether or not the class defines the
1787 named attribute. It does not include attributes inherited from parent
1790 =item B<< $metaclass->get_attribute_list >>
1792 This will return a list of attributes I<names> for all attributes
1793 defined in this class. Note that this operates on the current class
1794 only, it does not traverse the inheritance hierarchy.
1796 =item B<< $metaclass->get_all_attributes >>
1798 This will traverse the inheritance hierarchy and return a list of all
1799 the L<Class::MOP::Attribute> objects for this class and its parents.
1801 =item B<< $metaclass->find_attribute_by_name($attribute_name) >>
1803 This will return a L<Class::MOP::Attribute> for the specified
1804 C<$attribute_name>. If the class does not have the specified
1805 attribute, it returns C<undef>.
1807 Unlike C<get_attribute>, this attribute I<will> look for the named
1808 attribute in superclasses.
1810 =item B<< $metaclass->add_attribute(...) >>
1812 This method accepts either an existing L<Class::MOP::Attribute>
1813 object or parameters suitable for passing to that class's C<new>
1816 The attribute provided will be added to the class.
1818 Any accessor methods defined by the attribute will be added to the
1819 class when the attribute is added.
1821 If an attribute of the same name already exists, the old attribute
1822 will be removed first.
1824 =item B<< $metaclass->remove_attribute($attribute_name) >>
1826 This will remove the named attribute from the class, and
1827 L<Class::MOP::Attribute> object.
1829 Removing an attribute also removes any accessor methods defined by the
1832 However, note that removing an attribute will only affect I<future>
1833 object instances created for this class, not existing instances.
1835 =item B<< $metaclass->attribute_metaclass >>
1837 Returns the class name of the attribute metaclass for this class. By
1838 default, this is L<Class::MOP::Attribute>.
1842 =head2 Class Immutability
1844 Making a class immutable "freezes" the class definition. You can no
1845 longer call methods which alter the class, such as adding or removing
1846 methods or attributes.
1848 Making a class immutable lets us optimize the class by inlining some
1849 methods, and also allows us to optimize some methods on the metaclass
1852 After immutabilization, the metaclass object will cache most informational
1853 methods that returns information about methods or attributes. Methods which
1854 would alter the class, such as C<add_attribute> and C<add_method>, will
1855 throw an error on an immutable metaclass object.
1857 The immutabilization system in L<Moose> takes much greater advantage
1858 of the inlining features than Class::MOP itself does.
1862 =item B<< $metaclass->make_immutable(%options) >>
1864 This method will create an immutable transformer and use it to make
1865 the class and its metaclass object immutable.
1867 This method accepts the following options:
1871 =item * inline_accessors
1873 =item * inline_constructor
1875 =item * inline_destructor
1877 These are all booleans indicating whether the specified method(s)
1880 By default, accessors and the constructor are inlined, but not the
1883 =item * immutable_trait
1885 The name of a class which will be used as a parent class for the
1886 metaclass object being made immutable. This "trait" implements the
1887 post-immutability functionality of the metaclass (but not the
1888 transformation itself).
1890 This defaults to L<Class::MOP::Class::Immutable::Trait>.
1892 =item * constructor_name
1894 This is the constructor method name. This defaults to "new".
1896 =item * constructor_class
1898 The name of the method metaclass for constructors. It will be used to
1899 generate the inlined constructor. This defaults to
1900 "Class::MOP::Method::Constructor".
1902 =item * replace_constructor
1904 This is a boolean indicating whether an existing constructor should be
1905 replaced when inlining a constructor. This defaults to false.
1907 =item * destructor_class
1909 The name of the method metaclass for destructors. It will be used to
1910 generate the inlined destructor. This defaults to
1911 "Class::MOP::Method::Denstructor".
1913 =item * replace_destructor
1915 This is a boolean indicating whether an existing destructor should be
1916 replaced when inlining a destructor. This defaults to false.
1920 =item B<< $metaclass->immutable_options >>
1922 Returns a hash of the options used when making the class immutable, including
1923 both defaults and anything supplied by the user in the call to C<<
1924 $metaclass->make_immutable >>. This is useful if you need to temporarily make
1925 a class mutable and then restore immutability as it was before.
1927 =item B<< $metaclass->make_mutable >>
1929 Calling this method reverse the immutabilization transformation.
1933 =head2 Method Modifiers
1935 Method modifiers are hooks which allow a method to be wrapped with
1936 I<before>, I<after> and I<around> method modifiers. Every time a
1937 method is called, its modifiers are also called.
1939 A class can modify its own methods, as well as methods defined in
1942 =head3 How method modifiers work?
1944 Method modifiers work by wrapping the original method and then
1945 replacing it in the class's symbol table. The wrappers will handle
1946 calling all the modifiers in the appropriate order and preserving the
1947 calling context for the original method.
1949 The return values of C<before> and C<after> modifiers are
1950 ignored. This is because their purpose is B<not> to filter the input
1951 and output of the primary method (this is done with an I<around>
1954 This may seem like an odd restriction to some, but doing this allows
1955 for simple code to be added at the beginning or end of a method call
1956 without altering the function of the wrapped method or placing any
1957 extra responsibility on the code of the modifier.
1959 Of course if you have more complex needs, you can use the C<around>
1960 modifier which allows you to change both the parameters passed to the
1961 wrapped method, as well as its return value.
1963 Before and around modifiers are called in last-defined-first-called
1964 order, while after modifiers are called in first-defined-first-called
1965 order. So the call tree might looks something like this:
1977 =head3 What is the performance impact?
1979 Of course there is a performance cost associated with method
1980 modifiers, but we have made every effort to make that cost directly
1981 proportional to the number of modifier features you use.
1983 The wrapping method does its best to B<only> do as much work as it
1984 absolutely needs to. In order to do this we have moved some of the
1985 performance costs to set-up time, where they are easier to amortize.
1987 All this said, our benchmarks have indicated the following:
1989 simple wrapper with no modifiers 100% slower
1990 simple wrapper with simple before modifier 400% slower
1991 simple wrapper with simple after modifier 450% slower
1992 simple wrapper with simple around modifier 500-550% slower
1993 simple wrapper with all 3 modifiers 1100% slower
1995 These numbers may seem daunting, but you must remember, every feature
1996 comes with some cost. To put things in perspective, just doing a
1997 simple C<AUTOLOAD> which does nothing but extract the name of the
1998 method called and return it costs about 400% over a normal method
2003 =item B<< $metaclass->add_before_method_modifier($method_name, $code) >>
2005 This wraps the specified method with the supplied subroutine
2006 reference. The modifier will be called as a method itself, and will
2007 receive the same arguments as are passed to the method.
2009 When the modifier exits, the wrapped method will be called.
2011 The return value of the modifier will be ignored.
2013 =item B<< $metaclass->add_after_method_modifier($method_name, $code) >>
2015 This wraps the specified method with the supplied subroutine
2016 reference. The modifier will be called as a method itself, and will
2017 receive the same arguments as are passed to the method.
2019 When the wrapped methods exits, the modifier will be called.
2021 The return value of the modifier will be ignored.
2023 =item B<< $metaclass->add_around_method_modifier($method_name, $code) >>
2025 This wraps the specified method with the supplied subroutine
2028 The first argument passed to the modifier will be a subroutine
2029 reference to the wrapped method. The second argument is the object,
2030 and after that come any arguments passed when the method is called.
2032 The around modifier can choose to call the original method, as well as
2033 what arguments to pass if it does so.
2035 The return value of the modifier is what will be seen by the caller.
2039 =head2 Introspection
2043 =item B<< Class::MOP::Class->meta >>
2045 This will return a L<Class::MOP::Class> instance for this class.
2047 It should also be noted that L<Class::MOP> will actually bootstrap
2048 this module by installing a number of attribute meta-objects into its
2055 Stevan Little E<lt>stevan@iinteractive.comE<gt>
2057 =head1 COPYRIGHT AND LICENSE
2059 Copyright 2006-2010 by Infinity Interactive, Inc.
2061 L<http://www.iinteractive.com>
2063 This library is free software; you can redistribute it and/or modify
2064 it under the same terms as Perl itself.