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.10';
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 $options{weaken} = Class::MOP::metaclass_is_weak($old_metaclass->name)
57 if !exists $options{weaken}
58 && blessed($old_metaclass)
59 && $old_metaclass->isa('Class::MOP::Class');
60 $old_metaclass->_remove_generated_metaobjects
61 if $old_metaclass && $old_metaclass->isa('Class::MOP::Class');
62 my $new_metaclass = $class->SUPER::reinitialize(%options);
63 $new_metaclass->_restore_metaobjects_from($old_metaclass)
64 if $old_metaclass && $old_metaclass->isa('Class::MOP::Class');
65 return $new_metaclass;
68 # NOTE: (meta-circularity)
69 # this is a special form of _construct_instance
70 # (see below), which is used to construct class
71 # meta-object instances for any Class::MOP::*
72 # class. All other classes will use the more
73 # normal &construct_instance.
74 sub _construct_class_instance {
76 my $options = @_ == 1 ? $_[0] : {@_};
77 my $package_name = $options->{package};
78 (defined $package_name && $package_name)
79 || confess "You must pass a package name";
81 # return the metaclass if we have it cached,
82 # and it is still defined (it has not been
83 # reaped by DESTROY yet, which can happen
84 # annoyingly enough during global destruction)
86 if (defined(my $meta = Class::MOP::get_metaclass_by_name($package_name))) {
92 ? $class->_real_ref_name
95 # now create the metaclass
97 if ($class eq 'Class::MOP::Class') {
98 $meta = $class->_new($options);
102 # it is safe to use meta here because
103 # class will always be a subclass of
104 # Class::MOP::Class, which defines meta
105 $meta = $class->meta->_construct_instance($options)
108 # and check the metaclass compatibility
109 $meta->_check_metaclass_compatibility();
111 Class::MOP::store_metaclass_by_name($package_name, $meta);
114 # we need to weaken any anon classes
115 # so that they can call DESTROY properly
116 Class::MOP::weaken_metaclass($package_name) if $options->{weaken};
124 # NOTE: we need to deal with the possibility of class immutability here,
125 # and then get the name of the class appropriately
126 return $self->is_immutable
127 ? $self->_get_mutable_metaclass_name()
134 return Class::MOP::Class->initialize($class)->new_object(@_)
135 if $class ne __PACKAGE__;
137 my $options = @_ == 1 ? $_[0] : {@_};
140 # inherited from Class::MOP::Package
141 'package' => $options->{package},
144 # since the following attributes will
145 # actually be loaded from the symbol
146 # table, and actually bypass the instance
147 # entirely, we can just leave these things
148 # listed here for reference, because they
149 # should not actually have a value associated
151 'namespace' => \undef,
154 # inherited from Class::MOP::Module
156 'authority' => \undef,
158 # defined in Class::MOP::Class
159 'superclasses' => \undef,
162 'attribute_metaclass' =>
163 ( $options->{'attribute_metaclass'} || 'Class::MOP::Attribute' ),
164 'method_metaclass' =>
165 ( $options->{'method_metaclass'} || 'Class::MOP::Method' ),
166 'wrapped_method_metaclass' => (
167 $options->{'wrapped_method_metaclass'}
168 || 'Class::MOP::Method::Wrapped'
170 'instance_metaclass' =>
171 ( $options->{'instance_metaclass'} || 'Class::MOP::Instance' ),
172 'immutable_trait' => (
173 $options->{'immutable_trait'}
174 || 'Class::MOP::Class::Immutable::Trait'
176 'constructor_name' => ( $options->{constructor_name} || 'new' ),
177 'constructor_class' => (
178 $options->{constructor_class} || 'Class::MOP::Method::Constructor'
180 'destructor_class' => $options->{destructor_class},
184 sub reset_package_cache_flag { (shift)->{'_package_cache_flag'} = undef }
185 sub update_package_cache_flag {
188 # we can manually update the cache number
189 # since we are actually adding the method
190 # to our cache as well. This avoids us
191 # having to regenerate the method_map.
193 $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);
196 ## Metaclass compatibility
198 my %base_metaclass = (
199 attribute_metaclass => 'Class::MOP::Attribute',
200 method_metaclass => 'Class::MOP::Method',
201 wrapped_method_metaclass => 'Class::MOP::Method::Wrapped',
202 instance_metaclass => 'Class::MOP::Instance',
203 constructor_class => 'Class::MOP::Method::Constructor',
204 destructor_class => 'Class::MOP::Method::Destructor',
207 sub _base_metaclasses { %base_metaclass }
210 sub _check_metaclass_compatibility {
213 my @superclasses = $self->superclasses
216 $self->_fix_metaclass_incompatibility(@superclasses);
218 my %base_metaclass = $self->_base_metaclasses;
220 # this is always okay ...
222 if ref($self) eq 'Class::MOP::Class'
224 my $meta = $self->$_;
225 !defined($meta) || $meta eq $base_metaclass{$_};
227 keys %base_metaclass;
229 for my $superclass (@superclasses) {
230 $self->_check_class_metaclass_compatibility($superclass);
233 for my $metaclass_type ( keys %base_metaclass ) {
234 next unless defined $self->$metaclass_type;
235 for my $superclass (@superclasses) {
236 $self->_check_single_metaclass_compatibility( $metaclass_type,
242 sub _check_class_metaclass_compatibility {
244 my ( $superclass_name ) = @_;
246 if (!$self->_class_metaclass_is_compatible($superclass_name)) {
247 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
249 my $super_meta_type = $super_meta->_real_ref_name;
251 confess "The metaclass of " . $self->name . " ("
252 . (ref($self)) . ")" . " is not compatible with "
253 . "the metaclass of its superclass, "
254 . $superclass_name . " (" . ($super_meta_type) . ")";
258 sub _class_metaclass_is_compatible {
260 my ( $superclass_name ) = @_;
262 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
265 my $super_meta_name = $super_meta->_real_ref_name;
267 return $self->_is_compatible_with($super_meta_name);
270 sub _check_single_metaclass_compatibility {
272 my ( $metaclass_type, $superclass_name ) = @_;
274 if (!$self->_single_metaclass_is_compatible($metaclass_type, $superclass_name)) {
275 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name);
276 my $metaclass_type_name = $metaclass_type;
277 $metaclass_type_name =~ s/_(?:meta)?class$//;
278 $metaclass_type_name =~ s/_/ /g;
279 confess "The $metaclass_type_name metaclass for "
280 . $self->name . " (" . ($self->$metaclass_type)
281 . ")" . " is not compatible with the "
282 . "$metaclass_type_name metaclass of its "
283 . "superclass, $superclass_name ("
284 . ($super_meta->$metaclass_type) . ")";
288 sub _single_metaclass_is_compatible {
290 my ( $metaclass_type, $superclass_name ) = @_;
292 my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name)
295 # for instance, Moose::Meta::Class has a error_class attribute, but
296 # Class::MOP::Class doesn't - this shouldn't be an error
297 return 1 unless $super_meta->can($metaclass_type);
298 # for instance, Moose::Meta::Class has a destructor_class, but
299 # Class::MOP::Class doesn't - this shouldn't be an error
300 return 1 unless defined $super_meta->$metaclass_type;
301 # if metaclass is defined in superclass but not here, it's not compatible
302 # this is a really odd case
303 return 0 unless defined $self->$metaclass_type;
305 return $self->$metaclass_type->_is_compatible_with($super_meta->$metaclass_type);
308 sub _fix_metaclass_incompatibility {
310 my @supers = map { Class::MOP::Class->initialize($_) } @_;
313 for my $super (@supers) {
315 if $self->_can_fix_metaclass_incompatibility($super);
317 return unless $necessary;
319 for my $super (@supers) {
320 if (!$self->_class_metaclass_is_compatible($super->name)) {
321 $self->_fix_class_metaclass_incompatibility($super);
325 my %base_metaclass = $self->_base_metaclasses;
326 for my $metaclass_type (keys %base_metaclass) {
327 for my $super (@supers) {
328 if (!$self->_single_metaclass_is_compatible($metaclass_type, $super->name)) {
329 $self->_fix_single_metaclass_incompatibility(
330 $metaclass_type, $super
337 sub _can_fix_metaclass_incompatibility {
339 my ($super_meta) = @_;
341 return 1 if $self->_class_metaclass_can_be_made_compatible($super_meta);
343 my %base_metaclass = $self->_base_metaclasses;
344 for my $metaclass_type (keys %base_metaclass) {
345 return 1 if $self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type);
351 sub _class_metaclass_can_be_made_compatible {
353 my ($super_meta) = @_;
355 return $self->_can_be_made_compatible_with($super_meta->_real_ref_name);
358 sub _single_metaclass_can_be_made_compatible {
360 my ($super_meta, $metaclass_type) = @_;
362 my $specific_meta = $self->$metaclass_type;
364 return unless $super_meta->can($metaclass_type);
365 my $super_specific_meta = $super_meta->$metaclass_type;
367 # for instance, Moose::Meta::Class has a destructor_class, but
368 # Class::MOP::Class doesn't - this shouldn't be an error
369 return unless defined $super_specific_meta;
371 # if metaclass is defined in superclass but not here, it's fixable
372 # this is a really odd case
373 return 1 unless defined $specific_meta;
375 return 1 if $specific_meta->_can_be_made_compatible_with($super_specific_meta);
378 sub _fix_class_metaclass_incompatibility {
380 my ( $super_meta ) = @_;
382 if ($self->_class_metaclass_can_be_made_compatible($super_meta)) {
384 || confess "Can't fix metaclass incompatibility for "
386 . " because it is not pristine.";
388 my $super_meta_name = $super_meta->_real_ref_name;
390 $self->_make_compatible_with($super_meta_name);
394 sub _fix_single_metaclass_incompatibility {
396 my ( $metaclass_type, $super_meta ) = @_;
398 if ($self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type)) {
400 || confess "Can't fix metaclass incompatibility for "
402 . " because it is not pristine.";
404 my $new_metaclass = $self->$metaclass_type
405 ? $self->$metaclass_type->_get_compatible_metaclass($super_meta->$metaclass_type)
406 : $super_meta->$metaclass_type;
407 $self->{$metaclass_type} = $new_metaclass;
411 sub _restore_metaobjects_from {
415 $self->_restore_metamethods_from($old_meta);
416 $self->_restore_metaattributes_from($old_meta);
419 sub _remove_generated_metaobjects {
422 for my $attr (map { $self->get_attribute($_) } $self->get_attribute_list) {
423 $attr->remove_accessors;
431 # this should be sufficient, if you have a
432 # use case where it is not, write a test and
434 my $ANON_CLASS_SERIAL = 0;
437 # we need a sufficiently annoying prefix
438 # this should suffice for now, this is
439 # used in a couple of places below, so
440 # need to put it up here for now.
441 my $ANON_CLASS_PREFIX = 'Class::MOP::Class::__ANON__::SERIAL::';
445 no warnings 'uninitialized';
446 $self->name =~ /^$ANON_CLASS_PREFIX/o;
449 sub create_anon_class {
450 my ($class, %options) = @_;
451 $options{weaken} = 1 unless exists $options{weaken};
452 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
453 return $class->create($package_name, %options);
457 # this will only get called for
458 # anon-classes, all other calls
459 # are assumed to occur during
460 # global destruction and so don't
461 # really need to be handled explicitly
465 return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated
467 no warnings 'uninitialized';
468 my $name = $self->name;
469 return unless $name =~ /^$ANON_CLASS_PREFIX/o;
471 # Moose does a weird thing where it replaces the metaclass for
472 # class when fixing metaclass incompatibility. In that case,
473 # we don't want to clean out the namespace now. We can detect
474 # that because Moose will explicitly update the singleton
475 # cache in Class::MOP.
476 my $current_meta = Class::MOP::get_metaclass_by_name($name);
477 return if $current_meta ne $self;
479 my ($serial_id) = ($name =~ /^$ANON_CLASS_PREFIX(\d+)/o);
481 @{$name . '::ISA'} = ();
482 %{$name . '::'} = ();
483 delete ${$ANON_CLASS_PREFIX}{$serial_id . '::'};
485 Class::MOP::remove_metaclass_by_name($name);
490 # creating classes with MOP ...
493 my ( $class, @args ) = @_;
495 unshift @args, 'package' if @args % 2 == 1;
497 my (%options) = @args;
498 my $package_name = $options{package};
500 (ref $options{superclasses} eq 'ARRAY')
501 || confess "You must pass an ARRAY ref of superclasses"
502 if exists $options{superclasses};
504 (ref $options{attributes} eq 'ARRAY')
505 || confess "You must pass an ARRAY ref of attributes"
506 if exists $options{attributes};
508 (ref $options{methods} eq 'HASH')
509 || confess "You must pass a HASH ref of methods"
510 if exists $options{methods};
512 $options{meta_name} = 'meta'
513 unless exists $options{meta_name};
515 my (%initialize_options) = @args;
516 delete @initialize_options{qw(
525 my $meta = $class->initialize( $package_name => %initialize_options );
527 $meta->_instantiate_module( $options{version}, $options{authority} );
529 $meta->_add_meta_method($options{meta_name})
530 if defined $options{meta_name};
532 $meta->superclasses(@{$options{superclasses}})
533 if exists $options{superclasses};
535 # process attributes first, so that they can
536 # install accessors, but locally defined methods
537 # can then overwrite them. It is maybe a little odd, but
538 # I think this should be the order of things.
539 if (exists $options{attributes}) {
540 foreach my $attr (@{$options{attributes}}) {
541 $meta->add_attribute($attr);
544 if (exists $options{methods}) {
545 foreach my $method_name (keys %{$options{methods}}) {
546 $meta->add_method($method_name, $options{methods}->{$method_name});
555 # all these attribute readers will be bootstrapped
556 # away in the Class::MOP bootstrap section
558 sub instance_metaclass { $_[0]->{'instance_metaclass'} }
559 sub immutable_trait { $_[0]->{'immutable_trait'} }
560 sub constructor_class { $_[0]->{'constructor_class'} }
561 sub constructor_name { $_[0]->{'constructor_name'} }
562 sub destructor_class { $_[0]->{'destructor_class'} }
564 # Instance Construction & Cloning
570 # we need to protect the integrity of the
571 # Class::MOP::Class singletons here, so we
572 # delegate this to &construct_class_instance
573 # which will deal with the singletons
574 return $class->_construct_class_instance(@_)
575 if $class->name->isa('Class::MOP::Class');
576 return $class->_construct_instance(@_);
579 sub _construct_instance {
581 my $params = @_ == 1 ? $_[0] : {@_};
582 my $meta_instance = $class->get_meta_instance();
584 # the code below is almost certainly incorrect
585 # but this is foreign inheritance, so we might
586 # have to kludge it in the end.
588 if (my $instance_class = blessed($params->{__INSTANCE__})) {
589 ($instance_class eq $class->name)
590 || confess "Objects passed as the __INSTANCE__ parameter must "
591 . "already be blessed into the correct class, but "
592 . "$params->{__INSTANCE__} is not a " . $class->name;
593 $instance = $params->{__INSTANCE__};
595 elsif (exists $params->{__INSTANCE__}) {
596 confess "The __INSTANCE__ parameter must be a blessed reference, not "
597 . $params->{__INSTANCE__};
600 $instance = $meta_instance->create_instance();
602 foreach my $attr ($class->get_all_attributes()) {
603 $attr->initialize_instance_slot($meta_instance, $instance, $params);
605 if (Class::MOP::metaclass_is_weak($class->name)) {
606 $meta_instance->_set_mop_slot($instance, $class);
612 sub get_meta_instance {
614 $self->{'_meta_instance'} ||= $self->_create_meta_instance();
617 sub _create_meta_instance {
620 my $instance = $self->instance_metaclass->new(
621 associated_metaclass => $self,
622 attributes => [ $self->get_all_attributes() ],
625 $self->add_meta_instance_dependencies()
626 if $instance->is_dependent_on_superclasses();
631 sub inline_create_instance {
634 return $self->get_meta_instance->inline_create_instance(@_);
637 sub inline_rebless_instance {
640 return $self->get_meta_instance->inline_rebless_instance_structure(@_);
645 my $instance = shift;
646 (blessed($instance) && $instance->isa($class->name))
647 || confess "You must pass an instance of the metaclass (" . (ref $class ? $class->name : $class) . "), not ($instance)";
650 # we need to protect the integrity of the
651 # Class::MOP::Class singletons here, they
652 # should not be cloned.
653 return $instance if $instance->isa('Class::MOP::Class');
654 $class->_clone_instance($instance, @_);
657 sub _clone_instance {
658 my ($class, $instance, %params) = @_;
660 || confess "You can only clone instances, ($instance) is not a blessed instance";
661 my $meta_instance = $class->get_meta_instance();
662 my $clone = $meta_instance->clone_instance($instance);
663 foreach my $attr ($class->get_all_attributes()) {
664 if ( defined( my $init_arg = $attr->init_arg ) ) {
665 if (exists $params{$init_arg}) {
666 $attr->set_value($clone, $params{$init_arg});
673 sub _force_rebless_instance {
674 my ($self, $instance, %params) = @_;
675 my $old_metaclass = Class::MOP::class_of($instance);
677 $old_metaclass->rebless_instance_away($instance, $self, %params)
680 my $meta_instance = $self->get_meta_instance;
682 if (Class::MOP::metaclass_is_weak($old_metaclass->name)) {
683 $meta_instance->_clear_mop_slot($instance);
687 # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
688 $meta_instance->rebless_instance_structure($_[1], $self);
690 $self->_fixup_attributes_after_rebless($instance, $old_metaclass, %params);
692 if (Class::MOP::metaclass_is_weak($self->name)) {
693 $meta_instance->_set_mop_slot($instance, $self);
697 sub rebless_instance {
698 my ($self, $instance, %params) = @_;
699 my $old_metaclass = Class::MOP::class_of($instance);
701 my $old_class = $old_metaclass ? $old_metaclass->name : blessed($instance);
702 $self->name->isa($old_class)
703 || confess "You may rebless only into a subclass of ($old_class), of which (". $self->name .") isn't.";
705 $self->_force_rebless_instance($_[1], %params);
710 sub rebless_instance_back {
711 my ( $self, $instance ) = @_;
712 my $old_metaclass = Class::MOP::class_of($instance);
715 = $old_metaclass ? $old_metaclass->name : blessed($instance);
716 $old_class->isa( $self->name )
718 "You may rebless only into a superclass of ($old_class), of which ("
722 $self->_force_rebless_instance($_[1]);
727 sub rebless_instance_away {
728 # this intentionally does nothing, it is just a hook
731 sub _fixup_attributes_after_rebless {
733 my ($instance, $rebless_from, %params) = @_;
734 my $meta_instance = $self->get_meta_instance;
736 for my $attr ( $rebless_from->get_all_attributes ) {
737 next if $self->find_attribute_by_name( $attr->name );
738 $meta_instance->deinitialize_slot( $instance, $_ ) for $attr->slots;
741 foreach my $attr ( $self->get_all_attributes ) {
742 if ( $attr->has_value($instance) ) {
743 if ( defined( my $init_arg = $attr->init_arg ) ) {
744 $params{$init_arg} = $attr->get_value($instance)
745 unless exists $params{$init_arg};
748 $attr->set_value($instance, $attr->get_value($instance));
753 foreach my $attr ($self->get_all_attributes) {
754 $attr->initialize_instance_slot($meta_instance, $instance, \%params);
758 sub _attach_attribute {
759 my ($self, $attribute) = @_;
760 $attribute->attach_to_class($self);
763 sub _post_add_attribute {
764 my ( $self, $attribute ) = @_;
766 $self->invalidate_meta_instances;
768 # invalidate package flag here
771 $attribute->install_accessors;
774 $self->remove_attribute( $attribute->name );
779 sub remove_attribute {
782 my $removed_attribute = $self->SUPER::remove_attribute(@_)
785 $self->invalidate_meta_instances;
787 $removed_attribute->remove_accessors;
788 $removed_attribute->detach_from_class;
790 return$removed_attribute;
793 sub find_attribute_by_name {
794 my ( $self, $attr_name ) = @_;
796 foreach my $class ( $self->linearized_isa ) {
797 # fetch the meta-class ...
798 my $meta = Class::MOP::Class->initialize($class);
799 return $meta->get_attribute($attr_name)
800 if $meta->has_attribute($attr_name);
806 sub get_all_attributes {
808 my %attrs = map { %{ Class::MOP::Class->initialize($_)->_attribute_map } }
809 reverse $self->linearized_isa;
810 return values %attrs;
818 my $isa = $self->get_or_add_package_symbol(
819 { sigil => '@', type => 'ARRAY', name => 'ISA' } );
826 # on 5.8 and below, we need to call
827 # a method to get Perl to detect
828 # a cycle in the class hierarchy
829 my $class = $self->name;
833 # we need to check the metaclass
834 # compatibility here so that we can
835 # be sure that the superclass is
836 # not potentially creating an issues
837 # we don't know about
839 $self->_check_metaclass_compatibility();
840 $self->_superclasses_updated();
846 sub _superclasses_updated {
848 $self->update_meta_instance_dependencies();
849 # keep strong references to all our parents, so they don't disappear if
850 # they are anon classes and don't have any direct instances
851 $self->_superclass_metas(
852 map { Class::MOP::class_of($_) } $self->superclasses
856 sub _superclass_metas {
858 $self->{_superclass_metas} = [@_];
863 my $super_class = $self->name;
865 return @{ $super_class->mro::get_isarev() };
868 sub direct_subclasses {
870 my $super_class = $self->name;
875 } Class::MOP::Class->initialize($_)->superclasses
880 return @{ mro::get_linear_isa( (shift)->name ) };
883 sub class_precedence_list {
885 my $name = $self->name;
887 unless (Class::MOP::IS_RUNNING_ON_5_10()) {
889 # We need to check for circular inheritance here
890 # if we are are not on 5.10, cause 5.8 detects it
891 # late. This will do nothing if all is well, and
892 # blow up otherwise. Yes, it's an ugly hack, better
893 # suggestions are welcome.
895 ($name || return)->isa('This is a test for circular inheritance')
898 # if our mro is c3, we can
899 # just grab the linear_isa
900 if (mro::get_mro($name) eq 'c3') {
901 return @{ mro::get_linear_isa($name) }
905 # we can't grab the linear_isa for dfs
906 # since it has all the duplicates
911 Class::MOP::Class->initialize($_)->class_precedence_list()
912 } $self->superclasses()
920 my $fetch_and_prepare_method = sub {
921 my ($self, $method_name) = @_;
922 my $wrapped_metaclass = $self->wrapped_method_metaclass;
924 my $method = $self->get_method($method_name);
925 # if we dont have local ...
927 # try to find the next method
928 $method = $self->find_next_method_by_name($method_name);
929 # die if it does not exist
931 || confess "The method '$method_name' was not found in the inheritance hierarchy for " . $self->name;
932 # and now make sure to wrap it
933 # even if it is already wrapped
934 # because we need a new sub ref
935 $method = $wrapped_metaclass->wrap($method,
936 package_name => $self->name,
937 name => $method_name,
941 # now make sure we wrap it properly
942 $method = $wrapped_metaclass->wrap($method,
943 package_name => $self->name,
944 name => $method_name,
945 ) unless $method->isa($wrapped_metaclass);
947 $self->add_method($method_name => $method);
951 sub add_before_method_modifier {
952 my ($self, $method_name, $method_modifier) = @_;
953 (defined $method_name && length $method_name)
954 || confess "You must pass in a method name";
955 my $method = $fetch_and_prepare_method->($self, $method_name);
956 $method->add_before_modifier(
957 subname(':before' => $method_modifier)
961 sub add_after_method_modifier {
962 my ($self, $method_name, $method_modifier) = @_;
963 (defined $method_name && length $method_name)
964 || confess "You must pass in a method name";
965 my $method = $fetch_and_prepare_method->($self, $method_name);
966 $method->add_after_modifier(
967 subname(':after' => $method_modifier)
971 sub add_around_method_modifier {
972 my ($self, $method_name, $method_modifier) = @_;
973 (defined $method_name && length $method_name)
974 || confess "You must pass in a method name";
975 my $method = $fetch_and_prepare_method->($self, $method_name);
976 $method->add_around_modifier(
977 subname(':around' => $method_modifier)
982 # the methods above used to be named like this:
983 # ${pkg}::${method}:(before|after|around)
984 # but this proved problematic when using one modifier
985 # to wrap multiple methods (something which is likely
986 # to happen pretty regularly IMO). So instead of naming
987 # it like this, I have chosen to just name them purely
988 # with their modifier names, like so:
989 # :(before|after|around)
990 # The fact is that in a stack trace, it will be fairly
991 # evident from the context what method they are attached
992 # to, and so don't need the fully qualified name.
995 sub find_method_by_name {
996 my ($self, $method_name) = @_;
997 (defined $method_name && length $method_name)
998 || confess "You must define a method name to find";
999 foreach my $class ($self->linearized_isa) {
1000 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
1001 return $method if defined $method;
1006 sub get_all_methods {
1010 for my $class ( reverse $self->linearized_isa ) {
1011 my $meta = Class::MOP::Class->initialize($class);
1013 $methods{ $_->name } = $_ for $meta->_get_local_methods;
1016 return values %methods;
1019 sub get_all_method_names {
1022 return grep { !$uniq{$_}++ } map { Class::MOP::Class->initialize($_)->get_method_list } $self->linearized_isa;
1025 sub find_all_methods_by_name {
1026 my ($self, $method_name) = @_;
1027 (defined $method_name && length $method_name)
1028 || confess "You must define a method name to find";
1030 foreach my $class ($self->linearized_isa) {
1031 # fetch the meta-class ...
1032 my $meta = Class::MOP::Class->initialize($class);
1034 name => $method_name,
1036 code => $meta->get_method($method_name)
1037 } if $meta->has_method($method_name);
1042 sub find_next_method_by_name {
1043 my ($self, $method_name) = @_;
1044 (defined $method_name && length $method_name)
1045 || confess "You must define a method name to find";
1046 my @cpl = $self->linearized_isa;
1047 shift @cpl; # discard ourselves
1048 foreach my $class (@cpl) {
1049 my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
1050 return $method if defined $method;
1055 sub update_meta_instance_dependencies {
1058 if ( $self->{meta_instance_dependencies} ) {
1059 return $self->add_meta_instance_dependencies;
1063 sub add_meta_instance_dependencies {
1066 $self->remove_meta_instance_dependencies;
1068 my @attrs = $self->get_all_attributes();
1071 my @classes = grep { not $seen{ $_->name }++ }
1072 map { $_->associated_class } @attrs;
1074 foreach my $class (@classes) {
1075 $class->add_dependent_meta_instance($self);
1078 $self->{meta_instance_dependencies} = \@classes;
1081 sub remove_meta_instance_dependencies {
1084 if ( my $classes = delete $self->{meta_instance_dependencies} ) {
1085 foreach my $class (@$classes) {
1086 $class->remove_dependent_meta_instance($self);
1096 sub add_dependent_meta_instance {
1097 my ( $self, $metaclass ) = @_;
1098 push @{ $self->{dependent_meta_instances} }, $metaclass;
1101 sub remove_dependent_meta_instance {
1102 my ( $self, $metaclass ) = @_;
1103 my $name = $metaclass->name;
1104 @$_ = grep { $_->name ne $name } @$_
1105 for $self->{dependent_meta_instances};
1108 sub invalidate_meta_instances {
1110 $_->invalidate_meta_instance()
1111 for $self, @{ $self->{dependent_meta_instances} };
1114 sub invalidate_meta_instance {
1116 undef $self->{_meta_instance};
1119 # check if we can reinitialize
1123 # if any local attr is defined
1124 return if $self->get_attribute_list;
1126 # or any non-declared methods
1127 for my $method ( map { $self->get_method($_) } $self->get_method_list ) {
1128 return if $method->isa("Class::MOP::Method::Generated");
1129 # FIXME do we need to enforce this too? return unless $method->isa( $self->method_metaclass );
1137 sub is_mutable { 1 }
1138 sub is_immutable { 0 }
1140 sub immutable_options { %{ $_[0]{__immutable}{options} || {} } }
1142 sub _immutable_options {
1143 my ( $self, @args ) = @_;
1146 inline_accessors => 1,
1147 inline_constructor => 1,
1148 inline_destructor => 0,
1150 immutable_trait => $self->immutable_trait,
1151 constructor_name => $self->constructor_name,
1152 constructor_class => $self->constructor_class,
1153 destructor_class => $self->destructor_class,
1158 sub make_immutable {
1159 my ( $self, @args ) = @_;
1161 if ( $self->is_mutable ) {
1162 $self->_initialize_immutable( $self->_immutable_options(@args) );
1163 $self->_rebless_as_immutable(@args);
1174 if ( $self->is_immutable ) {
1175 my @args = $self->immutable_options;
1176 $self->_rebless_as_mutable();
1177 $self->_remove_inlined_code(@args);
1178 delete $self->{__immutable};
1186 sub _rebless_as_immutable {
1187 my ( $self, @args ) = @_;
1189 $self->{__immutable}{original_class} = ref $self;
1191 bless $self => $self->_immutable_metaclass(@args);
1194 sub _immutable_metaclass {
1195 my ( $self, %args ) = @_;
1197 if ( my $class = $args{immutable_metaclass} ) {
1201 my $trait = $args{immutable_trait} = $self->immutable_trait
1202 || confess "no immutable trait specified for $self";
1204 my $meta = $self->meta;
1205 my $meta_attr = $meta->find_attribute_by_name("immutable_trait");
1209 if ( $meta_attr and $trait eq $meta_attr->default ) {
1210 # if the trait is the same as the default we try and pick a
1211 # predictable name for the immutable metaclass
1212 $class_name = 'Class::MOP::Class::Immutable::' . ref($self);
1215 $class_name = join '::', 'Class::MOP::Class::Immutable::CustomTrait',
1216 $trait, 'ForMetaClass', ref($self);
1220 if Class::MOP::is_class_loaded($class_name);
1222 # If the metaclass is a subclass of CMOP::Class which has had
1223 # metaclass roles applied (via Moose), then we want to make sure
1224 # that we preserve that anonymous class (see Fey::ORM for an
1225 # example of where this matters).
1226 my $meta_name = $meta->_real_ref_name;
1228 my $immutable_meta = $meta_name->create(
1230 superclasses => [ ref $self ],
1233 Class::MOP::MiniTrait::apply( $immutable_meta, $trait );
1235 $immutable_meta->make_immutable(
1236 inline_constructor => 0,
1237 inline_accessors => 0,
1243 sub _remove_inlined_code {
1246 $self->remove_method( $_->name ) for $self->_inlined_methods;
1248 delete $self->{__immutable}{inlined_methods};
1251 sub _inlined_methods { @{ $_[0]{__immutable}{inlined_methods} || [] } }
1253 sub _add_inlined_method {
1254 my ( $self, $method ) = @_;
1256 push @{ $self->{__immutable}{inlined_methods} ||= [] }, $method;
1259 sub _initialize_immutable {
1260 my ( $self, %args ) = @_;
1262 $self->{__immutable}{options} = \%args;
1263 $self->_install_inlined_code(%args);
1266 sub _install_inlined_code {
1267 my ( $self, %args ) = @_;
1270 $self->_inline_accessors(%args) if $args{inline_accessors};
1271 $self->_inline_constructor(%args) if $args{inline_constructor};
1272 $self->_inline_destructor(%args) if $args{inline_destructor};
1275 sub _rebless_as_mutable {
1278 bless $self, $self->_get_mutable_metaclass_name;
1283 sub _inline_accessors {
1286 foreach my $attr_name ( $self->get_attribute_list ) {
1287 $self->get_attribute($attr_name)->install_accessors(1);
1291 sub _inline_constructor {
1292 my ( $self, %args ) = @_;
1294 my $name = $args{constructor_name};
1295 # A class may not even have a constructor, and that's okay.
1296 return unless defined $name;
1298 if ( $self->has_method($name) && !$args{replace_constructor} ) {
1299 my $class = $self->name;
1300 warn "Not inlining a constructor for $class since it defines"
1301 . " its own constructor.\n"
1302 . "If you are certain you don't need to inline your"
1303 . " constructor, specify inline_constructor => 0 in your"
1304 . " call to $class->meta->make_immutable\n";
1308 my $constructor_class = $args{constructor_class};
1310 Class::MOP::load_class($constructor_class);
1312 my $constructor = $constructor_class->new(
1316 package_name => $self->name,
1320 if ( $args{replace_constructor} or $constructor->can_be_inlined ) {
1321 $self->add_method( $name => $constructor );
1322 $self->_add_inlined_method($constructor);
1326 sub _inline_destructor {
1327 my ( $self, %args ) = @_;
1329 ( exists $args{destructor_class} && defined $args{destructor_class} )
1330 || confess "The 'inline_destructor' option is present, but "
1331 . "no destructor class was specified";
1333 if ( $self->has_method('DESTROY') && ! $args{replace_destructor} ) {
1334 my $class = $self->name;
1335 warn "Not inlining a destructor for $class since it defines"
1336 . " its own destructor.\n";
1340 my $destructor_class = $args{destructor_class};
1342 Class::MOP::load_class($destructor_class);
1344 return unless $destructor_class->is_needed($self);
1346 my $destructor = $destructor_class->new(
1349 package_name => $self->name,
1353 if ( $args{replace_destructor} or $destructor->can_be_inlined ) {
1354 $self->add_method( 'DESTROY' => $destructor );
1355 $self->_add_inlined_method($destructor);
1367 Class::MOP::Class - Class Meta Object
1371 # assuming that class Foo
1372 # has been defined, you can
1374 # use this for introspection ...
1376 # add a method to Foo ...
1377 Foo->meta->add_method( 'bar' => sub {...} )
1379 # get a list of all the classes searched
1380 # the method dispatcher in the correct order
1381 Foo->meta->class_precedence_list()
1383 # remove a method from Foo
1384 Foo->meta->remove_method('bar');
1386 # or use this to actually create classes ...
1388 Class::MOP::Class->create(
1391 superclasses => ['Foo'],
1393 Class::MOP::Attribute->new('$bar'),
1394 Class::MOP::Attribute->new('$baz'),
1397 calculate_bar => sub {...},
1398 construct_baz => sub {...}
1405 The Class Protocol is the largest and most complex part of the
1406 Class::MOP meta-object protocol. It controls the introspection and
1407 manipulation of Perl 5 classes, and it can create them as well. The
1408 best way to understand what this module can do is to read the
1409 documentation for each of its methods.
1413 C<Class::MOP::Class> is a subclass of L<Class::MOP::Module>.
1417 =head2 Class construction
1419 These methods all create new C<Class::MOP::Class> objects. These
1420 objects can represent existing classes or they can be used to create
1421 new classes from scratch.
1423 The metaclass object for a given class is a singleton. If you attempt
1424 to create a metaclass for the same class twice, you will just get the
1429 =item B<< Class::MOP::Class->create($package_name, %options) >>
1431 This method creates a new C<Class::MOP::Class> object with the given
1432 package name. It accepts a number of options:
1438 An optional version number for the newly created package.
1442 An optional authority for the newly created package.
1444 =item * superclasses
1446 An optional array reference of superclass names.
1450 An optional hash reference of methods for the class. The keys of the
1451 hash reference are method names and values are subroutine references.
1455 An optional array reference of L<Class::MOP::Attribute> objects.
1459 Specifies the name to install the C<meta> method for this class under.
1460 If it is not passed, C<meta> is assumed, and if C<undef> is explicitly
1461 given, no meta method will be installed.
1465 If true, the metaclass that is stored in the global cache will be a
1468 Classes created in this way are destroyed once the metaclass they are
1469 attached to goes out of scope, and will be removed from Perl's internal
1472 All instances of a class with a weakened metaclass keep a special
1473 reference to the metaclass object, which prevents the metaclass from
1474 going out of scope while any instances exist.
1476 This only works if the instance is based on a hash reference, however.
1480 =item B<< Class::MOP::Class->create_anon_class(%options) >>
1482 This method works just like C<< Class::MOP::Class->create >> but it
1483 creates an "anonymous" class. In fact, the class does have a name, but
1484 that name is a unique name generated internally by this module.
1486 It accepts the same C<superclasses>, C<methods>, and C<attributes>
1487 parameters that C<create> accepts.
1489 Anonymous classes default to C<< weaken => 1 >>, although this can be
1492 =item B<< Class::MOP::Class->initialize($package_name, %options) >>
1494 This method will initialize a C<Class::MOP::Class> object for the
1495 named package. Unlike C<create>, this method I<will not> create a new
1498 The purpose of this method is to retrieve a C<Class::MOP::Class>
1499 object for introspecting an existing class.
1501 If an existing C<Class::MOP::Class> object exists for the named
1502 package, it will be returned, and any options provided will be
1505 If the object does not yet exist, it will be created.
1507 The valid options that can be passed to this method are
1508 C<attribute_metaclass>, C<method_metaclass>,
1509 C<wrapped_method_metaclass>, and C<instance_metaclass>. These are all
1510 optional, and default to the appropriate class in the C<Class::MOP>
1515 =head2 Object instance construction and cloning
1517 These methods are all related to creating and/or cloning object
1522 =item B<< $metaclass->clone_object($instance, %params) >>
1524 This method clones an existing object instance. Any parameters you
1525 provide are will override existing attribute values in the object.
1527 This is a convenience method for cloning an object instance, then
1528 blessing it into the appropriate package.
1530 You could implement a clone method in your class, using this method:
1533 my ($self, %params) = @_;
1534 $self->meta->clone_object($self, %params);
1537 =item B<< $metaclass->rebless_instance($instance, %params) >>
1539 This method changes the class of C<$instance> to the metaclass's class.
1541 You can only rebless an instance into a subclass of its current
1542 class. If you pass any additional parameters, these will be treated
1543 like constructor parameters and used to initialize the object's
1544 attributes. Any existing attributes that are already set will be
1547 Before reblessing the instance, this method will call
1548 C<rebless_instance_away> on the instance's current metaclass. This method
1549 will be passed the instance, the new metaclass, and any parameters
1550 specified to C<rebless_instance>. By default, C<rebless_instance_away>
1551 does nothing; it is merely a hook.
1553 =item B<< $metaclass->rebless_instance_back($instance) >>
1555 Does the same thing as C<rebless_instance>, except that you can only
1556 rebless an instance into one of its superclasses. Any attributes that
1557 do not exist in the superclass will be deinitialized.
1559 This is a much more dangerous operation than C<rebless_instance>,
1560 especially when multiple inheritance is involved, so use this carefully!
1562 =item B<< $metaclass->new_object(%params) >>
1564 This method is used to create a new object of the metaclass's
1565 class. Any parameters you provide are used to initialize the
1566 instance's attributes. A special C<__INSTANCE__> key can be passed to
1567 provide an already generated instance, rather than having Class::MOP
1568 generate it for you. This is mostly useful for using Class::MOP with
1569 foreign classes which generate instances using their own constructors.
1571 =item B<< $metaclass->instance_metaclass >>
1573 Returns the class name of the instance metaclass. See
1574 L<Class::MOP::Instance> for more information on the instance
1577 =item B<< $metaclass->get_meta_instance >>
1579 Returns an instance of the C<instance_metaclass> to be used in the
1580 construction of a new instance of the class.
1582 =item B<< $metaclass->inline_create_instance($class_var) >>
1584 =item B<< $metaclass->inline_rebless_instance($instance_var, $class_var) >>
1586 These methods takes variable names, and use them to create an inline snippet
1587 of code that will create a new instance of the class.
1591 =head2 Informational predicates
1593 These are a few predicate methods for asking information about the
1598 =item B<< $metaclass->is_anon_class >>
1600 This returns true if the class was created by calling C<<
1601 Class::MOP::Class->create_anon_class >>.
1603 =item B<< $metaclass->is_mutable >>
1605 This returns true if the class is still mutable.
1607 =item B<< $metaclass->is_immutable >>
1609 This returns true if the class has been made immutable.
1611 =item B<< $metaclass->is_pristine >>
1613 A class is I<not> pristine if it has non-inherited attributes or if it
1614 has any generated methods.
1618 =head2 Inheritance Relationships
1622 =item B<< $metaclass->superclasses(@superclasses) >>
1624 This is a read-write accessor which represents the superclass
1625 relationships of the metaclass's class.
1627 This is basically sugar around getting and setting C<@ISA>.
1629 =item B<< $metaclass->class_precedence_list >>
1631 This returns a list of all of the class's ancestor classes. The
1632 classes are returned in method dispatch order.
1634 =item B<< $metaclass->linearized_isa >>
1636 This returns a list based on C<class_precedence_list> but with all
1639 =item B<< $metaclass->subclasses >>
1641 This returns a list of all subclasses for this class, even indirect
1644 =item B<< $metaclass->direct_subclasses >>
1646 This returns a list of immediate subclasses for this class, which does not
1647 include indirect subclasses.
1651 =head2 Method introspection and creation
1653 These methods allow you to introspect a class's methods, as well as
1654 add, remove, or change methods.
1656 Determining what is truly a method in a Perl 5 class requires some
1657 heuristics (aka guessing).
1659 Methods defined outside the package with a fully qualified name (C<sub
1660 Package::name { ... }>) will be included. Similarly, methods named
1661 with a fully qualified name using L<Sub::Name> are also included.
1663 However, we attempt to ignore imported functions.
1665 Ultimately, we are using heuristics to determine what truly is a
1666 method in a class, and these heuristics may get the wrong answer in
1667 some edge cases. However, for most "normal" cases the heuristics work
1672 =item B<< $metaclass->get_method($method_name) >>
1674 This will return a L<Class::MOP::Method> for the specified
1675 C<$method_name>. If the class does not have the specified method, it
1678 =item B<< $metaclass->has_method($method_name) >>
1680 Returns a boolean indicating whether or not the class defines the
1681 named method. It does not include methods inherited from parent
1684 =item B<< $metaclass->get_method_list >>
1686 This will return a list of method I<names> for all methods defined in
1689 =item B<< $metaclass->add_method($method_name, $method) >>
1691 This method takes a method name and a subroutine reference, and adds
1692 the method to the class.
1694 The subroutine reference can be a L<Class::MOP::Method>, and you are
1695 strongly encouraged to pass a meta method object instead of a code
1696 reference. If you do so, that object gets stored as part of the
1697 class's method map directly. If not, the meta information will have to
1698 be recreated later, and may be incorrect.
1700 If you provide a method object, this method will clone that object if
1701 the object's package name does not match the class name. This lets us
1702 track the original source of any methods added from other classes
1703 (notably Moose roles).
1705 =item B<< $metaclass->remove_method($method_name) >>
1707 Remove the named method from the class. This method returns the
1708 L<Class::MOP::Method> object for the method.
1710 =item B<< $metaclass->method_metaclass >>
1712 Returns the class name of the method metaclass, see
1713 L<Class::MOP::Method> for more information on the method metaclass.
1715 =item B<< $metaclass->wrapped_method_metaclass >>
1717 Returns the class name of the wrapped method metaclass, see
1718 L<Class::MOP::Method::Wrapped> for more information on the wrapped
1721 =item B<< $metaclass->get_all_methods >>
1723 This will traverse the inheritance hierarchy and return a list of all
1724 the L<Class::MOP::Method> objects for this class and its parents.
1726 =item B<< $metaclass->find_method_by_name($method_name) >>
1728 This will return a L<Class::MOP::Method> for the specified
1729 C<$method_name>. If the class does not have the specified method, it
1732 Unlike C<get_method>, this method I<will> look for the named method in
1735 =item B<< $metaclass->get_all_method_names >>
1737 This will return a list of method I<names> for all of this class's
1738 methods, including inherited methods.
1740 =item B<< $metaclass->find_all_methods_by_name($method_name) >>
1742 This method looks for the named method in the class and all of its
1743 parents. It returns every matching method it finds in the inheritance
1744 tree, so it returns a list of methods.
1746 Each method is returned as a hash reference with three keys. The keys
1747 are C<name>, C<class>, and C<code>. The C<code> key has a
1748 L<Class::MOP::Method> object as its value.
1750 The list of methods is distinct.
1752 =item B<< $metaclass->find_next_method_by_name($method_name) >>
1754 This method returns the first method in any superclass matching the
1755 given name. It is effectively the method that C<SUPER::$method_name>
1760 =head2 Attribute introspection and creation
1762 Because Perl 5 does not have a core concept of attributes in classes,
1763 we can only return information about attributes which have been added
1764 via this class's methods. We cannot discover information about
1765 attributes which are defined in terms of "regular" Perl 5 methods.
1769 =item B<< $metaclass->get_attribute($attribute_name) >>
1771 This will return a L<Class::MOP::Attribute> for the specified
1772 C<$attribute_name>. If the class does not have the specified
1773 attribute, it returns C<undef>.
1775 NOTE that get_attribute does not search superclasses, for that you
1776 need to use C<find_attribute_by_name>.
1778 =item B<< $metaclass->has_attribute($attribute_name) >>
1780 Returns a boolean indicating whether or not the class defines the
1781 named attribute. It does not include attributes inherited from parent
1784 =item B<< $metaclass->get_attribute_list >>
1786 This will return a list of attributes I<names> for all attributes
1787 defined in this class. Note that this operates on the current class
1788 only, it does not traverse the inheritance hierarchy.
1790 =item B<< $metaclass->get_all_attributes >>
1792 This will traverse the inheritance hierarchy and return a list of all
1793 the L<Class::MOP::Attribute> objects for this class and its parents.
1795 =item B<< $metaclass->find_attribute_by_name($attribute_name) >>
1797 This will return a L<Class::MOP::Attribute> for the specified
1798 C<$attribute_name>. If the class does not have the specified
1799 attribute, it returns C<undef>.
1801 Unlike C<get_attribute>, this attribute I<will> look for the named
1802 attribute in superclasses.
1804 =item B<< $metaclass->add_attribute(...) >>
1806 This method accepts either an existing L<Class::MOP::Attribute>
1807 object or parameters suitable for passing to that class's C<new>
1810 The attribute provided will be added to the class.
1812 Any accessor methods defined by the attribute will be added to the
1813 class when the attribute is added.
1815 If an attribute of the same name already exists, the old attribute
1816 will be removed first.
1818 =item B<< $metaclass->remove_attribute($attribute_name) >>
1820 This will remove the named attribute from the class, and
1821 L<Class::MOP::Attribute> object.
1823 Removing an attribute also removes any accessor methods defined by the
1826 However, note that removing an attribute will only affect I<future>
1827 object instances created for this class, not existing instances.
1829 =item B<< $metaclass->attribute_metaclass >>
1831 Returns the class name of the attribute metaclass for this class. By
1832 default, this is L<Class::MOP::Attribute>.
1836 =head2 Class Immutability
1838 Making a class immutable "freezes" the class definition. You can no
1839 longer call methods which alter the class, such as adding or removing
1840 methods or attributes.
1842 Making a class immutable lets us optimize the class by inlining some
1843 methods, and also allows us to optimize some methods on the metaclass
1846 After immutabilization, the metaclass object will cache most informational
1847 methods that returns information about methods or attributes. Methods which
1848 would alter the class, such as C<add_attribute> and C<add_method>, will
1849 throw an error on an immutable metaclass object.
1851 The immutabilization system in L<Moose> takes much greater advantage
1852 of the inlining features than Class::MOP itself does.
1856 =item B<< $metaclass->make_immutable(%options) >>
1858 This method will create an immutable transformer and use it to make
1859 the class and its metaclass object immutable.
1861 This method accepts the following options:
1865 =item * inline_accessors
1867 =item * inline_constructor
1869 =item * inline_destructor
1871 These are all booleans indicating whether the specified method(s)
1874 By default, accessors and the constructor are inlined, but not the
1877 =item * immutable_trait
1879 The name of a class which will be used as a parent class for the
1880 metaclass object being made immutable. This "trait" implements the
1881 post-immutability functionality of the metaclass (but not the
1882 transformation itself).
1884 This defaults to L<Class::MOP::Class::Immutable::Trait>.
1886 =item * constructor_name
1888 This is the constructor method name. This defaults to "new".
1890 =item * constructor_class
1892 The name of the method metaclass for constructors. It will be used to
1893 generate the inlined constructor. This defaults to
1894 "Class::MOP::Method::Constructor".
1896 =item * replace_constructor
1898 This is a boolean indicating whether an existing constructor should be
1899 replaced when inlining a constructor. This defaults to false.
1901 =item * destructor_class
1903 The name of the method metaclass for destructors. It will be used to
1904 generate the inlined destructor. This defaults to
1905 "Class::MOP::Method::Denstructor".
1907 =item * replace_destructor
1909 This is a boolean indicating whether an existing destructor should be
1910 replaced when inlining a destructor. This defaults to false.
1914 =item B<< $metaclass->immutable_options >>
1916 Returns a hash of the options used when making the class immutable, including
1917 both defaults and anything supplied by the user in the call to C<<
1918 $metaclass->make_immutable >>. This is useful if you need to temporarily make
1919 a class mutable and then restore immutability as it was before.
1921 =item B<< $metaclass->make_mutable >>
1923 Calling this method reverse the immutabilization transformation.
1927 =head2 Method Modifiers
1929 Method modifiers are hooks which allow a method to be wrapped with
1930 I<before>, I<after> and I<around> method modifiers. Every time a
1931 method is called, its modifiers are also called.
1933 A class can modify its own methods, as well as methods defined in
1936 =head3 How method modifiers work?
1938 Method modifiers work by wrapping the original method and then
1939 replacing it in the class's symbol table. The wrappers will handle
1940 calling all the modifiers in the appropriate order and preserving the
1941 calling context for the original method.
1943 The return values of C<before> and C<after> modifiers are
1944 ignored. This is because their purpose is B<not> to filter the input
1945 and output of the primary method (this is done with an I<around>
1948 This may seem like an odd restriction to some, but doing this allows
1949 for simple code to be added at the beginning or end of a method call
1950 without altering the function of the wrapped method or placing any
1951 extra responsibility on the code of the modifier.
1953 Of course if you have more complex needs, you can use the C<around>
1954 modifier which allows you to change both the parameters passed to the
1955 wrapped method, as well as its return value.
1957 Before and around modifiers are called in last-defined-first-called
1958 order, while after modifiers are called in first-defined-first-called
1959 order. So the call tree might looks something like this:
1971 =head3 What is the performance impact?
1973 Of course there is a performance cost associated with method
1974 modifiers, but we have made every effort to make that cost directly
1975 proportional to the number of modifier features you use.
1977 The wrapping method does its best to B<only> do as much work as it
1978 absolutely needs to. In order to do this we have moved some of the
1979 performance costs to set-up time, where they are easier to amortize.
1981 All this said, our benchmarks have indicated the following:
1983 simple wrapper with no modifiers 100% slower
1984 simple wrapper with simple before modifier 400% slower
1985 simple wrapper with simple after modifier 450% slower
1986 simple wrapper with simple around modifier 500-550% slower
1987 simple wrapper with all 3 modifiers 1100% slower
1989 These numbers may seem daunting, but you must remember, every feature
1990 comes with some cost. To put things in perspective, just doing a
1991 simple C<AUTOLOAD> which does nothing but extract the name of the
1992 method called and return it costs about 400% over a normal method
1997 =item B<< $metaclass->add_before_method_modifier($method_name, $code) >>
1999 This wraps the specified method with the supplied subroutine
2000 reference. The modifier will be called as a method itself, and will
2001 receive the same arguments as are passed to the method.
2003 When the modifier exits, the wrapped method will be called.
2005 The return value of the modifier will be ignored.
2007 =item B<< $metaclass->add_after_method_modifier($method_name, $code) >>
2009 This wraps the specified method with the supplied subroutine
2010 reference. The modifier will be called as a method itself, and will
2011 receive the same arguments as are passed to the method.
2013 When the wrapped methods exits, the modifier will be called.
2015 The return value of the modifier will be ignored.
2017 =item B<< $metaclass->add_around_method_modifier($method_name, $code) >>
2019 This wraps the specified method with the supplied subroutine
2022 The first argument passed to the modifier will be a subroutine
2023 reference to the wrapped method. The second argument is the object,
2024 and after that come any arguments passed when the method is called.
2026 The around modifier can choose to call the original method, as well as
2027 what arguments to pass if it does so.
2029 The return value of the modifier is what will be seen by the caller.
2033 =head2 Introspection
2037 =item B<< Class::MOP::Class->meta >>
2039 This will return a L<Class::MOP::Class> instance for this class.
2041 It should also be noted that L<Class::MOP> will actually bootstrap
2042 this module by installing a number of attribute meta-objects into its
2049 Stevan Little E<lt>stevan@iinteractive.comE<gt>
2051 =head1 COPYRIGHT AND LICENSE
2053 Copyright 2006-2010 by Infinity Interactive, Inc.
2055 L<http://www.iinteractive.com>
2057 This library is free software; you can redistribute it and/or modify
2058 it under the same terms as Perl itself.