| File | /usr/local/lib/perl/5.10.0/Class/MOP/Class.pm |
| Statements Executed | 25512 |
| Total Time | 0.0878139999999994 seconds |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1415 | 21 | 12 | 9.81ms | 49.2ms | Class::MOP::Class::initialize |
| 323 | 1 | 1 | 4.03ms | 12.4ms | Class::MOP::Class::_single_metaclass_can_be_made_compatible |
| 116 | 5 | 4 | 3.96ms | 12.0ms | Class::MOP::Class::get_all_attributes |
| 347 | 7 | 2 | 2.19ms | 4.64ms | Class::MOP::Class::linearized_isa |
| 108 | 5 | 5 | 2.09ms | 30.3ms | Class::MOP::Class::find_method_by_name |
| 103 | 4 | 3 | 2.09ms | 17.3ms | Class::MOP::Class::find_next_method_by_name |
| 43 | 2 | 1 | 1.89ms | 21.7ms | Class::MOP::Class::_construct_instance |
| 84 | 2 | 1 | 1.89ms | 34.7ms | Class::MOP::Class::_check_metaclass_compatibility |
| 52 | 1 | 1 | 1.62ms | 16.8ms | Class::MOP::Class::_can_fix_metaclass_incompatibility |
| 123 | 1 | 1 | 1.61ms | 65.8ms | Class::MOP::Class::_post_add_attribute |
| 321 | 10 | 5 | 1.58ms | 15.8ms | Class::MOP::Class::get_meta_instance |
| 71 | 1 | 1 | 1.54ms | 34.2ms | Class::MOP::Class::_construct_class_instance |
| 63 | 1 | 1 | 1.34ms | 1.34ms | Class::MOP::Class::_new |
| 123 | 1 | 1 | 1.30ms | 59.5ms | Class::MOP::Class::__ANON__[:768] |
| 58 | 1 | 1 | 1.29ms | 146ms | Class::MOP::Class::_initialize_immutable |
| 58 | 1 | 1 | 1.26ms | 55.2ms | Class::MOP::Class::_immutable_metaclass |
| 77 | 1 | 1 | 1.12ms | 3.80ms | Class::MOP::Class::_single_metaclass_is_compatible |
| 38 | 1 | 1 | 1.10ms | 83.1ms | Class::MOP::Class::_inline_constructor |
| 58 | 1 | 1 | 1.04ms | 144ms | Class::MOP::Class::_install_inlined_code |
| 58 | 13 | 11 | 1.00ms | 205ms | Class::MOP::Class::make_immutable |
| 123 | 1 | 1 | 927µs | 1.50ms | Class::MOP::Class::invalidate_meta_instances |
| 58 | 2 | 2 | 827µs | 2.02ms | Class::MOP::Class::_immutable_options |
| 46 | 1 | 1 | 806µs | 21.0ms | Class::MOP::Class::__ANON__[:935] |
| 58 | 1 | 1 | 779µs | 3.79ms | Class::MOP::Class::find_attribute_by_name |
| 58 | 1 | 1 | 777µs | 56.0ms | Class::MOP::Class::_rebless_as_immutable |
| 114 | 2 | 1 | 749µs | 1.04ms | Class::MOP::Class::is_anon_class |
| 44 | 1 | 1 | 693µs | 18.4ms | Class::MOP::Class::_fix_metaclass_incompatibility |
| 123 | 1 | 1 | 664µs | 2.51ms | Class::MOP::Class::_attach_attribute |
| 58 | 1 | 1 | 661µs | 14.2ms | Class::MOP::Class::_create_meta_instance |
| 96 | 3 | 2 | 631µs | 631µs | Class::MOP::Class::_base_metaclasses |
| 123 | 1 | 1 | 576µs | 576µs | Class::MOP::Class::invalidate_meta_instance |
| 77 | 1 | 1 | 494µs | 4.30ms | Class::MOP::Class::_check_single_metaclass_compatibility |
| 32 | 1 | 1 | 477µs | 51.3ms | Class::MOP::Class::_inline_accessors |
| 73 | 4 | 1 | 476µs | 1.19ms | Class::MOP::Class::_real_ref_name |
| 45 | 2 | 1 | 411µs | 411µs | Class::MOP::Class::_add_inlined_method |
| 46 | 1 | 1 | 394µs | 23.4ms | Class::MOP::Class::add_around_method_modifier |
| 52 | 1 | 1 | 376µs | 1.96ms | Class::MOP::Class::_class_metaclass_can_be_made_compatible |
| 14 | 2 | 2 | 347µs | 3.22ms | Class::MOP::Class::find_all_methods_by_name |
| 35 | 4 | 4 | 339µs | 13.6ms | Class::MOP::Class::new_object |
| 89 | 3 | 3 | 303µs | 303µs | Class::MOP::Class::is_mutable |
| 8 | 1 | 1 | 296µs | 7.14ms | Class::MOP::Class::get_all_methods |
| 38 | 2 | 2 | 272µs | 2.85ms | Class::MOP::Class::inline_create_instance |
| 7 | 1 | 1 | 248µs | 8.92ms | Class::MOP::Class::_inline_destructor |
| 45 | 1 | 1 | 141µs | 141µs | Class::MOP::Class::is_immutable |
| 11 | 1 | 1 | 129µs | 455µs | Class::MOP::Class::_class_metaclass_is_compatible |
| 11 | 1 | 1 | 106µs | 561µs | Class::MOP::Class::_check_class_metaclass_compatibility |
| 2 | 1 | 1 | 90µs | 3.13ms | Class::MOP::Class::create |
| 13 | 1 | 1 | 85µs | 150µs | Class::MOP::Class::_superclasses_updated |
| 13 | 1 | 1 | 65µs | 65µs | Class::MOP::Class::update_meta_instance_dependencies |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::BEGIN |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::DESTROY |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::__ANON__[:222] |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::__ANON__[:772] |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::_clone_instance |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::_fix_class_metaclass_incompatibility |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::_fix_single_metaclass_incompatibility |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::_fixup_attributes_after_rebless |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::_force_rebless_instance |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::_inlined_methods |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::_rebless_as_mutable |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::_remove_generated_metaobjects |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::_remove_inlined_code |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::_restore_metaobjects_from |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::add_after_method_modifier |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::add_before_method_modifier |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::add_dependent_meta_instance |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::add_meta_instance_dependencies |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::class_precedence_list |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::clone_object |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::create_anon_class |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::direct_subclasses |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::get_all_method_names |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::immutable_options |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::inline_rebless_instance |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::is_pristine |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::make_mutable |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::rebless_instance |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::rebless_instance_away |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::rebless_instance_back |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::reinitialize |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::remove_attribute |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::remove_dependent_meta_instance |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::remove_meta_instance_dependencies |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::reset_package_cache_flag |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::subclasses |
| 0 | 0 | 0 | 0s | 0s | Class::MOP::Class::update_package_cache_flag |
| Line | Stmts. | Exclusive Time | Avg. | Code |
|---|---|---|---|---|
| 1 | ||||
| 2 | package Class::MOP::Class; | |||
| 3 | ||||
| 4 | 3 | 30µs | 10µs | use strict; # spent 14µs making 1 call to strict::import |
| 5 | 3 | 28µs | 9µs | use warnings; # spent 26µs making 1 call to warnings::import |
| 6 | ||||
| 7 | 3 | 102µs | 34µs | use Class::MOP::Instance; # spent 5µs making 1 call to import |
| 8 | 3 | 116µs | 39µs | use Class::MOP::Method::Wrapped; # spent 7µs making 1 call to import |
| 9 | 3 | 122µs | 40µs | use Class::MOP::Method::Accessor; # spent 4µs making 1 call to import |
| 10 | 3 | 136µs | 45µs | use Class::MOP::Method::Constructor; # spent 9µs making 1 call to import |
| 11 | 3 | 115µs | 38µs | use Class::MOP::MiniTrait; # spent 8µs making 1 call to import |
| 12 | ||||
| 13 | 3 | 38µs | 13µs | use Carp 'confess'; # spent 54µs making 1 call to Exporter::import |
| 14 | 3 | 29µs | 10µs | use Scalar::Util 'blessed', 'reftype', 'weaken'; # spent 51µs making 1 call to Exporter::import |
| 15 | 3 | 30µs | 10µs | use Sub::Name 'subname'; # spent 34µs making 1 call to Exporter::import |
| 16 | 3 | 112µs | 37µs | use Devel::GlobalDestruction 'in_global_destruction'; # spent 244µs making 1 call to Sub::Exporter::__ANON__[/usr/local/share/perl/5.10.0/Sub/Exporter.pm:756] |
| 17 | 3 | 41µs | 14µs | use Try::Tiny; # spent 51µs making 1 call to Exporter::import |
| 18 | 3 | 196µs | 65µs | use List::MoreUtils 'all'; # spent 104µs making 1 call to Exporter::import |
| 19 | ||||
| 20 | 1 | 1µs | 1µs | our $VERSION = '1.09'; |
| 21 | 1 | 31µs | 31µs | $VERSION = eval $VERSION; |
| 22 | 1 | 600ns | 600ns | our $AUTHORITY = 'cpan:STEVAN'; |
| 23 | ||||
| 24 | use base 'Class::MOP::Module', # spent 4.37ms making 1 call to base::import | |||
| 25 | 'Class::MOP::Mixin::HasAttributes', | |||
| 26 | 3 | 2.02ms | 675µs | 'Class::MOP::Mixin::HasMethods'; |
| 27 | ||||
| 28 | # Creation | |||
| 29 | ||||
| 30 | # spent 49.2ms (9.81+39.4) within Class::MOP::Class::initialize which was called 1415 times, avg 35µs/call:
# 422 times (2.57ms+1.64ms) by Class::MOP::Class::get_all_attributes at line 804, avg 10µs/call
# 301 times (1.92ms+1.19ms) by Class::MOP::Class::find_method_by_name at line 986, avg 10µs/call
# 210 times (1.65ms+5.98ms) by Class::MOP::Method::Meta::_generate_meta_method or Class::MOP::Method::Meta::__ANON__[/usr/local/lib/perl/5.10.0/Class/MOP/Method/Meta.pm:45] at line 44 of /usr/local/lib/perl/5.10.0/Class/MOP/Method/Meta.pm, avg 36µs/call
# 175 times (1.19ms+629µs) by Class::MOP::Class::find_next_method_by_name at line 1035, avg 10µs/call
# 58 times (439µs+199µs) by Class::MOP::Class::find_attribute_by_name at line 794, avg 11µs/call
# 52 times (361µs+-198µs) by Class::MOP::Class::_fix_metaclass_incompatibility at line 306, avg 3µs/call
# 36 times (258µs+128µs) by Class::MOP::Class::find_all_methods_by_name at line 1018, avg 11µs/call
# 33 times (218µs+6.10ms) at line 18 of /usr/local/lib/perl/5.10.0/Class/MOP/Object.pm, avg 191µs/call
# 26 times (215µs+2.32ms) by Class::MOP::Method::_new at line 54 of /usr/local/lib/perl/5.10.0/Class/MOP/Method.pm, avg 98µs/call
# 19 times (236µs+3.24ms) by metaclass::import at line 45 of /usr/local/lib/perl/5.10.0/metaclass.pm, avg 183µs/call
# 18 times (128µs+2.01ms) at line 14 of /usr/local/lib/perl/5.10.0/Class/MOP/Mixin.pm, avg 119µs/call
# 14 times (156µs+4.20ms) by Class::MOP::MiniTrait::apply at line 15 of /usr/local/lib/perl/5.10.0/Class/MOP/MiniTrait.pm, avg 311µs/call
# 9 times (80µs+39µs) by Class::MOP::Class::get_all_methods at line 997, avg 13µs/call
# 8 times (102µs+10.4ms) by Moose::Meta::Class::initialize at line 68 of /usr/local/lib/perl/5.10.0/Moose/Meta/Class.pm, avg 1.31ms/call
# 8 times (49µs+27µs) by Class::MOP::Attribute::get_raw_value at line 274 of /usr/local/lib/perl/5.10.0/Class/MOP/Attribute.pm, avg 9µs/call
# 7 times (65µs+1.06ms) at line 11 of /usr/local/lib/perl/5.10.0/Moose/Meta/Attribute/Native.pm, avg 161µs/call
# 7 times (70µs+35µs) by namespace::autoclean::import or namespace::autoclean::__ANON__[/usr/local/share/perl/5.10.0/namespace/autoclean.pm:57] at line 44 of /usr/local/share/perl/5.10.0/namespace/autoclean.pm, avg 15µs/call
# 7 times (52µs+39µs) by Class::MOP::Method::Generated::__ANON__[(eval 0)[/usr/local/lib/perl/5.10.0/Class/MOP/Method/Generated.pm:53]:24] at line 4 of (eval 0)[/usr/local/lib/perl/5.10.0/Class/MOP/Method/Generated.pm:53] at line 53 of /usr/local/lib/perl/5.10.0/Class/MOP/Method/Generated.pm, avg 13µs/call
# 2 times (24µs+359µs) by Class::MOP::Class::create at line 520, avg 192µs/call
# 2 times (20µs+8µs) by Class::MOP::Attribute::has_value at line 282 of /usr/local/lib/perl/5.10.0/Class/MOP/Attribute.pm, avg 14µs/call
# once (8µs+4µs) by Class::MOP::Attribute::_new at line 62 of /usr/local/lib/perl/5.10.0/Class/MOP/Attribute.pm | |||
| 31 | 7075 | 10.00ms | 1µs | my $class = shift; |
| 32 | ||||
| 33 | my $package_name; | |||
| 34 | ||||
| 35 | if ( @_ % 2 ) { | |||
| 36 | $package_name = shift; | |||
| 37 | } else { | |||
| 38 | my %options = @_; | |||
| 39 | $package_name = $options{package}; | |||
| 40 | } | |||
| 41 | ||||
| 42 | ($package_name && !ref($package_name)) | |||
| 43 | || confess "You must pass a package name and it cannot be blessed"; | |||
| 44 | ||||
| 45 | return Class::MOP::get_metaclass_by_name($package_name) # spent 34.5ms making 71 calls to Class::MOP::Class::_construct_class_instance, avg 481µs/call, max recursion depth 1
# spent 5.99ms making 1415 calls to Class::MOP::get_metaclass_by_name, avg 4µs/call | |||
| 46 | || $class->_construct_class_instance(package => $package_name, @_); | |||
| 47 | } | |||
| 48 | ||||
| 49 | sub reinitialize { | |||
| 50 | my ( $class, @args ) = @_; | |||
| 51 | unshift @args, "package" if @args % 2; | |||
| 52 | my %options = @args; | |||
| 53 | my $old_metaclass = blessed($options{package}) | |||
| 54 | ? $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; | |||
| 62 | } | |||
| 63 | ||||
| 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 | # spent 34.2ms (1.54+32.6) within Class::MOP::Class::_construct_class_instance which was called 71 times, avg 481µs/call:
# 71 times (1.54ms+32.6ms) by Class::MOP::Class::initialize at line 45, avg 481µs/call | |||
| 71 | 860 | 2.25ms | 3µs | my $class = shift; |
| 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"; | |||
| 76 | # NOTE: | |||
| 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) | |||
| 81 | ||||
| 82 | if (defined(my $meta = Class::MOP::get_metaclass_by_name($package_name))) { # spent 240µs making 71 calls to Class::MOP::get_metaclass_by_name, avg 3µs/call | |||
| 83 | return $meta; | |||
| 84 | } | |||
| 85 | ||||
| 86 | $class | |||
| 87 | = ref $class # spent 206µs making 8 calls to Class::MOP::Class::_real_ref_name, avg 26µs/call | |||
| 88 | ? $class->_real_ref_name | |||
| 89 | : $class; | |||
| 90 | ||||
| 91 | # now create the metaclass | |||
| 92 | my $meta; | |||
| 93 | if ($class eq 'Class::MOP::Class') { # spent 1.34ms making 63 calls to Class::MOP::Class::_new, avg 21µs/call | |||
| 94 | $meta = $class->_new($options); | |||
| 95 | } | |||
| 96 | else { | |||
| 97 | # NOTE: | |||
| 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) # spent 8.80ms making 8 calls to Class::MOP::Class::_construct_instance, avg 1.10ms/call
# spent 174µs making 8 calls to Class::MOP::Object::meta, avg 22µs/call | |||
| 102 | } | |||
| 103 | ||||
| 104 | # and check the metaclass compatibility | |||
| 105 | $meta->_check_metaclass_compatibility(); # spent 21.1ms making 71 calls to Class::MOP::Class::_check_metaclass_compatibility, avg 294µs/call, max recursion depth 1 | |||
| 106 | ||||
| 107 | Class::MOP::store_metaclass_by_name($package_name, $meta); # spent 438µs making 71 calls to Class::MOP::store_metaclass_by_name, avg 6µs/call | |||
| 108 | ||||
| 109 | # NOTE: | |||
| 110 | # we need to weaken any anon classes | |||
| 111 | # so that they can call DESTROY properly | |||
| 112 | Class::MOP::weaken_metaclass($package_name) if $meta->is_anon_class; # spent 669µs making 71 calls to Class::MOP::Class::is_anon_class, avg 9µs/call | |||
| 113 | ||||
| 114 | $meta; | |||
| 115 | } | |||
| 116 | ||||
| 117 | # spent 1.19ms (476µs+714µs) within Class::MOP::Class::_real_ref_name which was called 73 times, avg 16µs/call:
# 52 times (329µs+456µs) by Class::MOP::Class::_class_metaclass_can_be_made_compatible at line 351, avg 15µs/call
# 11 times (75µs+85µs) by Class::MOP::Class::_class_metaclass_is_compatible at line 261, avg 15µs/call
# 8 times (57µs+149µs) by Class::MOP::Class::_construct_class_instance at line 87, avg 26µs/call
# 2 times (16µs+24µs) by Class::MOP::Class::_immutable_metaclass at line 1212, avg 20µs/call | |||
| 118 | 146 | 568µs | 4µs | my $self = shift; |
| 119 | ||||
| 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 # spent 282µs making 20 calls to Class::MOP::Class::Immutable::Class::MOP::Class::is_immutable, avg 14µs/call
# spent 176µs making 28 calls to Class::MOP::Class::Immutable::Trait::_get_mutable_metaclass_name, avg 6µs/call
# spent 141µs making 45 calls to Class::MOP::Class::is_immutable, avg 3µs/call
# spent 116µs making 8 calls to Class::MOP::Class::Immutable::Moose::Meta::Class::is_immutable, avg 15µs/call | |||
| 123 | ? $self->_get_mutable_metaclass_name() | |||
| 124 | : ref $self; | |||
| 125 | } | |||
| 126 | ||||
| 127 | # spent 1.34ms within Class::MOP::Class::_new which was called 63 times, avg 21µs/call:
# 63 times (1.34ms+0s) by Class::MOP::Class::_construct_class_instance at line 93, avg 21µs/call | |||
| 128 | 252 | 1.22ms | 5µs | my $class = shift; |
| 129 | ||||
| 130 | return Class::MOP::Class->initialize($class)->new_object(@_) | |||
| 131 | if $class ne __PACKAGE__; | |||
| 132 | ||||
| 133 | my $options = @_ == 1 ? $_[0] : {@_}; | |||
| 134 | ||||
| 135 | return bless { | |||
| 136 | # inherited from Class::MOP::Package | |||
| 137 | 'package' => $options->{package}, | |||
| 138 | ||||
| 139 | # NOTE: | |||
| 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 | |||
| 146 | # with the slot. | |||
| 147 | 'namespace' => \undef, | |||
| 148 | 'methods' => {}, | |||
| 149 | ||||
| 150 | # inherited from Class::MOP::Module | |||
| 151 | 'version' => \undef, | |||
| 152 | 'authority' => \undef, | |||
| 153 | ||||
| 154 | # defined in Class::MOP::Class | |||
| 155 | 'superclasses' => \undef, | |||
| 156 | ||||
| 157 | 'attributes' => {}, | |||
| 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' | |||
| 165 | ), | |||
| 166 | 'instance_metaclass' => | |||
| 167 | ( $options->{'instance_metaclass'} || 'Class::MOP::Instance' ), | |||
| 168 | 'immutable_trait' => ( | |||
| 169 | $options->{'immutable_trait'} | |||
| 170 | || 'Class::MOP::Class::Immutable::Trait' | |||
| 171 | ), | |||
| 172 | 'constructor_name' => ( $options->{constructor_name} || 'new' ), | |||
| 173 | 'constructor_class' => ( | |||
| 174 | $options->{constructor_class} || 'Class::MOP::Method::Constructor' | |||
| 175 | ), | |||
| 176 | 'destructor_class' => $options->{destructor_class}, | |||
| 177 | }, $class; | |||
| 178 | } | |||
| 179 | ||||
| 180 | sub reset_package_cache_flag { (shift)->{'_package_cache_flag'} = undef } | |||
| 181 | sub update_package_cache_flag { | |||
| 182 | my $self = shift; | |||
| 183 | # NOTE: | |||
| 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. | |||
| 188 | # - SL | |||
| 189 | $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name); | |||
| 190 | } | |||
| 191 | ||||
| 192 | ## Metaclass compatibility | |||
| 193 | { | |||
| 194 | 2 | 6µs | 3µs | 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', | |||
| 201 | ); | |||
| 202 | ||||
| 203 | 96 | 430µs | 4µs | # spent 631µs within Class::MOP::Class::_base_metaclasses which was called 96 times, avg 7µs/call:
# 41 times (296µs+0s) by Class::MOP::Class::_can_fix_metaclass_incompatibility at line 339, avg 7µs/call
# 33 times (184µs+0s) by Class::MOP::Class::_check_metaclass_compatibility at line 214, avg 6µs/call
# 22 times (151µs+0s) by Moose::Meta::Class::_base_metaclasses at line 348 of /usr/local/lib/perl/5.10.0/Moose/Meta/Class.pm, avg 7µs/call |
| 204 | } | |||
| 205 | ||||
| 206 | sub _check_metaclass_compatibility { | |||
| 207 | 564 | 2.73ms | 5µs | my $self = shift; |
| 208 | ||||
| 209 | my @superclasses = $self->superclasses # spent 5.31ms making 65 calls to Class::MOP::Class::superclasses, avg 80µs/call, max recursion depth 1
# spent 1.69ms making 19 calls to Moose::Meta::Class::superclasses, avg 39µs/call, max recursion depth 1 | |||
| 210 | or return; | |||
| 211 | ||||
| 212 | $self->_fix_metaclass_incompatibility(@superclasses); # spent 18.4ms making 44 calls to Class::MOP::Class::_fix_metaclass_incompatibility, avg 418µs/call | |||
| 213 | ||||
| 214 | my %base_metaclass = $self->_base_metaclasses; # spent 456µs making 11 calls to Moose::Meta::Class::_base_metaclasses, avg 41µs/call
# spent 184µs making 33 calls to Class::MOP::Class::_base_metaclasses, avg 6µs/call | |||
| 215 | ||||
| 216 | # this is always okay ... | |||
| 217 | return | |||
| 218 | if ref($self) eq 'Class::MOP::Class' | |||
| 219 | && all { | |||
| 220 | 396 | 890µs | 2µs | my $meta = $self->$_; # spent 151µs making 33 calls to Class::MOP::Mixin::HasMethods::method_metaclass, avg 5µs/call
# spent 140µs making 33 calls to Class::MOP::Mixin::HasAttributes::attribute_metaclass, avg 4µs/call
# spent 140µs making 33 calls to Class::MOP::Mixin::HasMethods::wrapped_method_metaclass, avg 4µs/call
# spent 135µs making 33 calls to Class::MOP::Class::constructor_class, avg 4µs/call
# spent 117µs making 33 calls to Class::MOP::Class::destructor_class, avg 4µs/call
# spent 114µs making 33 calls to Class::MOP::Class::instance_metaclass, avg 3µs/call |
| 221 | 1 | 253µs | 253µs | !defined($meta) || $meta eq $base_metaclass{$_}; |
| 222 | } | |||
| 223 | keys %base_metaclass; # spent 1.57ms making 33 calls to List::MoreUtils::all, avg 48µs/call | |||
| 224 | ||||
| 225 | for my $superclass (@superclasses) { | |||
| 226 | $self->_check_class_metaclass_compatibility($superclass); # spent 561µs making 11 calls to Class::MOP::Class::_check_class_metaclass_compatibility, avg 51µs/call | |||
| 227 | } | |||
| 228 | ||||
| 229 | for my $metaclass_type ( keys %base_metaclass ) { | |||
| 230 | next unless defined $self->$metaclass_type; # spent 97µs making 11 calls to Moose::Meta::Class::constructor_class, avg 9µs/call
# spent 95µs making 11 calls to Moose::Meta::Class::destructor_class, avg 9µs/call
# spent 91µs making 11 calls to Class::MOP::Mixin::HasAttributes::attribute_metaclass, avg 8µs/call
# spent 86µs making 11 calls to Class::MOP::Class::instance_metaclass, avg 8µs/call
# spent 84µs making 11 calls to Class::MOP::Mixin::HasMethods::method_metaclass, avg 8µs/call
# spent 57µs making 11 calls to Class::MOP::Mixin::HasMethods::wrapped_method_metaclass, avg 5µs/call
# spent 54µs making 11 calls to Moose::Meta::Class::error_class, avg 5µs/call | |||
| 231 | for my $superclass (@superclasses) { | |||
| 232 | $self->_check_single_metaclass_compatibility( $metaclass_type, # spent 4.30ms making 77 calls to Class::MOP::Class::_check_single_metaclass_compatibility, avg 56µs/call | |||
| 233 | $superclass ); | |||
| 234 | } | |||
| 235 | } | |||
| 236 | } | |||
| 237 | ||||
| 238 | # spent 561µs (106+455) within Class::MOP::Class::_check_class_metaclass_compatibility which was called 11 times, avg 51µs/call:
# 11 times (106µs+455µs) by Class::MOP::Class::_check_metaclass_compatibility at line 226, avg 51µs/call | |||
| 239 | 33 | 96µs | 3µs | my $self = shift; |
| 240 | my ( $superclass_name ) = @_; | |||
| 241 | ||||
| 242 | if (!$self->_class_metaclass_is_compatible($superclass_name)) { # spent 455µs making 11 calls to Class::MOP::Class::_class_metaclass_is_compatible, avg 41µs/call | |||
| 243 | my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name); | |||
| 244 | ||||
| 245 | my $super_meta_type = $super_meta->_real_ref_name; | |||
| 246 | ||||
| 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) . ")"; | |||
| 251 | } | |||
| 252 | } | |||
| 253 | ||||
| 254 | # spent 455µs (129+326) within Class::MOP::Class::_class_metaclass_is_compatible which was called 11 times, avg 41µs/call:
# 11 times (129µs+326µs) by Class::MOP::Class::_check_class_metaclass_compatibility at line 242, avg 41µs/call | |||
| 255 | 55 | 174µs | 3µs | my $self = shift; |
| 256 | my ( $superclass_name ) = @_; | |||
| 257 | ||||
| 258 | my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name) # spent 44µs making 11 calls to Class::MOP::get_metaclass_by_name, avg 4µs/call | |||
| 259 | || return 1; | |||
| 260 | ||||
| 261 | my $super_meta_name = $super_meta->_real_ref_name; # spent 160µs making 11 calls to Class::MOP::Class::_real_ref_name, avg 15µs/call | |||
| 262 | ||||
| 263 | return $self->_is_compatible_with($super_meta_name); # spent 122µs making 11 calls to Class::MOP::Object::_is_compatible_with, avg 11µs/call | |||
| 264 | } | |||
| 265 | ||||
| 266 | # spent 4.30ms (494µs+3.80) within Class::MOP::Class::_check_single_metaclass_compatibility which was called 77 times, avg 56µs/call:
# 77 times (494µs+3.80ms) by Class::MOP::Class::_check_metaclass_compatibility at line 232, avg 56µs/call | |||
| 267 | 231 | 505µs | 2µs | my $self = shift; |
| 268 | my ( $metaclass_type, $superclass_name ) = @_; | |||
| 269 | ||||
| 270 | if (!$self->_single_metaclass_is_compatible($metaclass_type, $superclass_name)) { # spent 3.80ms making 77 calls to Class::MOP::Class::_single_metaclass_is_compatible, avg 49µs/call | |||
| 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) . ")"; | |||
| 281 | } | |||
| 282 | } | |||
| 283 | ||||
| 284 | # spent 3.80ms (1.12+2.68) within Class::MOP::Class::_single_metaclass_is_compatible which was called 77 times, avg 49µs/call:
# 77 times (1.12ms+2.68ms) by Class::MOP::Class::_check_single_metaclass_compatibility at line 270, avg 49µs/call | |||
| 285 | 539 | 2.34ms | 4µs | my $self = shift; |
| 286 | my ( $metaclass_type, $superclass_name ) = @_; | |||
| 287 | ||||
| 288 | my $super_meta = Class::MOP::get_metaclass_by_name($superclass_name) # spent 268µs making 77 calls to Class::MOP::get_metaclass_by_name, avg 3µs/call | |||
| 289 | || return 1; | |||
| 290 | ||||
| 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); # spent 189µs making 77 calls to UNIVERSAL::can, avg 2µs/call | |||
| 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; # spent 93µs making 11 calls to Class::MOP::Mixin::HasMethods::method_metaclass, avg 8µs/call
# spent 85µs making 11 calls to Class::MOP::Class::instance_metaclass, avg 8µs/call
# spent 76µs making 11 calls to Class::MOP::Mixin::HasMethods::wrapped_method_metaclass, avg 7µs/call
# spent 75µs making 11 calls to Moose::Meta::Class::destructor_class, avg 7µs/call
# spent 74µs making 11 calls to Class::MOP::Mixin::HasAttributes::attribute_metaclass, avg 7µs/call
# spent 70µs making 11 calls to Moose::Meta::Class::constructor_class, avg 6µs/call
# spent 45µs making 11 calls to Moose::Meta::Class::error_class, avg 4µs/call | |||
| 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; # spent 69µs making 11 calls to Moose::Meta::Class::destructor_class, avg 6µs/call
# spent 65µs making 11 calls to Moose::Meta::Class::constructor_class, avg 6µs/call
# spent 48µs making 11 calls to Class::MOP::Mixin::HasMethods::method_metaclass, avg 4µs/call
# spent 47µs making 11 calls to Class::MOP::Mixin::HasAttributes::attribute_metaclass, avg 4µs/call
# spent 44µs making 11 calls to Class::MOP::Mixin::HasMethods::wrapped_method_metaclass, avg 4µs/call
# spent 42µs making 11 calls to Moose::Meta::Class::error_class, avg 4µs/call
# spent 39µs making 11 calls to Class::MOP::Class::instance_metaclass, avg 4µs/call | |||
| 300 | ||||
| 301 | return $self->$metaclass_type->_is_compatible_with($super_meta->$metaclass_type); # spent 730µs making 77 calls to Class::MOP::Object::_is_compatible_with, avg 9µs/call
# spent 106µs making 22 calls to Class::MOP::Mixin::HasAttributes::attribute_metaclass, avg 5µs/call
# spent 98µs making 22 calls to Moose::Meta::Class::constructor_class, avg 4µs/call
# spent 87µs making 22 calls to Moose::Meta::Class::destructor_class, avg 4µs/call
# spent 86µs making 22 calls to Class::MOP::Mixin::HasMethods::wrapped_method_metaclass, avg 4µs/call
# spent 82µs making 22 calls to Class::MOP::Mixin::HasMethods::method_metaclass, avg 4µs/call
# spent 82µs making 22 calls to Moose::Meta::Class::error_class, avg 4µs/call
# spent 81µs making 22 calls to Class::MOP::Class::instance_metaclass, avg 4µs/call | |||
| 302 | } | |||
| 303 | ||||
| 304 | # spent 18.4ms (693µs+17.7) within Class::MOP::Class::_fix_metaclass_incompatibility which was called 44 times, avg 418µs/call:
# 44 times (693µs+17.7ms) by Class::MOP::Class::_check_metaclass_compatibility at line 212, avg 418µs/call | |||
| 305 | 324 | 803µs | 2µs | my $self = shift; |
| 306 | my @supers = map { Class::MOP::Class->initialize($_) } @_; # spent 896µs making 52 calls to Class::MOP::Class::initialize, avg 3µs/call, max recursion depth 1 | |||
| 307 | ||||
| 308 | my $necessary = 0; | |||
| 309 | for my $super (@supers) { | |||
| 310 | $necessary = 1 # spent 16.8ms making 52 calls to Class::MOP::Class::_can_fix_metaclass_incompatibility, avg 323µs/call | |||
| 311 | if $self->_can_fix_metaclass_incompatibility($super); | |||
| 312 | } | |||
| 313 | return unless $necessary; | |||
| 314 | ||||
| 315 | for my $super (@supers) { | |||
| 316 | if (!$self->_class_metaclass_is_compatible($super->name)) { | |||
| 317 | $self->_fix_class_metaclass_incompatibility($super); | |||
| 318 | } | |||
| 319 | } | |||
| 320 | ||||
| 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 | |||
| 327 | ); | |||
| 328 | } | |||
| 329 | } | |||
| 330 | } | |||
| 331 | } | |||
| 332 | ||||
| 333 | # spent 16.8ms (1.62+15.2) within Class::MOP::Class::_can_fix_metaclass_incompatibility which was called 52 times, avg 323µs/call:
# 52 times (1.62ms+15.2ms) by Class::MOP::Class::_fix_metaclass_incompatibility at line 310, avg 323µs/call | |||
| 334 | 635 | 2.39ms | 4µs | my $self = shift; |
| 335 | my ($super_meta) = @_; | |||
| 336 | ||||
| 337 | return 1 if $self->_class_metaclass_can_be_made_compatible($super_meta); # spent 1.96ms making 52 calls to Class::MOP::Class::_class_metaclass_can_be_made_compatible, avg 38µs/call | |||
| 338 | ||||
| 339 | my %base_metaclass = $self->_base_metaclasses; # spent 515µs making 11 calls to Moose::Meta::Class::_base_metaclasses, avg 47µs/call
# spent 296µs making 41 calls to Class::MOP::Class::_base_metaclasses, avg 7µs/call | |||
| 340 | for my $metaclass_type (keys %base_metaclass) { | |||
| 341 | return 1 if $self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type); # spent 12.4ms making 323 calls to Class::MOP::Class::_single_metaclass_can_be_made_compatible, avg 38µs/call | |||
| 342 | } | |||
| 343 | ||||
| 344 | return; | |||
| 345 | } | |||
| 346 | ||||
| 347 | # spent 1.96ms (376µs+1.58) within Class::MOP::Class::_class_metaclass_can_be_made_compatible which was called 52 times, avg 38µs/call:
# 52 times (376µs+1.58ms) by Class::MOP::Class::_can_fix_metaclass_incompatibility at line 337, avg 38µs/call | |||
| 348 | 156 | 510µs | 3µs | my $self = shift; |
| 349 | my ($super_meta) = @_; | |||
| 350 | ||||
| 351 | return $self->_can_be_made_compatible_with($super_meta->_real_ref_name); # spent 800µs making 52 calls to Class::MOP::Object::_can_be_made_compatible_with, avg 15µs/call
# spent 785µs making 52 calls to Class::MOP::Class::_real_ref_name, avg 15µs/call | |||
| 352 | } | |||
| 353 | ||||
| 354 | # spent 12.4ms (4.03+8.40) within Class::MOP::Class::_single_metaclass_can_be_made_compatible which was called 323 times, avg 38µs/call:
# 323 times (4.03ms+8.40ms) by Class::MOP::Class::_can_fix_metaclass_incompatibility at line 341, avg 38µs/call | |||
| 355 | 2502 | 6.67ms | 3µs | my $self = shift; |
| 356 | my ($super_meta, $metaclass_type) = @_; | |||
| 357 | ||||
| 358 | my $specific_meta = $self->$metaclass_type; # spent 276µs making 52 calls to Class::MOP::Mixin::HasMethods::wrapped_method_metaclass, avg 5µs/call
# spent 269µs making 52 calls to Class::MOP::Mixin::HasAttributes::attribute_metaclass, avg 5µs/call
# spent 256µs making 52 calls to Class::MOP::Class::instance_metaclass, avg 5µs/call
# spent 236µs making 52 calls to Class::MOP::Mixin::HasMethods::method_metaclass, avg 5µs/call
# spent 167µs making 41 calls to Class::MOP::Class::constructor_class, avg 4µs/call
# spent 149µs making 41 calls to Class::MOP::Class::destructor_class, avg 4µs/call
# spent 76µs making 11 calls to Moose::Meta::Class::destructor_class, avg 7µs/call
# spent 68µs making 11 calls to Moose::Meta::Class::constructor_class, avg 6µs/call
# spent 67µs making 11 calls to Moose::Meta::Class::error_class, avg 6µs/call | |||
| 359 | ||||
| 360 | return unless $super_meta->can($metaclass_type); # spent 907µs making 323 calls to UNIVERSAL::can, avg 3µs/call | |||
| 361 | my $super_specific_meta = $super_meta->$metaclass_type; # spent 303µs making 52 calls to Class::MOP::Mixin::HasAttributes::attribute_metaclass, avg 6µs/call
# spent 292µs making 52 calls to Class::MOP::Mixin::HasMethods::method_metaclass, avg 6µs/call
# spent 278µs making 52 calls to Class::MOP::Class::instance_metaclass, avg 5µs/call
# spent 269µs making 52 calls to Class::MOP::Mixin::HasMethods::wrapped_method_metaclass, avg 5µs/call
# spent 223µs making 41 calls to Class::MOP::Class::constructor_class, avg 5µs/call
# spent 178µs making 41 calls to Class::MOP::Class::destructor_class, avg 4µs/call
# spent 96µs making 11 calls to Moose::Meta::Class::constructor_class, avg 9µs/call
# spent 87µs making 11 calls to Moose::Meta::Class::destructor_class, avg 8µs/call
# spent 50µs making 11 calls to Moose::Meta::Class::error_class, avg 5µs/call | |||
| 362 | ||||
| 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; | |||
| 366 | ||||
| 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; | |||
| 370 | ||||
| 371 | return 1 if $specific_meta->_can_be_made_compatible_with($super_specific_meta); # spent 4.15ms making 282 calls to Class::MOP::Object::_can_be_made_compatible_with, avg 15µs/call | |||
| 372 | } | |||
| 373 | ||||
| 374 | sub _fix_class_metaclass_incompatibility { | |||
| 375 | my $self = shift; | |||
| 376 | my ( $super_meta ) = @_; | |||
| 377 | ||||
| 378 | if ($self->_class_metaclass_can_be_made_compatible($super_meta)) { | |||
| 379 | ($self->is_pristine) | |||
| 380 | || confess "Can't fix metaclass incompatibility for " | |||
| 381 | . $self->name | |||
| 382 | . " because it is not pristine."; | |||
| 383 | ||||
| 384 | my $super_meta_name = $super_meta->_real_ref_name; | |||
| 385 | ||||
| 386 | $self->_make_compatible_with($super_meta_name); | |||
| 387 | } | |||
| 388 | } | |||
| 389 | ||||
| 390 | sub _fix_single_metaclass_incompatibility { | |||
| 391 | my $self = shift; | |||
| 392 | my ( $metaclass_type, $super_meta ) = @_; | |||
| 393 | ||||
| 394 | if ($self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type)) { | |||
| 395 | ($self->is_pristine) | |||
| 396 | || confess "Can't fix metaclass incompatibility for " | |||
| 397 | . $self->name | |||
| 398 | . " because it is not pristine."; | |||
| 399 | ||||
| 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; | |||
| 404 | } | |||
| 405 | } | |||
| 406 | ||||
| 407 | sub _restore_metaobjects_from { | |||
| 408 | my $self = shift; | |||
| 409 | my ($old_meta) = @_; | |||
| 410 | ||||
| 411 | $self->_restore_metamethods_from($old_meta); | |||
| 412 | $self->_restore_metaattributes_from($old_meta); | |||
| 413 | } | |||
| 414 | ||||
| 415 | sub _remove_generated_metaobjects { | |||
| 416 | my $self = shift; | |||
| 417 | ||||
| 418 | for my $attr (map { $self->get_attribute($_) } $self->get_attribute_list) { | |||
| 419 | $attr->remove_accessors; | |||
| 420 | } | |||
| 421 | } | |||
| 422 | ||||
| 423 | ## ANON classes | |||
| 424 | ||||
| 425 | { | |||
| 426 | # NOTE: | |||
| 427 | # this should be sufficient, if you have a | |||
| 428 | # use case where it is not, write a test and | |||
| 429 | # I will change it. | |||
| 430 | 2 | 900ns | 450ns | my $ANON_CLASS_SERIAL = 0; |
| 431 | ||||
| 432 | # NOTE: | |||
| 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 | 1 | 900ns | 900ns | my $ANON_CLASS_PREFIX = 'Class::MOP::Class::__ANON__::SERIAL::'; |
| 438 | ||||
| 439 | # spent 1.04ms (749µs+291µs) within Class::MOP::Class::is_anon_class which was called 114 times, avg 9µs/call:
# 71 times (486µs+183µs) by Class::MOP::Class::_construct_class_instance at line 112, avg 9µs/call
# 43 times (262µs+108µs) by Class::MOP::Class::_construct_instance at line 602, avg 9µs/call | |||
| 440 | 228 | 801µs | 4µs | my $self = shift; |
| 441 | 3 | 133µs | 44µs | no warnings 'uninitialized'; # spent 29µs making 1 call to warnings::unimport |
| 442 | $self->name =~ /^$ANON_CLASS_PREFIX/o; # spent 291µs making 114 calls to Class::MOP::Package::name, avg 3µs/call | |||
| 443 | } | |||
| 444 | ||||
| 445 | sub create_anon_class { | |||
| 446 | my ($class, %options) = @_; | |||
| 447 | my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL; | |||
| 448 | return $class->create($package_name, %options); | |||
| 449 | } | |||
| 450 | ||||
| 451 | # NOTE: | |||
| 452 | # this will only get called for | |||
| 453 | # anon-classes, all other calls | |||
| 454 | # are assumed to occur during | |||
| 455 | # global destruction and so don't | |||
| 456 | # really need to be handled explicitly | |||
| 457 | sub DESTROY { | |||
| 458 | my $self = shift; | |||
| 459 | ||||
| 460 | return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated | |||
| 461 | ||||
| 462 | 3 | 88µs | 29µs | no warnings 'uninitialized'; # spent 20µs making 1 call to warnings::unimport |
| 463 | my $name = $self->name; | |||
| 464 | return unless $name =~ /^$ANON_CLASS_PREFIX/o; | |||
| 465 | ||||
| 466 | # Moose does a weird thing where it replaces the metaclass for | |||
| 467 | # class when fixing metaclass incompatibility. In that case, | |||
| 468 | # we don't want to clean out the namespace now. We can detect | |||
| 469 | # that because Moose will explicitly update the singleton | |||
| 470 | # cache in Class::MOP. | |||
| 471 | my $current_meta = Class::MOP::get_metaclass_by_name($name); | |||
| 472 | return if $current_meta ne $self; | |||
| 473 | ||||
| 474 | my ($serial_id) = ($name =~ /^$ANON_CLASS_PREFIX(\d+)/o); | |||
| 475 | 3 | 4.67ms | 1.56ms | no strict 'refs'; # spent 21µs making 1 call to strict::unimport |
| 476 | @{$name . '::ISA'} = (); | |||
| 477 | %{$name . '::'} = (); | |||
| 478 | delete ${$ANON_CLASS_PREFIX}{$serial_id . '::'}; | |||
| 479 | ||||
| 480 | Class::MOP::remove_metaclass_by_name($name); | |||
| 481 | } | |||
| 482 | ||||
| 483 | } | |||
| 484 | ||||
| 485 | # creating classes with MOP ... | |||
| 486 | ||||
| 487 | # spent 3.13ms (90µs+3.04) within Class::MOP::Class::create which was called 2 times, avg 1.56ms/call:
# 2 times (90µs+3.04ms) by Class::MOP::Class::_immutable_metaclass at line 1214, avg 1.56ms/call | |||
| 488 | 34 | 111µs | 3µs | my ( $class, @args ) = @_; |
| 489 | ||||
| 490 | unshift @args, 'package' if @args % 2 == 1; | |||
| 491 | ||||
| 492 | my (%options) = @args; | |||
| 493 | my $package_name = $options{package}; | |||
| 494 | ||||
| 495 | (ref $options{superclasses} eq 'ARRAY') | |||
| 496 | || confess "You must pass an ARRAY ref of superclasses" | |||
| 497 | if exists $options{superclasses}; | |||
| 498 | ||||
| 499 | (ref $options{attributes} eq 'ARRAY') | |||
| 500 | || confess "You must pass an ARRAY ref of attributes" | |||
| 501 | if exists $options{attributes}; | |||
| 502 | ||||
| 503 | (ref $options{methods} eq 'HASH') | |||
| 504 | || confess "You must pass a HASH ref of methods" | |||
| 505 | if exists $options{methods}; | |||
| 506 | ||||
| 507 | $options{meta_name} = 'meta' | |||
| 508 | unless exists $options{meta_name}; | |||
| 509 | ||||
| 510 | my (%initialize_options) = @args; | |||
| 511 | delete @initialize_options{qw( | |||
| 512 | package | |||
| 513 | superclasses | |||
| 514 | attributes | |||
| 515 | methods | |||
| 516 | meta_name | |||
| 517 | version | |||
| 518 | authority | |||
| 519 | )}; | |||
| 520 | my $meta = $class->initialize( $package_name => %initialize_options ); # spent 383µs making 2 calls to Class::MOP::Class::initialize, avg 192µs/call | |||
| 521 | ||||
| 522 | $meta->_instantiate_module( $options{version}, $options{authority} ); # spent 74µs making 2 calls to Class::MOP::Module::_instantiate_module, avg 37µs/call | |||
| 523 | ||||
| 524 | $meta->_add_meta_method($options{meta_name}) # spent 1.36ms making 2 calls to Class::MOP::Mixin::HasMethods::_add_meta_method, avg 678µs/call | |||
| 525 | if defined $options{meta_name}; | |||
| 526 | ||||
| 527 | $meta->superclasses(@{$options{superclasses}}) # spent 1.22ms making 2 calls to Class::MOP::Class::superclasses, avg 612µs/call | |||
| 528 | if exists $options{superclasses}; | |||
| 529 | # NOTE: | |||
| 530 | # process attributes first, so that they can | |||
| 531 | # install accessors, but locally defined methods | |||
| 532 | # can then overwrite them. It is maybe a little odd, but | |||
| 533 | # I think this should be the order of things. | |||
| 534 | if (exists $options{attributes}) { | |||
| 535 | foreach my $attr (@{$options{attributes}}) { | |||
| 536 | $meta->add_attribute($attr); | |||
| 537 | } | |||
| 538 | } | |||
| 539 | if (exists $options{methods}) { | |||
| 540 | foreach my $method_name (keys %{$options{methods}}) { | |||
| 541 | $meta->add_method($method_name, $options{methods}->{$method_name}); | |||
| 542 | } | |||
| 543 | } | |||
| 544 | return $meta; | |||
| 545 | } | |||
| 546 | ||||
| 547 | ## Attribute readers | |||
| 548 | ||||
| 549 | # NOTE: | |||
| 550 | # all these attribute readers will be bootstrapped | |||
| 551 | # away in the Class::MOP bootstrap section | |||
| 552 | ||||
| 553 | 344 | 820µs | 2µs | sub instance_metaclass { $_[0]->{'instance_metaclass'} } |
| 554 | 102 | 353µs | 3µs | sub immutable_trait { $_[0]->{'immutable_trait'} } |
| 555 | 166 | 388µs | 2µs | sub constructor_class { $_[0]->{'constructor_class'} } |
| 556 | 58 | 151µs | 3µs | sub constructor_name { $_[0]->{'constructor_name'} } |
| 557 | 166 | 320µs | 2µs | sub destructor_class { $_[0]->{'destructor_class'} } |
| 558 | ||||
| 559 | # Instance Construction & Cloning | |||
| 560 | ||||
| 561 | # spent 13.6ms (339µs+13.3) within Class::MOP::Class::new_object which was called 35 times, avg 389µs/call:
# 26 times (243µs+8.78ms) by Class::MOP::Method::_new at line 54 of /usr/local/lib/perl/5.10.0/Class/MOP/Method.pm, avg 347µs/call
# 7 times (65µs+2.87ms) by Class::MOP::Method::Generated::__ANON__[(eval 0)[/usr/local/lib/perl/5.10.0/Class/MOP/Method/Generated.pm:53]:24] at line 4 of (eval 0)[/usr/local/lib/perl/5.10.0/Class/MOP/Method/Generated.pm:53] at line 53 of /usr/local/lib/perl/5.10.0/Class/MOP/Method/Generated.pm, avg 419µs/call
# once (18µs+1.22ms) by Class::MOP::Attribute::_new at line 62 of /usr/local/lib/perl/5.10.0/Class/MOP/Attribute.pm
# once (14µs+400µs) by Class::MOP::Object::_new at line 22 of /usr/local/lib/perl/5.10.0/Class/MOP/Object.pm | |||
| 562 | 105 | 664µs | 6µs | my $class = shift; |
| 563 | ||||
| 564 | # NOTE: | |||
| 565 | # we need to protect the integrity of the | |||
| 566 | # Class::MOP::Class singletons here, so we | |||
| 567 | # delegate this to &construct_class_instance | |||
| 568 | # which will deal with the singletons | |||
| 569 | return $class->_construct_class_instance(@_) # spent 225µs making 35 calls to UNIVERSAL::isa, avg 6µs/call
# spent 105µs making 35 calls to Class::MOP::Package::name, avg 3µs/call | |||
| 570 | if $class->name->isa('Class::MOP::Class'); | |||
| 571 | return $class->_construct_instance(@_); # spent 12.9ms making 35 calls to Class::MOP::Class::_construct_instance, avg 370µs/call | |||
| 572 | } | |||
| 573 | ||||
| 574 | # spent 21.7ms (1.89+19.8) within Class::MOP::Class::_construct_instance which was called 43 times, avg 505µs/call:
# 35 times (1.20ms+11.7ms) by Class::MOP::Class::new_object at line 571, avg 370µs/call
# 8 times (694µs+8.10ms) by Class::MOP::Class::_construct_class_instance at line 101, avg 1.10ms/call | |||
| 575 | 734 | 3.15ms | 4µs | my $class = shift; |
| 576 | my $params = @_ == 1 ? $_[0] : {@_}; | |||
| 577 | my $meta_instance = $class->get_meta_instance(); # spent 2.09ms making 16 calls to Class::MOP::Class::get_meta_instance, avg 130µs/call
# spent 482µs making 27 calls to Class::MOP::Class::Immutable::Class::MOP::Class::get_meta_instance, avg 18µs/call | |||
| 578 | # FIXME: | |||
| 579 | # the code below is almost certainly incorrect | |||
| 580 | # but this is foreign inheritance, so we might | |||
| 581 | # have to kludge it in the end. | |||
| 582 | my $instance; | |||
| 583 | if (my $instance_class = blessed($params->{__INSTANCE__})) { # spent 187µs making 43 calls to Scalar::Util::blessed, avg 4µs/call | |||
| 584 | ($instance_class eq $class->name) | |||
| 585 | || confess "Objects passed as the __INSTANCE__ parameter must " | |||
| 586 | . "already be blessed into the correct class, but " | |||
| 587 | . "$params->{__INSTANCE__} is not a " . $class->name; | |||
| 588 | $instance = $params->{__INSTANCE__}; | |||
| 589 | } | |||
| 590 | elsif (exists $params->{__INSTANCE__}) { | |||
| 591 | confess "The __INSTANCE__ parameter must be a blessed reference, not " | |||
| 592 | . $params->{__INSTANCE__}; | |||
| 593 | } | |||
| 594 | else { | |||
| 595 | $instance = $meta_instance->create_instance(); # spent 2.29ms making 43 calls to Class::MOP::Instance::create_instance, avg 53µs/call | |||
| 596 | } | |||
| 597 | foreach my $attr ($class->get_all_attributes()) { # spent 1.85ms making 16 calls to Class::MOP::Class::get_all_attributes, avg 116µs/call
# spent 843µs making 27 calls to Class::MOP::Class::Immutable::Class::MOP::Class::get_all_attributes, avg 31µs/call | |||
| 598 | $attr->initialize_instance_slot($meta_instance, $instance, $params); # spent 11.7ms making 347 calls to Class::MOP::Attribute::initialize_instance_slot, avg 34µs/call | |||
| 599 | } | |||
| 600 | # NOTE: | |||
| 601 | # this will only work for a HASH instance type | |||
| 602 | if ($class->is_anon_class) { # spent 371µs making 43 calls to Class::MOP::Class::is_anon_class, avg 9µs/call | |||
| 603 | (reftype($instance) eq 'HASH') | |||
| 604 | || confess "Currently only HASH based instances are supported with instance of anon-classes"; | |||
| 605 | # NOTE: | |||
| 606 | # At some point we should make this official | |||
| 607 | # as a reserved slot name, but right now I am | |||
| 608 | # going to keep it here. | |||
| 609 | # my $RESERVED_MOP_SLOT = '__MOP__'; | |||
| 610 | $instance->{'__MOP__'} = $class; | |||
| 611 | } | |||
| 612 | return $instance; | |||
| 613 | } | |||
| 614 | ||||
| 615 | ||||
| 616 | # spent 15.8ms (1.58+14.2) within Class::MOP::Class::get_meta_instance which was called 321 times, avg 49µs/call:
# 126 times (510µs+2.06ms) by Class::MOP::Attribute::inline_set at line 419 of /usr/local/lib/perl/5.10.0/Class/MOP/Attribute.pm, avg 20µs/call
# 78 times (441µs+3.37ms) by Class::MOP::Attribute::inline_get at line 411 of /usr/local/lib/perl/5.10.0/Class/MOP/Attribute.pm, avg 49µs/call
# 38 times (211µs+2.11ms) by Class::MOP::Class::inline_create_instance at line 638, avg 61µs/call
# 23 times (125µs+1.34ms) by Moose::Meta::Attribute::inline_set at line 587 of /usr/local/lib/perl/5.10.0/Moose/Meta/Attribute.pm, avg 64µs/call
# 22 times (108µs+3.10ms) by Class::MOP::Class::Immutable::Trait::get_meta_instance at line 74 of /usr/local/lib/perl/5.10.0/Class/MOP/Class/Immutable/Trait.pm, avg 146µs/call
# 16 times (92µs+1.99ms) by Class::MOP::Class::_construct_instance at line 577, avg 130µs/call
# 13 times (53µs+0s) by Class::MOP::Attribute::inline_has at line 428 of /usr/local/lib/perl/5.10.0/Class/MOP/Attribute.pm, avg 4µs/call
# 3 times (22µs+0s) by Class::MOP::Attribute::get_raw_value at line 274 of /usr/local/lib/perl/5.10.0/Class/MOP/Attribute.pm, avg 7µs/call
# once (12µs+228µs) at line 683 of /usr/local/lib/perl/5.10.0/Class/MOP.pm
# once (7µs+0s) by Class::MOP::Attribute::has_value at line 282 of /usr/local/lib/perl/5.10.0/Class/MOP/Attribute.pm | |||
| 617 | 642 | 1.02ms | 2µs | my $self = shift; |
| 618 | $self->{'_meta_instance'} ||= $self->_create_meta_instance(); # spent 14.2ms making 58 calls to Class::MOP::Class::_create_meta_instance, avg 245µs/call | |||
| 619 | } | |||
| 620 | ||||
| 621 | # spent 14.2ms (661µs+13.5) within Class::MOP::Class::_create_meta_instance which was called 58 times, avg 245µs/call:
# 58 times (661µs+13.5ms) by Class::MOP::Class::get_meta_instance at line 618, avg 245µs/call | |||
| 622 | 232 | 1.14ms | 5µs | my $self = shift; |
| 623 | ||||
| 624 | my $instance = $self->instance_metaclass->new( # spent 7.21ms making 58 calls to Class::MOP::Instance::new, avg 124µs/call
# spent 4.42ms making 46 calls to Class::MOP::Class::get_all_attributes, avg 96µs/call
# spent 1.43ms making 12 calls to Class::MOP::Class::Immutable::Class::MOP::Class::get_all_attributes, avg 119µs/call
# spent 278µs making 58 calls to Class::MOP::Class::instance_metaclass, avg 5µs/call | |||
| 625 | associated_metaclass => $self, | |||
| 626 | attributes => [ $self->get_all_attributes() ], | |||
| 627 | ); | |||
| 628 | ||||
| 629 | $self->add_meta_instance_dependencies() # spent 204µs making 58 calls to Class::MOP::Instance::is_dependent_on_superclasses, avg 4µs/call | |||
| 630 | if $instance->is_dependent_on_superclasses(); | |||
| 631 | ||||
| 632 | return $instance; | |||
| 633 | } | |||
| 634 | ||||
| 635 | # spent 2.85ms (272µs+2.58) within Class::MOP::Class::inline_create_instance which was called 38 times, avg 75µs/call:
# 31 times (217µs+2.19ms) by Class::MOP::Method::Constructor::_generate_constructor_method_inline at line 112 of /usr/local/lib/perl/5.10.0/Class/MOP/Method/Constructor.pm, avg 78µs/call
# 7 times (55µs+382µs) by Moose::Meta::Method::Constructor::_generate_instance at line 131 of /usr/local/lib/perl/5.10.0/Moose/Meta/Method/Constructor.pm, avg 62µs/call | |||
| 636 | 76 | 356µs | 5µs | my $self = shift; |
| 637 | ||||
| 638 | return $self->get_meta_instance->inline_create_instance(@_); # spent 2.32ms making 38 calls to Class::MOP::Class::get_meta_instance, avg 61µs/call
# spent 253µs making 38 calls to Class::MOP::Instance::inline_create_instance, avg 7µs/call | |||
| 639 | } | |||
| 640 | ||||
| 641 | sub inline_rebless_instance { | |||
| 642 | my $self = shift; | |||
| 643 | ||||
| 644 | return $self->get_meta_instance->inline_rebless_instance_structure(@_); | |||
| 645 | } | |||
| 646 | ||||
| 647 | sub clone_object { | |||
| 648 | my $class = shift; | |||
| 649 | my $instance = shift; | |||
| 650 | (blessed($instance) && $instance->isa($class->name)) | |||
| 651 | || confess "You must pass an instance of the metaclass (" . (ref $class ? $class->name : $class) . "), not ($instance)"; | |||
| 652 | ||||
| 653 | # NOTE: | |||
| 654 | # we need to protect the integrity of the | |||
| 655 | # Class::MOP::Class singletons here, they | |||
| 656 | # should not be cloned. | |||
| 657 | return $instance if $instance->isa('Class::MOP::Class'); | |||
| 658 | $class->_clone_instance($instance, @_); | |||
| 659 | } | |||
| 660 | ||||
| 661 | sub _clone_instance { | |||
| 662 | my ($class, $instance, %params) = @_; | |||
| 663 | (blessed($instance)) | |||
| 664 | || confess "You can only clone instances, ($instance) is not a blessed instance"; | |||
| 665 | my $meta_instance = $class->get_meta_instance(); | |||
| 666 | my $clone = $meta_instance->clone_instance($instance); | |||
| 667 | foreach my $attr ($class->get_all_attributes()) { | |||
| 668 | if ( defined( my $init_arg = $attr->init_arg ) ) { | |||
| 669 | if (exists $params{$init_arg}) { | |||
| 670 | $attr->set_value($clone, $params{$init_arg}); | |||
| 671 | } | |||
| 672 | } | |||
| 673 | } | |||
| 674 | return $clone; | |||
| 675 | } | |||
| 676 | ||||
| 677 | sub _force_rebless_instance { | |||
| 678 | my ($self, $instance, %params) = @_; | |||
| 679 | my $old_metaclass = Class::MOP::class_of($instance); | |||
| 680 | ||||
| 681 | $old_metaclass->rebless_instance_away($instance, $self, %params) | |||
| 682 | if $old_metaclass; | |||
| 683 | ||||
| 684 | my $meta_instance = $self->get_meta_instance; | |||
| 685 | ||||
| 686 | # rebless! | |||
| 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); | |||
| 689 | ||||
| 690 | $self->_fixup_attributes_after_rebless($instance, $old_metaclass, %params); | |||
| 691 | } | |||
| 692 | ||||
| 693 | sub rebless_instance { | |||
| 694 | my ($self, $instance, %params) = @_; | |||
| 695 | my $old_metaclass = Class::MOP::class_of($instance); | |||
| 696 | ||||
| 697 | my $old_class = $old_metaclass ? $old_metaclass->name : blessed($instance); | |||
| 698 | $self->name->isa($old_class) | |||
| 699 | || confess "You may rebless only into a subclass of ($old_class), of which (". $self->name .") isn't."; | |||
| 700 | ||||
| 701 | $self->_force_rebless_instance($_[1], %params); | |||
| 702 | ||||
| 703 | return $instance; | |||
| 704 | } | |||
| 705 | ||||
| 706 | sub rebless_instance_back { | |||
| 707 | my ( $self, $instance ) = @_; | |||
| 708 | my $old_metaclass = Class::MOP::class_of($instance); | |||
| 709 | ||||
| 710 | my $old_class | |||
| 711 | = $old_metaclass ? $old_metaclass->name : blessed($instance); | |||
| 712 | $old_class->isa( $self->name ) | |||
| 713 | || confess | |||
| 714 | "You may rebless only into a superclass of ($old_class), of which (" | |||
| 715 | . $self->name | |||
| 716 | . ") isn't."; | |||
| 717 | ||||
| 718 | $self->_force_rebless_instance($_[1]); | |||
| 719 | ||||
| 720 | return $instance; | |||
| 721 | } | |||
| 722 | ||||
| 723 | sub rebless_instance_away { | |||
| 724 | # this intentionally does nothing, it is just a hook | |||
| 725 | } | |||
| 726 | ||||
| 727 | sub _fixup_attributes_after_rebless { | |||
| 728 | my $self = shift; | |||
| 729 | my ($instance, $rebless_from, %params) = @_; | |||
| 730 | my $meta_instance = $self->get_meta_instance; | |||
| 731 | ||||
| 732 | for my $attr ( $rebless_from->get_all_attributes ) { | |||
| 733 | next if $self->find_attribute_by_name( $attr->name ); | |||
| 734 | $meta_instance->deinitialize_slot( $instance, $_ ) for $attr->slots; | |||
| 735 | } | |||
| 736 | ||||
| 737 | foreach my $attr ( $self->get_all_attributes ) { | |||
| 738 | if ( $attr->has_value($instance) ) { | |||
| 739 | if ( defined( my $init_arg = $attr->init_arg ) ) { | |||
| 740 | $params{$init_arg} = $attr->get_value($instance) | |||
| 741 | unless exists $params{$init_arg}; | |||
| 742 | } | |||
| 743 | else { | |||
| 744 | $attr->set_value($instance, $attr->get_value($instance)); | |||
| 745 | } | |||
| 746 | } | |||
| 747 | } | |||
| 748 | ||||
| 749 | foreach my $attr ($self->get_all_attributes) { | |||
| 750 | $attr->initialize_instance_slot($meta_instance, $instance, \%params); | |||
| 751 | } | |||
| 752 | } | |||
| 753 | ||||
| 754 | # spent 2.51ms (664µs+1.84) within Class::MOP::Class::_attach_attribute which was called 123 times, avg 20µs/call:
# 123 times (664µs+1.84ms) by Class::MOP::Mixin::HasAttributes::add_attribute at line 28 of /usr/local/lib/perl/5.10.0/Class/MOP/Mixin/HasAttributes.pm, avg 20µs/call | |||
| 755 | 246 | 670µs | 3µs | my ($self, $attribute) = @_; |
| 756 | $attribute->attach_to_class($self); # spent 1.84ms making 123 calls to Class::MOP::Attribute::attach_to_class, avg 15µs/call | |||
| 757 | } | |||
| 758 | ||||
| 759 | # spent 65.8ms (1.61+64.2) within Class::MOP::Class::_post_add_attribute which was called 123 times, avg 535µs/call:
# 123 times (1.61ms+64.2ms) by Class::MOP::Mixin::HasAttributes::add_attribute at line 45 of /usr/local/lib/perl/5.10.0/Class/MOP/Mixin/HasAttributes.pm, avg 535µs/call | |||
| 760 | 369 | 2.19ms | 6µs | my ( $self, $attribute ) = @_; |
| 761 | ||||
| 762 | $self->invalidate_meta_instances; # spent 1.50ms making 123 calls to Class::MOP::Class::invalidate_meta_instances, avg 12µs/call | |||
| 763 | ||||
| 764 | # invalidate package flag here | |||
| 765 | # spent 59.5ms (1.30+58.2) within Class::MOP::Class::__ANON__[/usr/local/lib/perl/5.10.0/Class/MOP/Class.pm:768] which was called 123 times, avg 484µs/call:
# 123 times (1.30ms+58.2ms) by Try::Tiny::try at line 76 of /usr/local/share/perl/5.10.0/Try/Tiny.pm, avg 484µs/call | |||
| 766 | 246 | 1.26ms | 5µs | local $SIG{__DIE__}; |
| 767 | $attribute->install_accessors; # spent 40.5ms making 114 calls to Class::MOP::Attribute::install_accessors, avg 355µs/call
# spent 17.7ms making 9 calls to Moose::Meta::Attribute::install_accessors, avg 1.97ms/call | |||
| 768 | } | |||
| 769 | catch { | |||
| 770 | $self->remove_attribute( $attribute->name ); | |||
| 771 | die $_; | |||
| 772 | }; # spent 61.8ms making 123 calls to Try::Tiny::try, avg 490µs/call, max recursion depth 1
# spent 865µs making 123 calls to Try::Tiny::catch, avg 7µs/call | |||
| 773 | } | |||
| 774 | ||||
| 775 | sub remove_attribute { | |||
| 776 | my $self = shift; | |||
| 777 | ||||
| 778 | my $removed_attribute = $self->SUPER::remove_attribute(@_) | |||
| 779 | or return; | |||
| 780 | ||||
| 781 | $self->invalidate_meta_instances; | |||
| 782 | ||||
| 783 | $removed_attribute->remove_accessors; | |||
| 784 | $removed_attribute->detach_from_class; | |||
| 785 | ||||
| 786 | return$removed_attribute; | |||
| 787 | } | |||
| 788 | ||||
| 789 | # spent 3.79ms (779µs+3.01) within Class::MOP::Class::find_attribute_by_name which was called 58 times, avg 65µs/call:
# 58 times (779µs+3.01ms) by Class::MOP::Class::_immutable_metaclass at line 1191, avg 65µs/call | |||
| 790 | 232 | 1.28ms | 6µs | my ( $self, $attr_name ) = @_; |
| 791 | ||||
| 792 | foreach my $class ( $self->linearized_isa ) { # spent 996µs making 54 calls to Class::MOP::Class::Immutable::Class::MOP::Class::linearized_isa, avg 18µs/call
# spent 42µs making 4 calls to Class::MOP::Class::linearized_isa, avg 11µs/call | |||
| 793 | # fetch the meta-class ... | |||
| 794 | my $meta = Class::MOP::Class->initialize($class); # spent 638µs making 58 calls to Class::MOP::Class::initialize, avg 11µs/call | |||
| 795 | return $meta->get_attribute($attr_name) # spent 736µs making 58 calls to Class::MOP::Mixin::HasAttributes::has_attribute, avg 13µs/call
# spent 598µs making 58 calls to Class::MOP::Mixin::HasAttributes::get_attribute, avg 10µs/call | |||
| 796 | if $meta->has_attribute($attr_name); | |||
| 797 | } | |||
| 798 | ||||
| 799 | return; | |||
| 800 | } | |||
| 801 | ||||
| 802 | # spent 12.0ms (3.96+8.00) within Class::MOP::Class::get_all_attributes which was called 116 times, avg 103µs/call:
# 46 times (1.51ms+2.90ms) by Class::MOP::Class::_create_meta_instance at line 624, avg 96µs/call
# 38 times (1.26ms+2.44ms) by Class::MOP::Method::Constructor::_attributes at line 77 of /usr/local/lib/perl/5.10.0/Class/MOP/Method/Constructor.pm, avg 97µs/call
# 16 times (580µs+1.27ms) by Class::MOP::Class::_construct_instance at line 597, avg 116µs/call
# 15 times (521µs+1.29ms) by Class::MOP::Class::Immutable::Trait::get_all_attributes at line 68 of /usr/local/lib/perl/5.10.0/Class/MOP/Class/Immutable/Trait.pm, avg 120µs/call
# once (82µs+100µs) by Moose::Meta::Attribute::new at line 63 of /usr/local/lib/perl/5.10.0/Moose/Meta/Attribute.pm | |||
| 803 | 1192 | 5.61ms | 5µs | my $self = shift; |
| 804 | my %attrs = map { %{ Class::MOP::Class->initialize($_)->_attribute_map } } # spent 4.46ms making 422 calls to Class::MOP::Class::initialize, avg 10µs/call, max recursion depth 1
# spent 1.66ms making 422 calls to Class::MOP::Mixin::HasAttributes::_attribute_map, avg 4µs/call
# spent 1.43ms making 101 calls to Class::MOP::Class::linearized_isa, avg 14µs/call
# spent 454µs making 15 calls to Class::MOP::Class::Immutable::Class::MOP::Class::linearized_isa, avg 30µs/call | |||
| 805 | reverse $self->linearized_isa; | |||
| 806 | return values %attrs; | |||
| 807 | } | |||
| 808 | ||||
| 809 | # Inheritance | |||
| 810 | ||||
| 811 | sub superclasses { | |||
| 812 | 494 | 1.81ms | 4µs | my $self = shift; |
| 813 | ||||
| 814 | my $isa = $self->get_or_add_package_symbol( # spent 6.17ms making 104 calls to Class::MOP::Package::get_or_add_package_symbol, avg 59µs/call | |||
| 815 | { sigil => '@', type => 'ARRAY', name => 'ISA' } ); | |||
| 816 | ||||
| 817 | if (@_) { | |||
| 818 | my @supers = @_; | |||
| 819 | @{$isa} = @supers; | |||
| 820 | ||||
| 821 | # NOTE: | |||
| 822 | # on 5.8 and below, we need to call | |||
| 823 | # a method to get Perl to detect | |||
| 824 | # a cycle in the class hierarchy | |||
| 825 | my $class = $self->name; # spent 50µs making 13 calls to Class::MOP::Package::name, avg 4µs/call | |||
| 826 | $class->isa($class); # spent 57µs making 13 calls to UNIVERSAL::isa, avg 4µs/call | |||
| 827 | ||||
| 828 | # NOTE: | |||
| 829 | # we need to check the metaclass | |||
| 830 | # compatibility here so that we can | |||
| 831 | # be sure that the superclass is | |||
| 832 | # not potentially creating an issues | |||
| 833 | # we don't know about | |||
| 834 | ||||
| 835 | $self->_check_metaclass_compatibility(); # spent 13.8ms making 13 calls to Class::MOP::Class::_check_metaclass_compatibility, avg 1.06ms/call | |||
| 836 | $self->_superclasses_updated(); # spent 150µs making 13 calls to Class::MOP::Class::_superclasses_updated, avg 12µs/call | |||
| 837 | } | |||
| 838 | ||||
| 839 | return @{$isa}; | |||
| 840 | } | |||
| 841 | ||||
| 842 | # spent 150µs (85+65) within Class::MOP::Class::_superclasses_updated which was called 13 times, avg 12µs/call:
# 13 times (85µs+65µs) at line 836, avg 12µs/call | |||
| 843 | 26 | 75µs | 3µs | my $self = shift; |
| 844 | $self->update_meta_instance_dependencies(); # spent 65µs making 13 calls to Class::MOP::Class::update_meta_instance_dependencies, avg 5µs/call | |||
| 845 | } | |||
| 846 | ||||
| 847 | sub subclasses { | |||
| 848 | my $self = shift; | |||
| 849 | my $super_class = $self->name; | |||
| 850 | ||||
| 851 | return @{ $super_class->mro::get_isarev() }; | |||
| 852 | } | |||
| 853 | ||||
| 854 | sub direct_subclasses { | |||
| 855 | my $self = shift; | |||
| 856 | my $super_class = $self->name; | |||
| 857 | ||||
| 858 | return grep { | |||
| 859 | grep { | |||
| 860 | $_ eq $super_class | |||
| 861 | } Class::MOP::Class->initialize($_)->superclasses | |||
| 862 | } $self->subclasses; | |||
| 863 | } | |||
| 864 | ||||
| 865 | # spent 4.64ms (2.19+2.45) within Class::MOP::Class::linearized_isa which was called 347 times, avg 13µs/call:
# 108 times (659µs+703µs) by Class::MOP::Class::find_method_by_name at line 985, avg 13µs/call
# 101 times (637µs+791µs) by Class::MOP::Class::get_all_attributes at line 804, avg 14µs/call
# 95 times (622µs+648µs) by Class::MOP::Class::find_next_method_by_name at line 1032, avg 13µs/call
# 17 times (103µs+132µs) by Class::MOP::Class::Immutable::Trait::linearized_isa at line 50 of /usr/local/lib/perl/5.10.0/Class/MOP/Class/Immutable/Trait.pm, avg 14µs/call
# 14 times (102µs+78µs) by Class::MOP::Class::find_all_methods_by_name at line 1016, avg 13µs/call
# 8 times (48µs+74µs) by Class::MOP::Class::get_all_methods at line 996, avg 15µs/call
# 4 times (22µs+20µs) by Class::MOP::Class::find_attribute_by_name at line 792, avg 11µs/call | |||
| 866 | 347 | 3.91ms | 11µs | return @{ mro::get_linear_isa( (shift)->name ) }; # spent 1.61ms making 347 calls to mro::get_linear_isa, avg 5µs/call
# spent 839µs making 347 calls to Class::MOP::Package::name, avg 2µs/call |
| 867 | } | |||
| 868 | ||||
| 869 | sub class_precedence_list { | |||
| 870 | my $self = shift; | |||
| 871 | my $name = $self->name; | |||
| 872 | ||||
| 873 | unless (Class::MOP::IS_RUNNING_ON_5_10()) { | |||
| 874 | # NOTE: | |||
| 875 | # We need to check for circular inheritance here | |||
| 876 | # if we are are not on 5.10, cause 5.8 detects it | |||
| 877 | # late. This will do nothing if all is well, and | |||
| 878 | # blow up otherwise. Yes, it's an ugly hack, better | |||
| 879 | # suggestions are welcome. | |||
| 880 | # - SL | |||
| 881 | ($name || return)->isa('This is a test for circular inheritance') | |||
| 882 | } | |||
| 883 | ||||
| 884 | # if our mro is c3, we can | |||
| 885 | # just grab the linear_isa | |||
| 886 | if (mro::get_mro($name) eq 'c3') { | |||
| 887 | return @{ mro::get_linear_isa($name) } | |||
| 888 | } | |||
| 889 | else { | |||
| 890 | # NOTE: | |||
| 891 | # we can't grab the linear_isa for dfs | |||
| 892 | # since it has all the duplicates | |||
| 893 | # already removed. | |||
| 894 | return ( | |||
| 895 | $name, | |||
| 896 | map { | |||
| 897 | Class::MOP::Class->initialize($_)->class_precedence_list() | |||
| 898 | } $self->superclasses() | |||
| 899 | ); | |||
| 900 | } | |||
| 901 | } | |||
| 902 | ||||
| 903 | ## Methods | |||
| 904 | ||||
| 905 | { | |||
| 906 | 1 | 600ns | 600ns | # spent 21.0ms (806µs+20.2) within Class::MOP::Class::__ANON__[/usr/local/lib/perl/5.10.0/Class/MOP/Class.pm:935] which was called 46 times, avg 457µs/call:
# 46 times (806µs+20.2ms) by Class::MOP::Class::add_around_method_modifier at line 961, avg 457µs/call |
| 907 | 414 | 1.31ms | 3µs | my ($self, $method_name) = @_; |
| 908 | my $wrapped_metaclass = $self->wrapped_method_metaclass; # spent 194µs making 46 calls to Class::MOP::Mixin::HasMethods::wrapped_method_metaclass, avg 4µs/call | |||
| 909 | # fetch it locally | |||
| 910 | my $method = $self->get_method($method_name); # spent 2.03ms making 46 calls to Class::MOP::Mixin::HasMethods::get_method, avg 44µs/call | |||
| 911 | # if we dont have local ... | |||
| 912 | unless ($method) { | |||
| 913 | # try to find the next method | |||
| 914 | $method = $self->find_next_method_by_name($method_name); # spent 8.71ms making 46 calls to Class::MOP::Class::find_next_method_by_name, avg 189µs/call | |||
| 915 | # die if it does not exist | |||
| 916 | (defined $method) | |||
| 917 | || confess "The method '$method_name' was not found in the inheritance hierarchy for " . $self->name; | |||
| 918 | # and now make sure to wrap it | |||
| 919 | # even if it is already wrapped | |||
| 920 | # because we need a new sub ref | |||
| 921 | $method = $wrapped_metaclass->wrap($method, # spent 4.10ms making 46 calls to Class::MOP::Method::Wrapped::wrap, avg 89µs/call
# spent 113µs making 46 calls to Class::MOP::Package::name, avg 2µs/call | |||
| 922 | package_name => $self->name, | |||
| 923 | name => $method_name, | |||
| 924 | ); | |||
| 925 | } | |||
| 926 | else { | |||
| 927 | # now make sure we wrap it properly | |||
| 928 | $method = $wrapped_metaclass->wrap($method, | |||
| 929 | package_name => $self->name, | |||
| 930 | name => $method_name, | |||
| 931 | ) unless $method->isa($wrapped_metaclass); | |||
| 932 | } | |||
| 933 | $self->add_method($method_name => $method); # spent 5.09ms making 46 calls to Class::MOP::Mixin::HasMethods::add_method, avg 111µs/call | |||
| 934 | return $method; | |||
| 935 | 1 | 5µs | 5µs | }; |
| 936 | ||||
| 937 | sub add_before_method_modifier { | |||
| 938 | my ($self, $method_name, $method_modifier) = @_; | |||
| 939 | (defined $method_name && length $method_name) | |||
| 940 | || confess "You must pass in a method name"; | |||
| 941 | my $method = $fetch_and_prepare_method->($self, $method_name); | |||
| 942 | $method->add_before_modifier( | |||
| 943 | subname(':before' => $method_modifier) | |||
| 944 | ); | |||
| 945 | } | |||
| 946 | ||||
| 947 | sub add_after_method_modifier { | |||
| 948 | my ($self, $method_name, $method_modifier) = @_; | |||
| 949 | (defined $method_name && length $method_name) | |||
| 950 | || confess "You must pass in a method name"; | |||
| 951 | my $method = $fetch_and_prepare_method->($self, $method_name); | |||
| 952 | $method->add_after_modifier( | |||
| 953 | subname(':after' => $method_modifier) | |||
| 954 | ); | |||
| 955 | } | |||
| 956 | ||||
| 957 | # spent 23.4ms (394µs+23.0) within Class::MOP::Class::add_around_method_modifier which was called 46 times, avg 510µs/call:
# 46 times (394µs+23.0ms) by Class::MOP::MiniTrait::apply at line 21 of /usr/local/lib/perl/5.10.0/Class/MOP/MiniTrait.pm, avg 510µs/call | |||
| 958 | 184 | 889µs | 5µs | my ($self, $method_name, $method_modifier) = @_; |
| 959 | (defined $method_name && length $method_name) | |||
| 960 | || confess "You must pass in a method name"; | |||
| 961 | my $method = $fetch_and_prepare_method->($self, $method_name); # spent 21.0ms making 46 calls to Class::MOP::Class::__ANON__[/usr/local/lib/perl/5.10.0/Class/MOP/Class.pm:935], avg 457µs/call | |||
| 962 | $method->add_around_modifier( # spent 1.63ms making 46 calls to Class::MOP::Method::Wrapped::add_around_modifier, avg 36µs/call
# spent 368µs making 46 calls to Sub::Name::subname, avg 8µs/call | |||
| 963 | subname(':around' => $method_modifier) | |||
| 964 | ); | |||
| 965 | } | |||
| 966 | ||||
| 967 | # NOTE: | |||
| 968 | # the methods above used to be named like this: | |||
| 969 | # ${pkg}::${method}:(before|after|around) | |||
| 970 | # but this proved problematic when using one modifier | |||
| 971 | # to wrap multiple methods (something which is likely | |||
| 972 | # to happen pretty regularly IMO). So instead of naming | |||
| 973 | # it like this, I have chosen to just name them purely | |||
| 974 | # with their modifier names, like so: | |||
| 975 | # :(before|after|around) | |||
| 976 | # The fact is that in a stack trace, it will be fairly | |||
| 977 | # evident from the context what method they are attached | |||
| 978 | # to, and so don't need the fully qualified name. | |||
| 979 | } | |||
| 980 | ||||
| 981 | # spent 30.3ms (2.09+28.2) within Class::MOP::Class::find_method_by_name which was called 108 times, avg 281µs/call:
# 56 times (1.26ms+19.9ms) by Class::MOP::MiniTrait::apply at line 21 of /usr/local/lib/perl/5.10.0/Class/MOP/MiniTrait.pm, avg 378µs/call
# 31 times (461µs+3.39ms) by Class::MOP::Mixin::HasMethods::_add_meta_method at line 25 of /usr/local/lib/perl/5.10.0/Class/MOP/Mixin/HasMethods.pm, avg 124µs/call
# 7 times (160µs+2.74ms) by Moose::Meta::Method::Constructor::_generate_BUILDARGS at line 145 of /usr/local/lib/perl/5.10.0/Moose/Meta/Method/Constructor.pm, avg 414µs/call
# 7 times (132µs+1.53ms) by Moose::Meta::Method::Destructor::is_needed at line 62 of /usr/local/lib/perl/5.10.0/Moose/Meta/Method/Destructor.pm, avg 238µs/call
# 7 times (83µs+639µs) at line 14 of /usr/local/lib/perl/5.10.0/Moose/Meta/Attribute/Native.pm, avg 103µs/call | |||
| 982 | 972 | 3.19ms | 3µs | my ($self, $method_name) = @_; |
| 983 | (defined $method_name && length $method_name) | |||
| 984 | || confess "You must define a method name to find"; | |||
| 985 | foreach my $class ($self->linearized_isa) { # spent 1.36ms making 108 calls to Class::MOP::Class::linearized_isa, avg 13µs/call | |||
| 986 | my $method = Class::MOP::Class->initialize($class)->get_method($method_name); # spent 23.8ms making 301 calls to Class::MOP::Mixin::HasMethods::get_method, avg 79µs/call
# spent 3.11ms making 301 calls to Class::MOP::Class::initialize, avg 10µs/call | |||
| 987 | return $method if defined $method; | |||
| 988 | } | |||
| 989 | return; | |||
| 990 | } | |||
| 991 | ||||
| 992 | # spent 7.14ms (296µs+6.84) within Class::MOP::Class::get_all_methods which was called 8 times, avg 892µs/call:
# 8 times (296µs+6.84ms) by Class::MOP::MiniTrait::apply at line 18 of /usr/local/lib/perl/5.10.0/Class/MOP/MiniTrait.pm, avg 892µs/call | |||
| 993 | 59 | 484µs | 8µs | my $self = shift; |
| 994 | ||||
| 995 | my %methods; | |||
| 996 | for my $class ( reverse $self->linearized_isa ) { # spent 122µs making 8 calls to Class::MOP::Class::linearized_isa, avg 15µs/call | |||
| 997 | my $meta = Class::MOP::Class->initialize($class); # spent 119µs making 9 calls to Class::MOP::Class::initialize, avg 13µs/call | |||
| 998 | ||||
| 999 | $methods{ $_->name } = $_ for $meta->_get_local_methods; # spent 6.46ms making 9 calls to Class::MOP::Mixin::HasMethods::_get_local_methods, avg 718µs/call
# spent 137µs making 56 calls to Class::MOP::Method::name, avg 2µs/call | |||
| 1000 | } | |||
| 1001 | ||||
| 1002 | return values %methods; | |||
| 1003 | } | |||
| 1004 | ||||
| 1005 | sub get_all_method_names { | |||
| 1006 | my $self = shift; | |||
| 1007 | my %uniq; | |||
| 1008 | return grep { !$uniq{$_}++ } map { Class::MOP::Class->initialize($_)->get_method_list } $self->linearized_isa; | |||
| 1009 | } | |||
| 1010 | ||||
| 1011 | # spent 3.22ms (347µs+2.88) within Class::MOP::Class::find_all_methods_by_name which was called 14 times, avg 230µs/call:
# 7 times (208µs+1.48ms) by Moose::Meta::Method::Constructor::_generate_BUILDALL at line 163 of /usr/local/lib/perl/5.10.0/Moose/Meta/Method/Constructor.pm, avg 241µs/call
# 7 times (139µs+1.40ms) by Moose::Meta::Method::Destructor::_initialize_body at line 81 of /usr/local/lib/perl/5.10.0/Moose/Meta/Method/Destructor.pm, avg 219µs/call | |||
| 1012 | 142 | 482µs | 3µs | my ($self, $method_name) = @_; |
| 1013 | (defined $method_name && length $method_name) | |||
| 1014 | || confess "You must define a method name to find"; | |||
| 1015 | my @methods; | |||
| 1016 | foreach my $class ($self->linearized_isa) { # spent 179µs making 14 calls to Class::MOP::Class::linearized_isa, avg 13µs/call | |||
| 1017 | # fetch the meta-class ... | |||
| 1018 | my $meta = Class::MOP::Class->initialize($class); # spent 386µs making 36 calls to Class::MOP::Class::initialize, avg 11µs/call | |||
| 1019 | push @methods => { # spent 2.31ms making 36 calls to Class::MOP::Mixin::HasMethods::has_method, avg 64µs/call | |||
| 1020 | name => $method_name, | |||
| 1021 | class => $class, | |||
| 1022 | code => $meta->get_method($method_name) | |||
| 1023 | } if $meta->has_method($method_name); | |||
| 1024 | } | |||
| 1025 | return @methods; | |||
| 1026 | } | |||
| 1027 | ||||
| 1028 | # spent 17.3ms (2.09+15.2) within Class::MOP::Class::find_next_method_by_name which was called 103 times, avg 168µs/call:
# 46 times (1.03ms+7.67ms) by Class::MOP::Class::__ANON__[/usr/local/lib/perl/5.10.0/Class/MOP/Class.pm:935] at line 914, avg 189µs/call
# 45 times (856µs+5.96ms) by Class::MOP::Method::Inlined::can_be_inlined at line 40 of /usr/local/lib/perl/5.10.0/Class/MOP/Method/Inlined.pm, avg 151µs/call
# 8 times (104µs+721µs) by Class::MOP::Method::Inlined::_uninlined_body at line 20 of /usr/local/lib/perl/5.10.0/Class/MOP/Method/Inlined.pm, avg 103µs/call
# 4 times (91µs+862µs) by Moose::Meta::Method::Overridden::new at line 23 of /usr/local/lib/perl/5.10.0/Moose/Meta/Method/Overridden.pm, avg 238µs/call | |||
| 1029 | 868 | 2.84ms | 3µs | my ($self, $method_name) = @_; |
| 1030 | (defined $method_name && length $method_name) | |||
| 1031 | || confess "You must define a method name to find"; | |||
| 1032 | my @cpl = $self->linearized_isa; # spent 1.27ms making 95 calls to Class::MOP::Class::linearized_isa, avg 13µs/call
# spent 164µs making 8 calls to Class::MOP::Class::Immutable::Moose::Meta::Class::linearized_isa, avg 21µs/call | |||
| 1033 | shift @cpl; # discard ourselves | |||
| 1034 | foreach my $class (@cpl) { | |||
| 1035 | my $method = Class::MOP::Class->initialize($class)->get_method($method_name); # spent 12.0ms making 175 calls to Class::MOP::Mixin::HasMethods::get_method, avg 68µs/call
# spent 1.82ms making 175 calls to Class::MOP::Class::initialize, avg 10µs/call | |||
| 1036 | return $method if defined $method; | |||
| 1037 | } | |||
| 1038 | return; | |||
| 1039 | } | |||
| 1040 | ||||
| 1041 | # spent 65µs within Class::MOP::Class::update_meta_instance_dependencies which was called 13 times, avg 5µs/call:
# 13 times (65µs+0s) by Class::MOP::Class::_superclasses_updated at line 844, avg 5µs/call | |||
| 1042 | 26 | 28µs | 1µs | my $self = shift; |
| 1043 | ||||
| 1044 | if ( $self->{meta_instance_dependencies} ) { | |||
| 1045 | return $self->add_meta_instance_dependencies; | |||
| 1046 | } | |||
| 1047 | } | |||
| 1048 | ||||
| 1049 | sub add_meta_instance_dependencies { | |||
| 1050 | my $self = shift; | |||
| 1051 | ||||
| 1052 | $self->remove_meta_instance_dependencies; | |||
| 1053 | ||||
| 1054 | my @attrs = $self->get_all_attributes(); | |||
| 1055 | ||||
| 1056 | my %seen; | |||
| 1057 | my @classes = grep { not $seen{ $_->name }++ } | |||
| 1058 | map { $_->associated_class } @attrs; | |||
| 1059 | ||||
| 1060 | foreach my $class (@classes) { | |||
| 1061 | $class->add_dependent_meta_instance($self); | |||
| 1062 | } | |||
| 1063 | ||||
| 1064 | $self->{meta_instance_dependencies} = \@classes; | |||
| 1065 | } | |||
| 1066 | ||||
| 1067 | sub remove_meta_instance_dependencies { | |||
| 1068 | my $self = shift; | |||
| 1069 | ||||
| 1070 | if ( my $classes = delete $self->{meta_instance_dependencies} ) { | |||
| 1071 | foreach my $class (@$classes) { | |||
| 1072 | $class->remove_dependent_meta_instance($self); | |||
| 1073 | } | |||
| 1074 | ||||
| 1075 | return $classes; | |||
| 1076 | } | |||
| 1077 | ||||
| 1078 | return; | |||
| 1079 | ||||
| 1080 | } | |||
| 1081 | ||||
| 1082 | sub add_dependent_meta_instance { | |||
| 1083 | my ( $self, $metaclass ) = @_; | |||
| 1084 | push @{ $self->{dependent_meta_instances} }, $metaclass; | |||
| 1085 | } | |||
| 1086 | ||||
| 1087 | sub remove_dependent_meta_instance { | |||
| 1088 | my ( $self, $metaclass ) = @_; | |||
| 1089 | my $name = $metaclass->name; | |||
| 1090 | @$_ = grep { $_->name ne $name } @$_ | |||
| 1091 | for $self->{dependent_meta_instances}; | |||
| 1092 | } | |||
| 1093 | ||||
| 1094 | # spent 1.50ms (927µs+576µs) within Class::MOP::Class::invalidate_meta_instances which was called 123 times, avg 12µs/call:
# 123 times (927µs+576µs) by Class::MOP::Class::_post_add_attribute at line 762, avg 12µs/call | |||
| 1095 | 369 | 982µs | 3µs | my $self = shift; |
| 1096 | $_->invalidate_meta_instance() | |||
| 1097 | for $self, @{ $self->{dependent_meta_instances} }; # spent 576µs making 123 calls to Class::MOP::Class::invalidate_meta_instance, avg 5µs/call | |||
| 1098 | } | |||
| 1099 | ||||
| 1100 | # spent 576µs within Class::MOP::Class::invalidate_meta_instance which was called 123 times, avg 5µs/call:
# 123 times (576µs+0s) by Class::MOP::Class::invalidate_meta_instances at line 1097, avg 5µs/call | |||
| 1101 | 246 | 274µs | 1µs | my $self = shift; |
| 1102 | undef $self->{_meta_instance}; | |||
| 1103 | } | |||
| 1104 | ||||
| 1105 | # check if we can reinitialize | |||
| 1106 | sub is_pristine { | |||
| 1107 | my $self = shift; | |||
| 1108 | ||||
| 1109 | # if any local attr is defined | |||
| 1110 | return if $self->get_attribute_list; | |||
| 1111 | ||||
| 1112 | # or any non-declared methods | |||
| 1113 | for my $method ( map { $self->get_method($_) } $self->get_method_list ) { | |||
| 1114 | return if $method->isa("Class::MOP::Method::Generated"); | |||
| 1115 | # FIXME do we need to enforce this too? return unless $method->isa( $self->method_metaclass ); | |||
| 1116 | } | |||
| 1117 | ||||
| 1118 | return 1; | |||
| 1119 | } | |||
| 1120 | ||||
| 1121 | ## Class closing | |||
| 1122 | ||||
| 1123 | 89 | 109µs | 1µs | # spent 303µs within Class::MOP::Class::is_mutable which was called 89 times, avg 3µs/call:
# 58 times (227µs+0s) by Class::MOP::Class::make_immutable at line 1147, avg 4µs/call
# 22 times (52µs+0s) at line 231 of /usr/local/lib/perl/5.10.0/Moose.pm, avg 2µs/call
# 9 times (24µs+0s) at line 688 of /usr/local/lib/perl/5.10.0/Moose/Util/TypeConstraints.pm, avg 3µs/call |
| 1124 | 45 | 49µs | 1µs | # spent 141µs within Class::MOP::Class::is_immutable which was called 45 times, avg 3µs/call:
# 45 times (141µs+0s) by Class::MOP::Class::_real_ref_name at line 122, avg 3µs/call |
| 1125 | ||||
| 1126 | sub immutable_options { %{ $_[0]{__immutable}{options} || {} } } | |||
| 1127 | ||||
| 1128 | # spent 2.02ms (827µs+1.19) within Class::MOP::Class::_immutable_options which was called 58 times, avg 35µs/call:
# 51 times (703µs+1.03ms) by Class::MOP::Class::make_immutable at line 1148, avg 34µs/call
# 7 times (124µs+161µs) by Moose::Meta::Class::_immutable_options at line 454 of /usr/local/lib/perl/5.10.0/Moose/Meta/Class.pm, avg 41µs/call | |||
| 1129 | 116 | 1.18ms | 10µs | my ( $self, @args ) = @_; |
| 1130 | ||||
| 1131 | return ( | |||
| 1132 | inline_accessors => 1, # spent 358µs making 51 calls to Class::MOP::Class::immutable_trait, avg 7µs/call
# spent 279µs making 58 calls to Class::MOP::Class::constructor_name, avg 5µs/call
# spent 231µs making 51 calls to Class::MOP::Class::constructor_class, avg 5µs/call
# spent 203µs making 51 calls to Class::MOP::Class::destructor_class, avg 4µs/call
# spent 49µs making 7 calls to Moose::Meta::Class::immutable_trait, avg 7µs/call
# spent 37µs making 7 calls to Moose::Meta::Class::constructor_class, avg 5µs/call
# spent 34µs making 7 calls to Moose::Meta::Class::destructor_class, avg 5µs/call | |||
| 1133 | inline_constructor => 1, | |||
| 1134 | inline_destructor => 0, | |||
| 1135 | debug => 0, | |||
| 1136 | immutable_trait => $self->immutable_trait, | |||
| 1137 | constructor_name => $self->constructor_name, | |||
| 1138 | constructor_class => $self->constructor_class, | |||
| 1139 | destructor_class => $self->destructor_class, | |||
| 1140 | @args, | |||
| 1141 | ); | |||
| 1142 | } | |||
| 1143 | ||||
| 1144 | # spent 205ms (1.00+204) within Class::MOP::Class::make_immutable which was called 58 times, avg 3.53ms/call:
# 22 times (408µs+79.0ms) at line 236 of /usr/local/lib/perl/5.10.0/Moose.pm, avg 3.61ms/call
# 13 times (170µs+25.3ms) at line 698 of /usr/local/lib/perl/5.10.0/Class/MOP.pm, avg 1.96ms/call
# 9 times (169µs+34.3ms) at line 693 of /usr/local/lib/perl/5.10.0/Moose/Util/TypeConstraints.pm, avg 3.83ms/call
# 4 times (52µs+661µs) at line 723 of /usr/local/lib/perl/5.10.0/Class/MOP.pm, avg 178µs/call
# 2 times (34µs+-34µs) by Class::MOP::Class::_immutable_metaclass at line 1221, avg 0s/call
# once (34µs+30.9ms) at line 104 of /home/tamil/util/marc-moose/lib/MARC/Moose/Record.pm
# once (16µs+9.28ms) at line 268 of /usr/local/lib/perl/5.10.0/Moose.pm
# once (21µs+6.65ms) at line 39 of /home/tamil/util/marc-moose/lib/MARC/Moose/Field/Std.pm
# once (17µs+4.05ms) at line 17 of /home/tamil/util/marc-moose/lib/MARC/Moose/Field/Control.pm
# once (20µs+3.93ms) at line 43 of /home/tamil/util/marc-moose/lib/MARC/Moose/Parser/MarcxmlSax.pm
# once (19µs+3.52ms) at line 64 of /home/tamil/util/marc-moose/lib/MARC/Moose/Parser/Marcxml.pm
# once (22µs+3.17ms) at line 16 of /home/tamil/util/marc-moose/lib/MARC/Moose/Field.pm
# once (19µs+2.99ms) at line 30 of /home/tamil/util/marc-moose/lib/MARC/Moose/Parser.pm | |||
| 1145 | 290 | 1.46ms | 5µs | my ( $self, @args ) = @_; |
| 1146 | ||||
| 1147 | if ( $self->is_mutable ) { # spent 227µs making 58 calls to Class::MOP::Class::is_mutable, avg 4µs/call | |||
| 1148 | $self->_initialize_immutable( $self->_immutable_options(@args) ); # spent 146ms making 58 calls to Class::MOP::Class::_initialize_immutable, avg 2.51ms/call
# spent 1.73ms making 51 calls to Class::MOP::Class::_immutable_options, avg 34µs/call
# spent 361µs making 7 calls to Moose::Meta::Class::_immutable_options, avg 52µs/call | |||
| 1149 | $self->_rebless_as_immutable(@args); # spent 56.3ms making 58 calls to Class::MOP::Class::_rebless_as_immutable, avg 965µs/call, max recursion depth 1 | |||
| 1150 | return $self; | |||
| 1151 | } | |||
| 1152 | else { | |||
| 1153 | return; | |||
| 1154 | } | |||
| 1155 | } | |||
| 1156 | ||||
| 1157 | sub make_mutable { | |||
| 1158 | my $self = shift; | |||
| 1159 | ||||
| 1160 | if ( $self->is_immutable ) { | |||
| 1161 | my @args = $self->immutable_options; | |||
| 1162 | $self->_rebless_as_mutable(); | |||
| 1163 | $self->_remove_inlined_code(@args); | |||
| 1164 | delete $self->{__immutable}; | |||
| 1165 | return $self; | |||
| 1166 | } | |||
| 1167 | else { | |||
| 1168 | return; | |||
| 1169 | } | |||
| 1170 | } | |||
| 1171 | ||||
| 1172 | # spent 56.0ms (777µs+55.2) within Class::MOP::Class::_rebless_as_immutable which was called 58 times, avg 965µs/call:
# 58 times (777µs+55.2ms) by Class::MOP::Class::make_immutable at line 1149, avg 965µs/call | |||
| 1173 | 174 | 772µs | 4µs | my ( $self, @args ) = @_; |
| 1174 | ||||
| 1175 | $self->{__immutable}{original_class} = ref $self; | |||
| 1176 | ||||
| 1177 | bless $self => $self->_immutable_metaclass(@args); # spent 55.5ms making 58 calls to Class::MOP::Class::_immutable_metaclass, avg 952µs/call, max recursion depth 1 | |||
| 1178 | } | |||
| 1179 | ||||
| 1180 | # spent 55.2ms (1.26+54.0) within Class::MOP::Class::_immutable_metaclass which was called 58 times, avg 952µs/call:
# 58 times (1.26ms+54.0ms) by Class::MOP::Class::_rebless_as_immutable at line 1177, avg 952µs/call | |||
| 1181 | 474 | 2.11ms | 4µs | my ( $self, %args ) = @_; |
| 1182 | ||||
| 1183 | if ( my $class = $args{immutable_metaclass} ) { | |||
| 1184 | return $class; | |||
| 1185 | } | |||
| 1186 | ||||
| 1187 | my $trait = $args{immutable_trait} = $self->immutable_trait # spent 209µs making 51 calls to Class::MOP::Class::immutable_trait, avg 4µs/call
# spent 38µs making 7 calls to Moose::Meta::Class::immutable_trait, avg 5µs/call | |||
| 1188 | || confess "no immutable trait specified for $self"; | |||
| 1189 | ||||
| 1190 | my $meta = $self->meta; # spent 1.21ms making 58 calls to Class::MOP::Object::meta, avg 21µs/call | |||
| 1191 | my $meta_attr = $meta->find_attribute_by_name("immutable_trait"); # spent 3.79ms making 58 calls to Class::MOP::Class::find_attribute_by_name, avg 65µs/call | |||
| 1192 | ||||
| 1193 | my $class_name; | |||
| 1194 | ||||
| 1195 | if ( $meta_attr and $trait eq $meta_attr->default ) { # spent 340µs making 58 calls to Class::MOP::Mixin::AttributeCore::default, avg 6µs/call | |||
| 1196 | # if the trait is the same as the default we try and pick a | |||
| 1197 | # predictable name for the immutable metaclass | |||
| 1198 | $class_name = 'Class::MOP::Class::Immutable::' . ref($self); | |||
| 1199 | } | |||
| 1200 | else { | |||
| 1201 | $class_name = join '::', 'Class::MOP::Class::Immutable::CustomTrait', | |||
| 1202 | $trait, 'ForMetaClass', ref($self); | |||
| 1203 | } | |||
| 1204 | ||||
| 1205 | return $class_name # spent 406µs making 58 calls to Class::MOP::is_class_loaded, avg 7µs/call | |||
| 1206 | if Class::MOP::is_class_loaded($class_name); | |||
| 1207 | ||||
| 1208 | # If the metaclass is a subclass of CMOP::Class which has had | |||
| 1209 | # metaclass roles applied (via Moose), then we want to make sure | |||
| 1210 | # that we preserve that anonymous class (see Fey::ORM for an | |||
| 1211 | # example of where this matters). | |||
| 1212 | my $meta_name = $meta->_real_ref_name; # spent 40µs making 2 calls to Class::MOP::Class::_real_ref_name, avg 20µs/call | |||
| 1213 | ||||
| 1214 | my $immutable_meta = $meta_name->create( # spent 3.13ms making 2 calls to Class::MOP::Class::create, avg 1.56ms/call | |||
| 1215 | $class_name, | |||
| 1216 | superclasses => [ ref $self ], | |||
| 1217 | ); | |||
| 1218 | ||||
| 1219 | Class::MOP::MiniTrait::apply( $immutable_meta, $trait ); # spent 44.6ms making 2 calls to Class::MOP::MiniTrait::apply, avg 22.3ms/call | |||
| 1220 | ||||
| 1221 | $immutable_meta->make_immutable( # spent 452µs making 2 calls to Class::MOP::Class::make_immutable, avg 0s/call, max recursion depth 1 | |||
| 1222 | inline_constructor => 0, | |||
| 1223 | inline_accessors => 0, | |||
| 1224 | ); | |||
| 1225 | ||||
| 1226 | return $class_name; | |||
| 1227 | } | |||
| 1228 | ||||
| 1229 | sub _remove_inlined_code { | |||
| 1230 | my $self = shift; | |||
| 1231 | ||||
| 1232 | $self->remove_method( $_->name ) for $self->_inlined_methods; | |||
| 1233 | ||||
| 1234 | delete $self->{__immutable}{inlined_methods}; | |||
| 1235 | } | |||
| 1236 | ||||
| 1237 | sub _inlined_methods { @{ $_[0]{__immutable}{inlined_methods} || [] } } | |||
| 1238 | ||||
| 1239 | sub _add_inlined_method { | |||
| 1240 | 90 | 246µs | 3µs | my ( $self, $method ) = @_; |
| 1241 | ||||
| 1242 | push @{ $self->{__immutable}{inlined_methods} ||= [] }, $method; | |||
| 1243 | } | |||
| 1244 | ||||
| 1245 | # spent 146ms (1.29+144) within Class::MOP::Class::_initialize_immutable which was called 58 times, avg 2.51ms/call:
# 58 times (1.29ms+144ms) by Class::MOP::Class::make_immutable at line 1148, avg 2.51ms/call | |||
| 1246 | 174 | 1.27ms | 7µs | my ( $self, %args ) = @_; |
| 1247 | ||||
| 1248 | $self->{__immutable}{options} = \%args; | |||
| 1249 | $self->_install_inlined_code(%args); # spent 144ms making 58 calls to Class::MOP::Class::_install_inlined_code, avg 2.49ms/call | |||
| 1250 | } | |||
| 1251 | ||||
| 1252 | # spent 144ms (1.04+143) within Class::MOP::Class::_install_inlined_code which was called 58 times, avg 2.49ms/call:
# 58 times (1.04ms+143ms) by Class::MOP::Class::_initialize_immutable at line 1249, avg 2.49ms/call | |||
| 1253 | 232 | 1.10ms | 5µs | my ( $self, %args ) = @_; |
| 1254 | ||||
| 1255 | # FIXME | |||
| 1256 | $self->_inline_accessors(%args) if $args{inline_accessors}; # spent 51.3ms making 32 calls to Class::MOP::Class::_inline_accessors, avg 1.60ms/call | |||
| 1257 | $self->_inline_constructor(%args) if $args{inline_constructor}; # spent 83.1ms making 38 calls to Class::MOP::Class::_inline_constructor, avg 2.19ms/call | |||
| 1258 | $self->_inline_destructor(%args) if $args{inline_destructor}; # spent 8.92ms making 7 calls to Class::MOP::Class::_inline_destructor, avg 1.27ms/call | |||
| 1259 | } | |||
| 1260 | ||||
| 1261 | sub _rebless_as_mutable { | |||
| 1262 | my $self = shift; | |||
| 1263 | ||||
| 1264 | bless $self, $self->_get_mutable_metaclass_name; | |||
| 1265 | ||||
| 1266 | return $self; | |||
| 1267 | } | |||
| 1268 | ||||
| 1269 | # spent 51.3ms (477µs+50.8) within Class::MOP::Class::_inline_accessors which was called 32 times, avg 1.60ms/call:
# 32 times (477µs+50.8ms) by Class::MOP::Class::_install_inlined_code at line 1256, avg 1.60ms/call | |||
| 1270 | 132 | 800µs | 6µs | my $self = shift; |
| 1271 | ||||
| 1272 | foreach my $attr_name ( $self->get_attribute_list ) { # spent 391µs making 32 calls to Class::MOP::Mixin::HasAttributes::get_attribute_list, avg 12µs/call | |||
| 1273 | $self->get_attribute($attr_name)->install_accessors(1); # spent 47.5ms making 67 calls to Class::MOP::Attribute::install_accessors, avg 710µs/call
# spent 1.96ms making 1 call to Moose::Meta::Attribute::install_accessors
# spent 920µs making 68 calls to Class::MOP::Mixin::HasAttributes::get_attribute, avg 14µs/call | |||
| 1274 | } | |||
| 1275 | } | |||
| 1276 | ||||
| 1277 | # spent 83.1ms (1.10+82.0) within Class::MOP::Class::_inline_constructor which was called 38 times, avg 2.19ms/call:
# 38 times (1.10ms+82.0ms) by Class::MOP::Class::_install_inlined_code at line 1257, avg 2.19ms/call | |||
| 1278 | 380 | 1.87ms | 5µs | my ( $self, %args ) = @_; |
| 1279 | ||||
| 1280 | my $name = $args{constructor_name}; | |||
| 1281 | # A class may not even have a constructor, and that's okay. | |||
| 1282 | return unless defined $name; | |||
| 1283 | ||||
| 1284 | if ( $self->has_method($name) && !$args{replace_constructor} ) { # spent 3.26ms making 38 calls to Class::MOP::Mixin::HasMethods::has_method, avg 86µs/call | |||
| 1285 | my $class = $self->name; | |||
| 1286 | warn "Not inlining a constructor for $class since it defines" | |||
| 1287 | . " its own constructor.\n" | |||
| 1288 | . "If you are certain you don't need to inline your" | |||
| 1289 | . " constructor, specify inline_constructor => 0 in your" | |||
| 1290 | . " call to $class->meta->make_immutable\n"; | |||
| 1291 | return; | |||
| 1292 | } | |||
| 1293 | ||||
| 1294 | my $constructor_class = $args{constructor_class}; | |||
| 1295 | ||||
| 1296 | Class::MOP::load_class($constructor_class); # spent 2.52ms making 38 calls to Class::MOP::load_class, avg 63µs/call, max recursion depth 1 | |||
| 1297 | ||||
| 1298 | my $constructor = $constructor_class->new( # spent 47.7ms making 31 calls to Class::MOP::Method::Constructor::new, avg 1.54ms/call
# spent 14.1ms making 7 calls to Moose::Meta::Method::Constructor::new, avg 2.02ms/call
# spent 131µs making 38 calls to Class::MOP::Package::name, avg 3µs/call | |||
| 1299 | options => \%args, | |||
| 1300 | metaclass => $self, | |||
| 1301 | is_inline => 1, | |||
| 1302 | package_name => $self->name, | |||
| 1303 | name => $name, | |||
| 1304 | ); | |||
| 1305 | ||||
| 1306 | if ( $args{replace_constructor} or $constructor->can_be_inlined ) { # spent 8.99ms making 38 calls to Class::MOP::Method::Inlined::can_be_inlined, avg 237µs/call | |||
| 1307 | $self->add_method( $name => $constructor ); # spent 4.98ms making 38 calls to Class::MOP::Mixin::HasMethods::add_method, avg 131µs/call | |||
| 1308 | $self->_add_inlined_method($constructor); # spent 366µs making 38 calls to Class::MOP::Class::_add_inlined_method, avg 10µs/call | |||
| 1309 | } | |||
| 1310 | } | |||
| 1311 | ||||
| 1312 | # spent 8.92ms (248µs+8.67) within Class::MOP::Class::_inline_destructor which was called 7 times, avg 1.27ms/call:
# 7 times (248µs+8.67ms) by Class::MOP::Class::_install_inlined_code at line 1258, avg 1.27ms/call | |||
| 1313 | 70 | 397µs | 6µs | my ( $self, %args ) = @_; |
| 1314 | ||||
| 1315 | ( exists $args{destructor_class} && defined $args{destructor_class} ) | |||
| 1316 | || confess "The 'inline_destructor' option is present, but " | |||
| 1317 | . "no destructor class was specified"; | |||
| 1318 | ||||
| 1319 | if ( $self->has_method('DESTROY') && ! $args{replace_destructor} ) { # spent 558µs making 7 calls to Class::MOP::Mixin::HasMethods::has_method, avg 80µs/call | |||
| 1320 | my $class = $self->name; | |||
| 1321 | warn "Not inlining a destructor for $class since it defines" | |||
| 1322 | . " its own destructor.\n"; | |||
| 1323 | return; | |||
| 1324 | } | |||
| 1325 | ||||
| 1326 | my $destructor_class = $args{destructor_class}; | |||
| 1327 | ||||
| 1328 | Class::MOP::load_class($destructor_class); # spent 469µs making 7 calls to Class::MOP::load_class, avg 48µs/call, max recursion depth 1 | |||
| 1329 | ||||
| 1330 | return unless $destructor_class->is_needed($self); # spent 1.79ms making 7 calls to Moose::Meta::Method::Destructor::is_needed, avg 255µs/call | |||
| 1331 | ||||
| 1332 | my $destructor = $destructor_class->new( # spent 3.67ms making 7 calls to Moose::Meta::Method::Destructor::new, avg 524µs/call
# spent 23µs making 7 calls to Class::MOP::Package::name, avg 3µs/call | |||
| 1333 | options => \%args, | |||
| 1334 | metaclass => $self, | |||
| 1335 | package_name => $self->name, | |||
| 1336 | name => 'DESTROY' | |||
| 1337 | ); | |||
| 1338 | ||||
| 1339 | if ( $args{replace_destructor} or $destructor->can_be_inlined ) { # spent 1.19ms making 7 calls to Class::MOP::Method::Inlined::can_be_inlined, avg 170µs/call | |||
| 1340 | $self->add_method( 'DESTROY' => $destructor ); # spent 929µs making 7 calls to Class::MOP::Mixin::HasMethods::add_method, avg 133µs/call | |||
| 1341 | $self->_add_inlined_method($destructor); # spent 45µs making 7 calls to Class::MOP::Class::_add_inlined_method, avg 6µs/call | |||
| 1342 | } | |||
| 1343 | } | |||
| 1344 | ||||
| 1345 | 1 | 16µs | 16µs | 1; |
| 1346 | ||||
| 1347 | __END__ | |||
| 1348 | ||||
| 1349 | =pod | |||
| 1350 | ||||
| 1351 | =head1 NAME | |||
| 1352 | ||||
| 1353 | Class::MOP::Class - Class Meta Object | |||
| 1354 | ||||
| 1355 | =head1 SYNOPSIS | |||
| 1356 | ||||
| 1357 | # assuming that class Foo | |||
| 1358 | # has been defined, you can | |||
| 1359 | ||||
| 1360 | # use this for introspection ... | |||
| 1361 | ||||
| 1362 | # add a method to Foo ... | |||
| 1363 | Foo->meta->add_method( 'bar' => sub {...} ) | |||
| 1364 | ||||
| 1365 | # get a list of all the classes searched | |||
| 1366 | # the method dispatcher in the correct order | |||
| 1367 | Foo->meta->class_precedence_list() | |||
| 1368 | ||||
| 1369 | # remove a method from Foo | |||
| 1370 | Foo->meta->remove_method('bar'); | |||
| 1371 | ||||
| 1372 | # or use this to actually create classes ... | |||
| 1373 | ||||
| 1374 | Class::MOP::Class->create( | |||
| 1375 | 'Bar' => ( | |||
| 1376 | version => '0.01', | |||
| 1377 | superclasses => ['Foo'], | |||
| 1378 | attributes => [ | |||
| 1379 | Class::MOP::Attribute->new('$bar'), | |||
| 1380 | Class::MOP::Attribute->new('$baz'), | |||
| 1381 | ], | |||
| 1382 | methods => { | |||
| 1383 | calculate_bar => sub {...}, | |||
| 1384 | construct_baz => sub {...} | |||
| 1385 | } | |||
| 1386 | ) | |||
| 1387 | ); | |||
| 1388 | ||||
| 1389 | =head1 DESCRIPTION | |||
| 1390 | ||||
| 1391 | The Class Protocol is the largest and most complex part of the | |||
| 1392 | Class::MOP meta-object protocol. It controls the introspection and | |||
| 1393 | manipulation of Perl 5 classes, and it can create them as well. The | |||
| 1394 | best way to understand what this module can do is to read the | |||
| 1395 | documentation for each of its methods. | |||
| 1396 | ||||
| 1397 | =head1 INHERITANCE | |||
| 1398 | ||||
| 1399 | C<Class::MOP::Class> is a subclass of L<Class::MOP::Module>. | |||
| 1400 | ||||
| 1401 | =head1 METHODS | |||
| 1402 | ||||
| 1403 | =head2 Class construction | |||
| 1404 | ||||
| 1405 | These methods all create new C<Class::MOP::Class> objects. These | |||
| 1406 | objects can represent existing classes or they can be used to create | |||
| 1407 | new classes from scratch. | |||
| 1408 | ||||
| 1409 | The metaclass object for a given class is a singleton. If you attempt | |||
| 1410 | to create a metaclass for the same class twice, you will just get the | |||
| 1411 | existing object. | |||
| 1412 | ||||
| 1413 | =over 4 | |||
| 1414 | ||||
| 1415 | =item B<< Class::MOP::Class->create($package_name, %options) >> | |||
| 1416 | ||||
| 1417 | This method creates a new C<Class::MOP::Class> object with the given | |||
| 1418 | package name. It accepts a number of options: | |||
| 1419 | ||||
| 1420 | =over 8 | |||
| 1421 | ||||
| 1422 | =item * version | |||
| 1423 | ||||
| 1424 | An optional version number for the newly created package. | |||
| 1425 | ||||
| 1426 | =item * authority | |||
| 1427 | ||||
| 1428 | An optional authority for the newly created package. | |||
| 1429 | ||||
| 1430 | =item * superclasses | |||
| 1431 | ||||
| 1432 | An optional array reference of superclass names. | |||
| 1433 | ||||
| 1434 | =item * methods | |||
| 1435 | ||||
| 1436 | An optional hash reference of methods for the class. The keys of the | |||
| 1437 | hash reference are method names and values are subroutine references. | |||
| 1438 | ||||
| 1439 | =item * attributes | |||
| 1440 | ||||
| 1441 | An optional array reference of L<Class::MOP::Attribute> objects. | |||
| 1442 | ||||
| 1443 | =item * meta_name | |||
| 1444 | ||||
| 1445 | Specifies the name to install the C<meta> method for this class under. | |||
| 1446 | If it is not passed, C<meta> is assumed, and if C<undef> is explicitly | |||
| 1447 | given, no meta method will be installed. | |||
| 1448 | ||||
| 1449 | =back | |||
| 1450 | ||||
| 1451 | =item B<< Class::MOP::Class->create_anon_class(%options) >> | |||
| 1452 | ||||
| 1453 | This method works just like C<< Class::MOP::Class->create >> but it | |||
| 1454 | creates an "anonymous" class. In fact, the class does have a name, but | |||
| 1455 | that name is a unique name generated internally by this module. | |||
| 1456 | ||||
| 1457 | It accepts the same C<superclasses>, C<methods>, and C<attributes> | |||
| 1458 | parameters that C<create> accepts. | |||
| 1459 | ||||
| 1460 | Anonymous classes are destroyed once the metaclass they are attached | |||
| 1461 | to goes out of scope, and will be removed from Perl's internal symbol | |||
| 1462 | table. | |||
| 1463 | ||||
| 1464 | All instances of an anonymous class keep a special reference to the | |||
| 1465 | metaclass object, which prevents the metaclass from going out of scope | |||
| 1466 | while any instances exist. | |||
| 1467 | ||||
| 1468 | This only works if the instance is based on a hash reference, however. | |||
| 1469 | ||||
| 1470 | =item B<< Class::MOP::Class->initialize($package_name, %options) >> | |||
| 1471 | ||||
| 1472 | This method will initialize a C<Class::MOP::Class> object for the | |||
| 1473 | named package. Unlike C<create>, this method I<will not> create a new | |||
| 1474 | class. | |||
| 1475 | ||||
| 1476 | The purpose of this method is to retrieve a C<Class::MOP::Class> | |||
| 1477 | object for introspecting an existing class. | |||
| 1478 | ||||
| 1479 | If an existing C<Class::MOP::Class> object exists for the named | |||
| 1480 | package, it will be returned, and any options provided will be | |||
| 1481 | ignored! | |||
| 1482 | ||||
| 1483 | If the object does not yet exist, it will be created. | |||
| 1484 | ||||
| 1485 | The valid options that can be passed to this method are | |||
| 1486 | C<attribute_metaclass>, C<method_metaclass>, | |||
| 1487 | C<wrapped_method_metaclass>, and C<instance_metaclass>. These are all | |||
| 1488 | optional, and default to the appropriate class in the C<Class::MOP> | |||
| 1489 | distribution. | |||
| 1490 | ||||
| 1491 | =back | |||
| 1492 | ||||
| 1493 | =head2 Object instance construction and cloning | |||
| 1494 | ||||
| 1495 | These methods are all related to creating and/or cloning object | |||
| 1496 | instances. | |||
| 1497 | ||||
| 1498 | =over 4 | |||
| 1499 | ||||
| 1500 | =item B<< $metaclass->clone_object($instance, %params) >> | |||
| 1501 | ||||
| 1502 | This method clones an existing object instance. Any parameters you | |||
| 1503 | provide are will override existing attribute values in the object. | |||
| 1504 | ||||
| 1505 | This is a convenience method for cloning an object instance, then | |||
| 1506 | blessing it into the appropriate package. | |||
| 1507 | ||||
| 1508 | You could implement a clone method in your class, using this method: | |||
| 1509 | ||||
| 1510 | sub clone { | |||
| 1511 | my ($self, %params) = @_; | |||
| 1512 | $self->meta->clone_object($self, %params); | |||
| 1513 | } | |||
| 1514 | ||||
| 1515 | =item B<< $metaclass->rebless_instance($instance, %params) >> | |||
| 1516 | ||||
| 1517 | This method changes the class of C<$instance> to the metaclass's class. | |||
| 1518 | ||||
| 1519 | You can only rebless an instance into a subclass of its current | |||
| 1520 | class. If you pass any additional parameters, these will be treated | |||
| 1521 | like constructor parameters and used to initialize the object's | |||
| 1522 | attributes. Any existing attributes that are already set will be | |||
| 1523 | overwritten. | |||
| 1524 | ||||
| 1525 | Before reblessing the instance, this method will call | |||
| 1526 | C<rebless_instance_away> on the instance's current metaclass. This method | |||
| 1527 | will be passed the instance, the new metaclass, and any parameters | |||
| 1528 | specified to C<rebless_instance>. By default, C<rebless_instance_away> | |||
| 1529 | does nothing; it is merely a hook. | |||
| 1530 | ||||
| 1531 | =item B<< $metaclass->rebless_instance_back($instance) >> | |||
| 1532 | ||||
| 1533 | Does the same thing as C<rebless_instance>, except that you can only | |||
| 1534 | rebless an instance into one of its superclasses. Any attributes that | |||
| 1535 | do not exist in the superclass will be deinitialized. | |||
| 1536 | ||||
| 1537 | This is a much more dangerous operation than C<rebless_instance>, | |||
| 1538 | especially when multiple inheritance is involved, so use this carefully! | |||
| 1539 | ||||
| 1540 | =item B<< $metaclass->new_object(%params) >> | |||
| 1541 | ||||
| 1542 | This method is used to create a new object of the metaclass's | |||
| 1543 | class. Any parameters you provide are used to initialize the | |||
| 1544 | instance's attributes. A special C<__INSTANCE__> key can be passed to | |||
| 1545 | provide an already generated instance, rather than having Class::MOP | |||
| 1546 | generate it for you. This is mostly useful for using Class::MOP with | |||
| 1547 | foreign classes which generate instances using their own constructors. | |||
| 1548 | ||||
| 1549 | =item B<< $metaclass->instance_metaclass >> | |||
| 1550 | ||||
| 1551 | Returns the class name of the instance metaclass. See | |||
| 1552 | L<Class::MOP::Instance> for more information on the instance | |||
| 1553 | metaclass. | |||
| 1554 | ||||
| 1555 | =item B<< $metaclass->get_meta_instance >> | |||
| 1556 | ||||
| 1557 | Returns an instance of the C<instance_metaclass> to be used in the | |||
| 1558 | construction of a new instance of the class. | |||
| 1559 | ||||
| 1560 | =item B<< $metaclass->inline_create_instance($class_var) >> | |||
| 1561 | ||||
| 1562 | =item B<< $metaclass->inline_rebless_instance($instance_var, $class_var) >> | |||
| 1563 | ||||
| 1564 | These methods takes variable names, and use them to create an inline snippet | |||
| 1565 | of code that will create a new instance of the class. | |||
| 1566 | ||||
| 1567 | =back | |||
| 1568 | ||||
| 1569 | =head2 Informational predicates | |||
| 1570 | ||||
| 1571 | These are a few predicate methods for asking information about the | |||
| 1572 | class itself. | |||
| 1573 | ||||
| 1574 | =over 4 | |||
| 1575 | ||||
| 1576 | =item B<< $metaclass->is_anon_class >> | |||
| 1577 | ||||
| 1578 | This returns true if the class was created by calling C<< | |||
| 1579 | Class::MOP::Class->create_anon_class >>. | |||
| 1580 | ||||
| 1581 | =item B<< $metaclass->is_mutable >> | |||
| 1582 | ||||
| 1583 | This returns true if the class is still mutable. | |||
| 1584 | ||||
| 1585 | =item B<< $metaclass->is_immutable >> | |||
| 1586 | ||||
| 1587 | This returns true if the class has been made immutable. | |||
| 1588 | ||||
| 1589 | =item B<< $metaclass->is_pristine >> | |||
| 1590 | ||||
| 1591 | A class is I<not> pristine if it has non-inherited attributes or if it | |||
| 1592 | has any generated methods. | |||
| 1593 | ||||
| 1594 | =back | |||
| 1595 | ||||
| 1596 | =head2 Inheritance Relationships | |||
| 1597 | ||||
| 1598 | =over 4 | |||
| 1599 | ||||
| 1600 | =item B<< $metaclass->superclasses(@superclasses) >> | |||
| 1601 | ||||
| 1602 | This is a read-write accessor which represents the superclass | |||
| 1603 | relationships of the metaclass's class. | |||
| 1604 | ||||
| 1605 | This is basically sugar around getting and setting C<@ISA>. | |||
| 1606 | ||||
| 1607 | =item B<< $metaclass->class_precedence_list >> | |||
| 1608 | ||||
| 1609 | This returns a list of all of the class's ancestor classes. The | |||
| 1610 | classes are returned in method dispatch order. | |||
| 1611 | ||||
| 1612 | =item B<< $metaclass->linearized_isa >> | |||
| 1613 | ||||
| 1614 | This returns a list based on C<class_precedence_list> but with all | |||
| 1615 | duplicates removed. | |||
| 1616 | ||||
| 1617 | =item B<< $metaclass->subclasses >> | |||
| 1618 | ||||
| 1619 | This returns a list of all subclasses for this class, even indirect | |||
| 1620 | subclasses. | |||
| 1621 | ||||
| 1622 | =item B<< $metaclass->direct_subclasses >> | |||
| 1623 | ||||
| 1624 | This returns a list of immediate subclasses for this class, which does not | |||
| 1625 | include indirect subclasses. | |||
| 1626 | ||||
| 1627 | =back | |||
| 1628 | ||||
| 1629 | =head2 Method introspection and creation | |||
| 1630 | ||||
| 1631 | These methods allow you to introspect a class's methods, as well as | |||
| 1632 | add, remove, or change methods. | |||
| 1633 | ||||
| 1634 | Determining what is truly a method in a Perl 5 class requires some | |||
| 1635 | heuristics (aka guessing). | |||
| 1636 | ||||
| 1637 | Methods defined outside the package with a fully qualified name (C<sub | |||
| 1638 | Package::name { ... }>) will be included. Similarly, methods named | |||
| 1639 | with a fully qualified name using L<Sub::Name> are also included. | |||
| 1640 | ||||
| 1641 | However, we attempt to ignore imported functions. | |||
| 1642 | ||||
| 1643 | Ultimately, we are using heuristics to determine what truly is a | |||
| 1644 | method in a class, and these heuristics may get the wrong answer in | |||
| 1645 | some edge cases. However, for most "normal" cases the heuristics work | |||
| 1646 | correctly. | |||
| 1647 | ||||
| 1648 | =over 4 | |||
| 1649 | ||||
| 1650 | =item B<< $metaclass->get_method($method_name) >> | |||
| 1651 | ||||
| 1652 | This will return a L<Class::MOP::Method> for the specified | |||
| 1653 | C<$method_name>. If the class does not have the specified method, it | |||
| 1654 | returns C<undef> | |||
| 1655 | ||||
| 1656 | =item B<< $metaclass->has_method($method_name) >> | |||
| 1657 | ||||
| 1658 | Returns a boolean indicating whether or not the class defines the | |||
| 1659 | named method. It does not include methods inherited from parent | |||
| 1660 | classes. | |||
| 1661 | ||||
| 1662 | =item B<< $metaclass->get_method_list >> | |||
| 1663 | ||||
| 1664 | This will return a list of method I<names> for all methods defined in | |||
| 1665 | this class. | |||
| 1666 | ||||
| 1667 | =item B<< $metaclass->add_method($method_name, $method) >> | |||
| 1668 | ||||
| 1669 | This method takes a method name and a subroutine reference, and adds | |||
| 1670 | the method to the class. | |||
| 1671 | ||||
| 1672 | The subroutine reference can be a L<Class::MOP::Method>, and you are | |||
| 1673 | strongly encouraged to pass a meta method object instead of a code | |||
| 1674 | reference. If you do so, that object gets stored as part of the | |||
| 1675 | class's method map directly. If not, the meta information will have to | |||
| 1676 | be recreated later, and may be incorrect. | |||
| 1677 | ||||
| 1678 | If you provide a method object, this method will clone that object if | |||
| 1679 | the object's package name does not match the class name. This lets us | |||
| 1680 | track the original source of any methods added from other classes | |||
| 1681 | (notably Moose roles). | |||
| 1682 | ||||
| 1683 | =item B<< $metaclass->remove_method($method_name) >> | |||
| 1684 | ||||
| 1685 | Remove the named method from the class. This method returns the | |||
| 1686 | L<Class::MOP::Method> object for the method. | |||
| 1687 | ||||
| 1688 | =item B<< $metaclass->method_metaclass >> | |||
| 1689 | ||||
| 1690 | Returns the class name of the method metaclass, see | |||
| 1691 | L<Class::MOP::Method> for more information on the method metaclass. | |||
| 1692 | ||||
| 1693 | =item B<< $metaclass->wrapped_method_metaclass >> | |||
| 1694 | ||||
| 1695 | Returns the class name of the wrapped method metaclass, see | |||
| 1696 | L<Class::MOP::Method::Wrapped> for more information on the wrapped | |||
| 1697 | method metaclass. | |||
| 1698 | ||||
| 1699 | =item B<< $metaclass->get_all_methods >> | |||
| 1700 | ||||
| 1701 | This will traverse the inheritance hierarchy and return a list of all | |||
| 1702 | the L<Class::MOP::Method> objects for this class and its parents. | |||
| 1703 | ||||
| 1704 | =item B<< $metaclass->find_method_by_name($method_name) >> | |||
| 1705 | ||||
| 1706 | This will return a L<Class::MOP::Method> for the specified | |||
| 1707 | C<$method_name>. If the class does not have the specified method, it | |||
| 1708 | returns C<undef> | |||
| 1709 | ||||
| 1710 | Unlike C<get_method>, this method I<will> look for the named method in | |||
| 1711 | superclasses. | |||
| 1712 | ||||
| 1713 | =item B<< $metaclass->get_all_method_names >> | |||
| 1714 | ||||
| 1715 | This will return a list of method I<names> for all of this class's | |||
| 1716 | methods, including inherited methods. | |||
| 1717 | ||||
| 1718 | =item B<< $metaclass->find_all_methods_by_name($method_name) >> | |||
| 1719 | ||||
| 1720 | This method looks for the named method in the class and all of its | |||
| 1721 | parents. It returns every matching method it finds in the inheritance | |||
| 1722 | tree, so it returns a list of methods. | |||
| 1723 | ||||
| 1724 | Each method is returned as a hash reference with three keys. The keys | |||
| 1725 | are C<name>, C<class>, and C<code>. The C<code> key has a | |||
| 1726 | L<Class::MOP::Method> object as its value. | |||
| 1727 | ||||
| 1728 | The list of methods is distinct. | |||
| 1729 | ||||
| 1730 | =item B<< $metaclass->find_next_method_by_name($method_name) >> | |||
| 1731 | ||||
| 1732 | This method returns the first method in any superclass matching the | |||
| 1733 | given name. It is effectively the method that C<SUPER::$method_name> | |||
| 1734 | would dispatch to. | |||
| 1735 | ||||
| 1736 | =back | |||
| 1737 | ||||
| 1738 | =head2 Attribute introspection and creation | |||
| 1739 | ||||
| 1740 | Because Perl 5 does not have a core concept of attributes in classes, | |||
| 1741 | we can only return information about attributes which have been added | |||
| 1742 | via this class's methods. We cannot discover information about | |||
| 1743 | attributes which are defined in terms of "regular" Perl 5 methods. | |||
| 1744 | ||||
| 1745 | =over 4 | |||
| 1746 | ||||
| 1747 | =item B<< $metaclass->get_attribute($attribute_name) >> | |||
| 1748 | ||||
| 1749 | This will return a L<Class::MOP::Attribute> for the specified | |||
| 1750 | C<$attribute_name>. If the class does not have the specified | |||
| 1751 | attribute, it returns C<undef>. | |||
| 1752 | ||||
| 1753 | NOTE that get_attribute does not search superclasses, for that you | |||
| 1754 | need to use C<find_attribute_by_name>. | |||
| 1755 | ||||
| 1756 | =item B<< $metaclass->has_attribute($attribute_name) >> | |||
| 1757 | ||||
| 1758 | Returns a boolean indicating whether or not the class defines the | |||
| 1759 | named attribute. It does not include attributes inherited from parent | |||
| 1760 | classes. | |||
| 1761 | ||||
| 1762 | =item B<< $metaclass->get_attribute_list >> | |||
| 1763 | ||||
| 1764 | This will return a list of attributes I<names> for all attributes | |||
| 1765 | defined in this class. Note that this operates on the current class | |||
| 1766 | only, it does not traverse the inheritance hierarchy. | |||
| 1767 | ||||
| 1768 | =item B<< $metaclass->get_all_attributes >> | |||
| 1769 | ||||
| 1770 | This will traverse the inheritance hierarchy and return a list of all | |||
| 1771 | the L<Class::MOP::Attribute> objects for this class and its parents. | |||
| 1772 | ||||
| 1773 | =item B<< $metaclass->find_attribute_by_name($attribute_name) >> | |||
| 1774 | ||||
| 1775 | This will return a L<Class::MOP::Attribute> for the specified | |||
| 1776 | C<$attribute_name>. If the class does not have the specified | |||
| 1777 | attribute, it returns C<undef>. | |||
| 1778 | ||||
| 1779 | Unlike C<get_attribute>, this attribute I<will> look for the named | |||
| 1780 | attribute in superclasses. | |||
| 1781 | ||||
| 1782 | =item B<< $metaclass->add_attribute(...) >> | |||
| 1783 | ||||
| 1784 | This method accepts either an existing L<Class::MOP::Attribute> | |||
| 1785 | object or parameters suitable for passing to that class's C<new> | |||
| 1786 | method. | |||
| 1787 | ||||
| 1788 | The attribute provided will be added to the class. | |||
| 1789 | ||||
| 1790 | Any accessor methods defined by the attribute will be added to the | |||
| 1791 | class when the attribute is added. | |||
| 1792 | ||||
| 1793 | If an attribute of the same name already exists, the old attribute | |||
| 1794 | will be removed first. | |||
| 1795 | ||||
| 1796 | =item B<< $metaclass->remove_attribute($attribute_name) >> | |||
| 1797 | ||||
| 1798 | This will remove the named attribute from the class, and | |||
| 1799 | L<Class::MOP::Attribute> object. | |||
| 1800 | ||||
| 1801 | Removing an attribute also removes any accessor methods defined by the | |||
| 1802 | attribute. | |||
| 1803 | ||||
| 1804 | However, note that removing an attribute will only affect I<future> | |||
| 1805 | object instances created for this class, not existing instances. | |||
| 1806 | ||||
| 1807 | =item B<< $metaclass->attribute_metaclass >> | |||
| 1808 | ||||
| 1809 | Returns the class name of the attribute metaclass for this class. By | |||
| 1810 | default, this is L<Class::MOP::Attribute>. | |||
| 1811 | ||||
| 1812 | =back | |||
| 1813 | ||||
| 1814 | =head2 Class Immutability | |||
| 1815 | ||||
| 1816 | Making a class immutable "freezes" the class definition. You can no | |||
| 1817 | longer call methods which alter the class, such as adding or removing | |||
| 1818 | methods or attributes. | |||
| 1819 | ||||
| 1820 | Making a class immutable lets us optimize the class by inlining some | |||
| 1821 | methods, and also allows us to optimize some methods on the metaclass | |||
| 1822 | object itself. | |||
| 1823 | ||||
| 1824 | After immutabilization, the metaclass object will cache most informational | |||
| 1825 | methods that returns information about methods or attributes. Methods which | |||
| 1826 | would alter the class, such as C<add_attribute> and C<add_method>, will | |||
| 1827 | throw an error on an immutable metaclass object. | |||
| 1828 | ||||
| 1829 | The immutabilization system in L<Moose> takes much greater advantage | |||
| 1830 | of the inlining features than Class::MOP itself does. | |||
| 1831 | ||||
| 1832 | =over 4 | |||
| 1833 | ||||
| 1834 | =item B<< $metaclass->make_immutable(%options) >> | |||
| 1835 | ||||
| 1836 | This method will create an immutable transformer and use it to make | |||
| 1837 | the class and its metaclass object immutable. | |||
| 1838 | ||||
| 1839 | This method accepts the following options: | |||
| 1840 | ||||
| 1841 | =over 8 | |||
| 1842 | ||||
| 1843 | =item * inline_accessors | |||
| 1844 | ||||
| 1845 | =item * inline_constructor | |||
| 1846 | ||||
| 1847 | =item * inline_destructor | |||
| 1848 | ||||
| 1849 | These are all booleans indicating whether the specified method(s) | |||
| 1850 | should be inlined. | |||
| 1851 | ||||
| 1852 | By default, accessors and the constructor are inlined, but not the | |||
| 1853 | destructor. | |||
| 1854 | ||||
| 1855 | =item * immutable_trait | |||
| 1856 | ||||
| 1857 | The name of a class which will be used as a parent class for the | |||
| 1858 | metaclass object being made immutable. This "trait" implements the | |||
| 1859 | post-immutability functionality of the metaclass (but not the | |||
| 1860 | transformation itself). | |||
| 1861 | ||||
| 1862 | This defaults to L<Class::MOP::Class::Immutable::Trait>. | |||
| 1863 | ||||
| 1864 | =item * constructor_name | |||
| 1865 | ||||
| 1866 | This is the constructor method name. This defaults to "new". | |||
| 1867 | ||||
| 1868 | =item * constructor_class | |||
| 1869 | ||||
| 1870 | The name of the method metaclass for constructors. It will be used to | |||
| 1871 | generate the inlined constructor. This defaults to | |||
| 1872 | "Class::MOP::Method::Constructor". | |||
| 1873 | ||||
| 1874 | =item * replace_constructor | |||
| 1875 | ||||
| 1876 | This is a boolean indicating whether an existing constructor should be | |||
| 1877 | replaced when inlining a constructor. This defaults to false. | |||
| 1878 | ||||
| 1879 | =item * destructor_class | |||
| 1880 | ||||
| 1881 | The name of the method metaclass for destructors. It will be used to | |||
| 1882 | generate the inlined destructor. This defaults to | |||
| 1883 | "Class::MOP::Method::Denstructor". | |||
| 1884 | ||||
| 1885 | =item * replace_destructor | |||
| 1886 | ||||
| 1887 | This is a boolean indicating whether an existing destructor should be | |||
| 1888 | replaced when inlining a destructor. This defaults to false. | |||
| 1889 | ||||
| 1890 | =back | |||
| 1891 | ||||
| 1892 | =item B<< $metaclass->immutable_options >> | |||
| 1893 | ||||
| 1894 | Returns a hash of the options used when making the class immutable, including | |||
| 1895 | both defaults and anything supplied by the user in the call to C<< | |||
| 1896 | $metaclass->make_immutable >>. This is useful if you need to temporarily make | |||
| 1897 | a class mutable and then restore immutability as it was before. | |||
| 1898 | ||||
| 1899 | =item B<< $metaclass->make_mutable >> | |||
| 1900 | ||||
| 1901 | Calling this method reverse the immutabilization transformation. | |||
| 1902 | ||||
| 1903 | =back | |||
| 1904 | ||||
| 1905 | =head2 Method Modifiers | |||
| 1906 | ||||
| 1907 | Method modifiers are hooks which allow a method to be wrapped with | |||
| 1908 | I<before>, I<after> and I<around> method modifiers. Every time a | |||
| 1909 | method is called, its modifiers are also called. | |||
| 1910 | ||||
| 1911 | A class can modify its own methods, as well as methods defined in | |||
| 1912 | parent classes. | |||
| 1913 | ||||
| 1914 | =head3 How method modifiers work? | |||
| 1915 | ||||
| 1916 | Method modifiers work by wrapping the original method and then | |||
| 1917 | replacing it in the class's symbol table. The wrappers will handle | |||
| 1918 | calling all the modifiers in the appropriate order and preserving the | |||
| 1919 | calling context for the original method. | |||
| 1920 | ||||
| 1921 | The return values of C<before> and C<after> modifiers are | |||
| 1922 | ignored. This is because their purpose is B<not> to filter the input | |||
| 1923 | and output of the primary method (this is done with an I<around> | |||
| 1924 | modifier). | |||
| 1925 | ||||
| 1926 | This may seem like an odd restriction to some, but doing this allows | |||
| 1927 | for simple code to be added at the beginning or end of a method call | |||
| 1928 | without altering the function of the wrapped method or placing any | |||
| 1929 | extra responsibility on the code of the modifier. | |||
| 1930 | ||||
| 1931 | Of course if you have more complex needs, you can use the C<around> | |||
| 1932 | modifier which allows you to change both the parameters passed to the | |||
| 1933 | wrapped method, as well as its return value. | |||
| 1934 | ||||
| 1935 | Before and around modifiers are called in last-defined-first-called | |||
| 1936 | order, while after modifiers are called in first-defined-first-called | |||
| 1937 | order. So the call tree might looks something like this: | |||
| 1938 | ||||
| 1939 | before 2 | |||
| 1940 | before 1 | |||
| 1941 | around 2 | |||
| 1942 | around 1 | |||
| 1943 | primary | |||
| 1944 | around 1 | |||
| 1945 | around 2 | |||
| 1946 | after 1 | |||
| 1947 | after 2 | |||
| 1948 | ||||
| 1949 | =head3 What is the performance impact? | |||
| 1950 | ||||
| 1951 | Of course there is a performance cost associated with method | |||
| 1952 | modifiers, but we have made every effort to make that cost directly | |||
| 1953 | proportional to the number of modifier features you use. | |||
| 1954 | ||||
| 1955 | The wrapping method does its best to B<only> do as much work as it | |||
| 1956 | absolutely needs to. In order to do this we have moved some of the | |||
| 1957 | performance costs to set-up time, where they are easier to amortize. | |||
| 1958 | ||||
| 1959 | All this said, our benchmarks have indicated the following: | |||
| 1960 | ||||
| 1961 | simple wrapper with no modifiers 100% slower | |||
| 1962 | simple wrapper with simple before modifier 400% slower | |||
| 1963 | simple wrapper with simple after modifier 450% slower | |||
| 1964 | simple wrapper with simple around modifier 500-550% slower | |||
| 1965 | simple wrapper with all 3 modifiers 1100% slower | |||
| 1966 | ||||
| 1967 | These numbers may seem daunting, but you must remember, every feature | |||
| 1968 | comes with some cost. To put things in perspective, just doing a | |||
| 1969 | simple C<AUTOLOAD> which does nothing but extract the name of the | |||
| 1970 | method called and return it costs about 400% over a normal method | |||
| 1971 | call. | |||
| 1972 | ||||
| 1973 | =over 4 | |||
| 1974 | ||||
| 1975 | =item B<< $metaclass->add_before_method_modifier($method_name, $code) >> | |||
| 1976 | ||||
| 1977 | This wraps the specified method with the supplied subroutine | |||
| 1978 | reference. The modifier will be called as a method itself, and will | |||
| 1979 | receive the same arguments as are passed to the method. | |||
| 1980 | ||||
| 1981 | When the modifier exits, the wrapped method will be called. | |||
| 1982 | ||||
| 1983 | The return value of the modifier will be ignored. | |||
| 1984 | ||||
| 1985 | =item B<< $metaclass->add_after_method_modifier($method_name, $code) >> | |||
| 1986 | ||||
| 1987 | This wraps the specified method with the supplied subroutine | |||
| 1988 | reference. The modifier will be called as a method itself, and will | |||
| 1989 | receive the same arguments as are passed to the method. | |||
| 1990 | ||||
| 1991 | When the wrapped methods exits, the modifier will be called. | |||
| 1992 | ||||
| 1993 | The return value of the modifier will be ignored. | |||
| 1994 | ||||
| 1995 | =item B<< $metaclass->add_around_method_modifier($method_name, $code) >> | |||
| 1996 | ||||
| 1997 | This wraps the specified method with the supplied subroutine | |||
| 1998 | reference. | |||
| 1999 | ||||
| 2000 | The first argument passed to the modifier will be a subroutine | |||
| 2001 | reference to the wrapped method. The second argument is the object, | |||
| 2002 | and after that come any arguments passed when the method is called. | |||
| 2003 | ||||
| 2004 | The around modifier can choose to call the original method, as well as | |||
| 2005 | what arguments to pass if it does so. | |||
| 2006 | ||||
| 2007 | The return value of the modifier is what will be seen by the caller. | |||
| 2008 | ||||
| 2009 | =back | |||
| 2010 | ||||
| 2011 | =head2 Introspection | |||
| 2012 | ||||
| 2013 | =over 4 | |||
| 2014 | ||||
| 2015 | =item B<< Class::MOP::Class->meta >> | |||
| 2016 | ||||
| 2017 | This will return a L<Class::MOP::Class> instance for this class. | |||
| 2018 | ||||
| 2019 | It should also be noted that L<Class::MOP> will actually bootstrap | |||
| 2020 | this module by installing a number of attribute meta-objects into its | |||
| 2021 | metaclass. | |||
| 2022 | ||||
| 2023 | =back | |||
| 2024 | ||||
| 2025 | =head1 AUTHORS | |||
| 2026 | ||||
| 2027 | Stevan Little E<lt>stevan@iinteractive.comE<gt> | |||
| 2028 | ||||
| 2029 | =head1 COPYRIGHT AND LICENSE | |||
| 2030 | ||||
| 2031 | Copyright 2006-2010 by Infinity Interactive, Inc. | |||
| 2032 | ||||
| 2033 | L<http://www.iinteractive.com> | |||
| 2034 | ||||
| 2035 | This library is free software; you can redistribute it and/or modify | |||
| 2036 | it under the same terms as Perl itself. | |||
| 2037 | ||||
| 2038 | =cut |