| File | /usr/lib/perl5/vendor_perl/5.10.1/i386-linux-thread-multi/Moose/Util.pm |
| Statements Executed | 3175 |
| Statement Execution Time | 37.3ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1105 | 10 | 7 | 13.2ms | 37.7ms | Moose::Util::find_meta |
| 130 | 3 | 3 | 5.83ms | 56.6ms | Moose::Util::does_role |
| 37 | 1 | 1 | 5.40ms | 1.58s | Moose::Util::_apply_all_roles |
| 100 | 2 | 2 | 3.36ms | 3.36ms | Moose::Util::_caller_info |
| 34 | 1 | 1 | 1.07ms | 16.3ms | Moose::Util::resolve_metaclass_alias |
| 17 | 3 | 1 | 863µs | 25.0ms | Moose::Util::add_method_modifier |
| 34 | 2 | 1 | 777µs | 17.1ms | Moose::Util::resolve_metatrait_alias |
| 37 | 4 | 4 | 707µs | 1.58s | Moose::Util::apply_all_roles |
| 1 | 1 | 1 | 146µs | 324µs | Moose::Util::BEGIN@8 |
| 1 | 1 | 1 | 109µs | 125µs | Moose::Util::BEGIN@3 |
| 1 | 1 | 1 | 73µs | 91µs | Moose::Util::BEGIN@6 |
| 1 | 1 | 1 | 37µs | 169µs | Moose::Util::BEGIN@9 |
| 1 | 1 | 1 | 33µs | 98µs | Moose::Util::BEGIN@4 |
| 1 | 1 | 1 | 32µs | 784µs | Moose::Util::BEGIN@7 |
| 1 | 1 | 1 | 16µs | 16µs | Moose::Util::_build_alias_package_name |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::__ANON__[:257] |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::__ANON__[:81] |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::_create_alias |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::english_list |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::ensure_all_roles |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::get_all_attribute_values |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::get_all_init_args |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::meta_attribute_alias |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::meta_class_alias |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::search_class_by_role |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package Moose::Util; | ||||
| 2 | |||||
| 3 | 3 | 94µs | 2 | 142µs | # spent 125µs (109+16) within Moose::Util::BEGIN@3 which was called
# once (109µs+16µs) by Moose::Meta::Attribute::BEGIN@17 at line 3 # spent 125µs making 1 call to Moose::Util::BEGIN@3
# spent 16µs making 1 call to strict::import |
| 4 | 3 | 90µs | 2 | 163µs | # spent 98µs (33+65) within Moose::Util::BEGIN@4 which was called
# once (33µs+65µs) by Moose::Meta::Attribute::BEGIN@17 at line 4 # spent 98µs making 1 call to Moose::Util::BEGIN@4
# spent 65µs making 1 call to warnings::import |
| 5 | |||||
| 6 | 3 | 93µs | 2 | 109µs | # spent 91µs (73+18) within Moose::Util::BEGIN@6 which was called
# once (73µs+18µs) by Moose::Meta::Attribute::BEGIN@17 at line 6 # spent 91µs making 1 call to Moose::Util::BEGIN@6
# spent 18µs making 1 call to Sub::Install::__ANON__[Sub/Install.pm:284] |
| 7 | 3 | 268µs | 2 | 1.54ms | # spent 784µs (32+753) within Moose::Util::BEGIN@7 which was called
# once (32µs+753µs) by Moose::Meta::Attribute::BEGIN@17 at line 7 # spent 784µs making 1 call to Moose::Util::BEGIN@7
# spent 752µs making 1 call to Sub::Exporter::__ANON__[Sub/Exporter.pm:756] |
| 8 | 3 | 95µs | 2 | 503µs | # spent 324µs (146+178) within Moose::Util::BEGIN@8 which was called
# once (146µs+178µs) by Moose::Meta::Attribute::BEGIN@17 at line 8 # spent 324µs making 1 call to Moose::Util::BEGIN@8
# spent 178µs making 1 call to Exporter::import |
| 9 | 3 | 5.03ms | 2 | 301µs | # spent 169µs (37+132) within Moose::Util::BEGIN@9 which was called
# once (37µs+132µs) by Moose::Meta::Attribute::BEGIN@17 at line 9 # spent 169µs making 1 call to Moose::Util::BEGIN@9
# spent 132µs making 1 call to UNIVERSAL::VERSION |
| 10 | |||||
| 11 | 1 | 4µs | our $VERSION = '1.01'; | ||
| 12 | 1 | 50µs | $VERSION = eval $VERSION; | ||
| 13 | 1 | 3µs | our $AUTHORITY = 'cpan:STEVAN'; | ||
| 14 | |||||
| 15 | 1 | 11µs | my @exports = qw[ | ||
| 16 | find_meta | ||||
| 17 | does_role | ||||
| 18 | search_class_by_role | ||||
| 19 | ensure_all_roles | ||||
| 20 | apply_all_roles | ||||
| 21 | get_all_init_args | ||||
| 22 | get_all_attribute_values | ||||
| 23 | resolve_metatrait_alias | ||||
| 24 | resolve_metaclass_alias | ||||
| 25 | add_method_modifier | ||||
| 26 | english_list | ||||
| 27 | meta_attribute_alias | ||||
| 28 | meta_class_alias | ||||
| 29 | ]; | ||||
| 30 | |||||
| 31 | 1 | 20µs | 1 | 2.45ms | Sub::Exporter::setup_exporter({ # spent 2.45ms making 1 call to Sub::Exporter::setup_exporter |
| 32 | exports => \@exports, | ||||
| 33 | groups => { all => \@exports } | ||||
| 34 | }); | ||||
| 35 | |||||
| 36 | ## some utils for the utils ... | ||||
| 37 | |||||
| 38 | 1105 | 13.3ms | 1105 | 24.6ms | # spent 37.7ms (13.2+24.6) within Moose::Util::find_meta which was called 1105 times, avg 34µs/call:
# 801 times (9.41ms+17.9ms) by Catalyst::Component::__config_accessor or Catalyst::Component::__plugins_accessor or Catalyst::Controller::__action_class_accessor or Catalyst::Controller::__dispatch_steps_accessor or Catalyst::_arguments_accessor or Catalyst::_components_accessor or Catalyst::_context_class_accessor or Catalyst::_dispatcher_accessor or Catalyst::_dispatcher_class_accessor or Catalyst::_engine_accessor or Catalyst::_engine_class_accessor or Catalyst::_log_accessor or Catalyst::_request_class_accessor or Catalyst::_response_class_accessor or Catalyst::_setup_finished_accessor or Catalyst::_stats_class_accessor at line 16 of Catalyst/ClassData.pm, avg 34µs/call
# 130 times (1.41ms+2.89ms) by Moose::Util::does_role at line 45, avg 33µs/call
# 45 times (420µs+801µs) by MooseX::MethodAttributes::Role::Meta::Class::get_all_methods_with_attributes at line 48 of MooseX/MethodAttributes/Role/Meta/Class.pm, avg 27µs/call
# 42 times (587µs+1.16ms) by MooseX::MethodAttributes::init_meta at line 22 of MooseX/MethodAttributes.pm, avg 42µs/call
# 35 times (364µs+710µs) by MooseX::MethodAttributes::Role::AttrContainer::MODIFY_CODE_ATTRIBUTES at line 13 of MooseX/MethodAttributes/Role/AttrContainer.pm, avg 31µs/call
# 30 times (355µs+509µs) by Catalyst::_controller_init_base_classes at line 2488 of Catalyst.pm, avg 29µs/call
# 7 times (302µs+241µs) by Catalyst::setup_plugins at line 2814 of Catalyst.pm, avg 78µs/call
# 7 times (211µs+128µs) by Catalyst::Controller::get_action_methods at line 184 of Catalyst/Controller.pm, avg 48µs/call
# 7 times (87µs+140µs) by Catalyst::setup_plugins at line 2822 of Catalyst.pm, avg 32µs/call
# once (16µs+70µs) by Moose::Util::_apply_all_roles at line 128 # spent 24.6ms making 1105 calls to Class::MOP::class_of, avg 22µs/call |
| 39 | |||||
| 40 | ## the functions ... | ||||
| 41 | |||||
| 42 | # spent 56.6ms (5.83+50.8) within Moose::Util::does_role which was called 130 times, avg 435µs/call:
# 106 times (3.93ms+49.9ms) by MooseX::MethodAttributes::init_meta at line 24 of MooseX/MethodAttributes.pm, avg 508µs/call
# 12 times (1.42ms+424µs) by Class::MOP::Class:::around at line 78 of MooseX/MethodAttributes/Role/Meta/Class.pm, avg 153µs/call
# 12 times (485µs+388µs) by MooseX::MethodAttributes::Role::Meta::Method::MaybeWrapped::__ANON__[/usr/lib/perl5/vendor_perl/5.10.1/MooseX/MethodAttributes/Role/Meta/Method/MaybeWrapped.pm:21] at line 14 of MooseX/MethodAttributes/Role/Meta/Method/MaybeWrapped.pm, avg 73µs/call | ||||
| 43 | 130 | 401µs | my ($class_or_obj, $role) = @_; | ||
| 44 | |||||
| 45 | 130 | 775µs | 130 | 4.30ms | my $meta = find_meta($class_or_obj); # spent 4.30ms making 130 calls to Moose::Util::find_meta, avg 33µs/call |
| 46 | |||||
| 47 | 130 | 167µs | return unless defined $meta; | ||
| 48 | 130 | 2.22ms | 130 | 620µs | return unless $meta->can('does_role'); # spent 620µs making 130 calls to UNIVERSAL::can, avg 5µs/call |
| 49 | 104 | 1.56ms | 104 | 45.8ms | return 1 if $meta->does_role($role); # spent 45.8ms making 104 calls to Moose::Meta::Class::does_role, avg 441µs/call |
| 50 | return; | ||||
| 51 | } | ||||
| 52 | |||||
| 53 | sub search_class_by_role { | ||||
| 54 | my ($class_or_obj, $role) = @_; | ||||
| 55 | |||||
| 56 | my $meta = find_meta($class_or_obj); | ||||
| 57 | |||||
| 58 | return unless defined $meta; | ||||
| 59 | |||||
| 60 | my $role_name = blessed $role ? $role->name : $role; | ||||
| 61 | |||||
| 62 | foreach my $class ($meta->class_precedence_list) { | ||||
| 63 | |||||
| 64 | my $_meta = find_meta($class); | ||||
| 65 | |||||
| 66 | next unless defined $_meta; | ||||
| 67 | |||||
| 68 | foreach my $role (@{ $_meta->roles || [] }) { | ||||
| 69 | return $class if $role->name eq $role_name; | ||||
| 70 | } | ||||
| 71 | } | ||||
| 72 | |||||
| 73 | return; | ||||
| 74 | } | ||||
| 75 | |||||
| 76 | # this can possibly behave in unexpected ways because the roles being composed | ||||
| 77 | # before being applied could differ from call to call; I'm not sure if or how | ||||
| 78 | # to document this possible quirk. | ||||
| 79 | sub ensure_all_roles { | ||||
| 80 | my $applicant = shift; | ||||
| 81 | _apply_all_roles($applicant, sub { !does_role($applicant, $_) }, @_); | ||||
| 82 | } | ||||
| 83 | |||||
| 84 | # spent 1.58s (707µs+1.58) within Moose::Util::apply_all_roles which was called 37 times, avg 42.7ms/call:
# 17 times (263µs+1.22s) by Moose::with at line 57 of Moose.pm, avg 71.5ms/call
# 11 times (184µs+119ms) by Moose::Role::with at line 26 of Moose/Role.pm, avg 10.8ms/call
# 8 times (240µs+157ms) by Moose::Meta::Class::create at line 94 of Moose/Meta/Class.pm, avg 19.7ms/call
# once (20µs+85.4ms) by Catalyst::setup_plugins at line 2826 of Catalyst.pm | ||||
| 85 | 37 | 69µs | my $applicant = shift; | ||
| 86 | 37 | 807µs | 38 | 1.58s | _apply_all_roles($applicant, undef, @_); # spent 2.30s making 37 calls to Moose::Util::_apply_all_roles, avg 62.2ms/call, recursion: max depth 2, time 725ms
# spent 55µs making 1 call to Class::MOP::Class::DESTROY |
| 87 | } | ||||
| 88 | |||||
| 89 | # spent 1.58s (5.40ms+1.57) within Moose::Util::_apply_all_roles which was called 37 times, avg 42.6ms/call:
# 37 times (5.40ms+1.57s) by Moose::Util::apply_all_roles at line 86, avg 42.6ms/call | ||||
| 90 | 37 | 59µs | my $applicant = shift; | ||
| 91 | 37 | 63µs | my $role_filter = shift; | ||
| 92 | |||||
| 93 | 37 | 53µs | unless (@_) { | ||
| 94 | require Moose; | ||||
| 95 | Moose->throw_error("Must specify at least one role to apply to $applicant"); | ||||
| 96 | } | ||||
| 97 | |||||
| 98 | 37 | 561µs | 37 | 2.26ms | my $roles = Data::OptList::mkopt( [@_] ); # spent 2.26ms making 37 calls to Data::OptList::mkopt, avg 61µs/call |
| 99 | |||||
| 100 | 37 | 50µs | my @role_metas; | ||
| 101 | 37 | 158µs | foreach my $role (@$roles) { | ||
| 102 | 41 | 53µs | my $meta; | ||
| 103 | |||||
| 104 | 41 | 824µs | 41 | 253µs | if ( blessed $role->[0] ) { # spent 253µs making 41 calls to Scalar::Util::blessed, avg 6µs/call |
| 105 | $meta = $role->[0]; | ||||
| 106 | } | ||||
| 107 | else { | ||||
| 108 | 41 | 377µs | 41 | 731ms | Class::MOP::load_class( $role->[0] ); # spent 1.38s making 41 calls to Class::MOP::load_class, avg 33.6ms/call, recursion: max depth 2, time 646ms |
| 109 | 41 | 352µs | 41 | 1.19ms | $meta = Class::MOP::class_of( $role->[0] ); # spent 1.19ms making 41 calls to Class::MOP::class_of, avg 29µs/call |
| 110 | } | ||||
| 111 | |||||
| 112 | 41 | 700µs | 41 | 245µs | unless ($meta && $meta->isa('Moose::Meta::Role') ) { # spent 245µs making 41 calls to UNIVERSAL::isa, avg 6µs/call |
| 113 | require Moose; | ||||
| 114 | Moose->throw_error( "You can only consume roles, " | ||||
| 115 | . $role->[0] | ||||
| 116 | . " is not a Moose role" ); | ||||
| 117 | } | ||||
| 118 | |||||
| 119 | 41 | 374µs | push @role_metas, [ $meta, $role->[1] ]; | ||
| 120 | } | ||||
| 121 | |||||
| 122 | 37 | 55µs | if ( defined $role_filter ) { | ||
| 123 | @role_metas = grep { local $_ = $_->[0]; $role_filter->() } @role_metas; | ||||
| 124 | } | ||||
| 125 | |||||
| 126 | 37 | 60µs | return unless @role_metas; | ||
| 127 | |||||
| 128 | 37 | 504µs | 38 | 269µs | my $meta = ( blessed $applicant ? $applicant : find_meta($applicant) ); # spent 183µs making 37 calls to Scalar::Util::blessed, avg 5µs/call
# spent 86µs making 1 call to Moose::Util::find_meta |
| 129 | |||||
| 130 | 37 | 531µs | if ( scalar @role_metas == 1 ) { | ||
| 131 | 33 | 88µs | my ( $role, $params ) = @{ $role_metas[0] }; | ||
| 132 | 33 | 575µs | 33 | 807ms | $role->apply( $meta, ( defined $params ? %$params : () ) ); # spent 807ms making 33 calls to Moose::Meta::Role::apply, avg 24.5ms/call |
| 133 | } | ||||
| 134 | else { | ||||
| 135 | 4 | 272µs | 8 | 110ms | Moose::Meta::Role->combine(@role_metas)->apply($meta); # spent 81.0ms making 4 calls to Moose::Meta::Role::combine, avg 20.3ms/call
# spent 28.8ms making 4 calls to Moose::Meta::Role::apply, avg 7.21ms/call |
| 136 | } | ||||
| 137 | } | ||||
| 138 | |||||
| 139 | # instance deconstruction ... | ||||
| 140 | |||||
| 141 | sub get_all_attribute_values { | ||||
| 142 | my ($class, $instance) = @_; | ||||
| 143 | return +{ | ||||
| 144 | map { $_->name => $_->get_value($instance) } | ||||
| 145 | grep { $_->has_value($instance) } | ||||
| 146 | $class->get_all_attributes | ||||
| 147 | }; | ||||
| 148 | } | ||||
| 149 | |||||
| 150 | sub get_all_init_args { | ||||
| 151 | my ($class, $instance) = @_; | ||||
| 152 | return +{ | ||||
| 153 | map { $_->init_arg => $_->get_value($instance) } | ||||
| 154 | grep { $_->has_value($instance) } | ||||
| 155 | grep { defined($_->init_arg) } | ||||
| 156 | $class->get_all_attributes | ||||
| 157 | }; | ||||
| 158 | } | ||||
| 159 | |||||
| 160 | # spent 17.1ms (777µs+16.3) within Moose::Util::resolve_metatrait_alias which was called 34 times, avg 502µs/call:
# 17 times (427µs+15.9ms) by Moose::Meta::Attribute::interpolate_class at line 106 of Moose/Meta/Attribute.pm, avg 959µs/call
# 17 times (349µs+436µs) by Moose::Meta::Attribute::__ANON__[/usr/lib/perl5/vendor_perl/5.10.1/i386-linux-thread-multi/Moose/Meta/Attribute.pm:35] at line 34 of Moose/Meta/Attribute.pm, avg 46µs/call | ||||
| 161 | 34 | 1.02ms | 34 | 16.3ms | return resolve_metaclass_alias( @_, trait => 1 ); # spent 16.3ms making 34 calls to Moose::Util::resolve_metaclass_alias, avg 480µs/call |
| 162 | } | ||||
| 163 | |||||
| 164 | # spent 16µs within Moose::Util::_build_alias_package_name which was called
# once (16µs+0s) by Moose::Util::resolve_metaclass_alias at line 183 | ||||
| 165 | 1 | 4µs | my ($type, $name, $trait) = @_; | ||
| 166 | 1 | 20µs | return 'Moose::Meta::' | ||
| 167 | . $type | ||||
| 168 | . '::Custom::' | ||||
| 169 | . ( $trait ? 'Trait::' : '' ) | ||||
| 170 | . $name; | ||||
| 171 | } | ||||
| 172 | |||||
| 173 | { | ||||
| 174 | 2 | 5µs | my %cache; | ||
| 175 | |||||
| 176 | # spent 16.3ms (1.07+15.2) within Moose::Util::resolve_metaclass_alias which was called 34 times, avg 480µs/call:
# 34 times (1.07ms+15.2ms) by Moose::Util::resolve_metatrait_alias at line 161, avg 480µs/call | ||||
| 177 | 34 | 190µs | my ( $type, $metaclass_name, %options ) = @_; | ||
| 178 | |||||
| 179 | 34 | 185µs | my $cache_key = $type . q{ } . ( $options{trait} ? '-Trait' : '' ); | ||
| 180 | 34 | 546µs | return $cache{$cache_key}{$metaclass_name} | ||
| 181 | if $cache{$cache_key}{$metaclass_name}; | ||||
| 182 | |||||
| 183 | 1 | 10µs | 1 | 16µs | my $possible_full_name = _build_alias_package_name( # spent 16µs making 1 call to Moose::Util::_build_alias_package_name |
| 184 | $type, $metaclass_name, $options{trait} | ||||
| 185 | ); | ||||
| 186 | |||||
| 187 | 1 | 10µs | 1 | 0s | my $loaded_class = Class::MOP::load_first_existing_class( # spent 15.2ms making 1 call to Class::MOP::load_first_existing_class, recursion: max depth 1, time 15.2ms |
| 188 | $possible_full_name, | ||||
| 189 | $metaclass_name | ||||
| 190 | ); | ||||
| 191 | |||||
| 192 | 1 | 130µs | 1 | 18µs | return $cache{$cache_key}{$metaclass_name} # spent 18µs making 1 call to UNIVERSAL::can |
| 193 | = $loaded_class->can('register_implementation') | ||||
| 194 | ? $loaded_class->register_implementation | ||||
| 195 | : $loaded_class; | ||||
| 196 | } | ||||
| 197 | } | ||||
| 198 | |||||
| 199 | # spent 25.0ms (863µs+24.2) within Moose::Util::add_method_modifier which was called 17 times, avg 1.47ms/call:
# 12 times (516µs+17.4ms) by Moose::around at line 81 of Moose.pm, avg 1.49ms/call
# 4 times (201µs+5.73ms) by Moose::before at line 73 of Moose.pm, avg 1.48ms/call
# once (145µs+1.05ms) by Moose::after at line 77 of Moose.pm | ||||
| 200 | 17 | 41µs | my ( $class_or_obj, $modifier_name, $args ) = @_; | ||
| 201 | 17 | 256µs | 17 | 89µs | my $meta # spent 89µs making 17 calls to UNIVERSAL::can, avg 5µs/call |
| 202 | = $class_or_obj->can('add_before_method_modifier') | ||||
| 203 | ? $class_or_obj | ||||
| 204 | : find_meta($class_or_obj); | ||||
| 205 | 17 | 31µs | my $code = pop @{$args}; | ||
| 206 | 17 | 49µs | my $add_modifier_method = 'add_' . $modifier_name . '_method_modifier'; | ||
| 207 | 17 | 177µs | if ( my $method_modifier_type = ref( @{$args}[0] ) ) { | ||
| 208 | if ( $method_modifier_type eq 'Regexp' ) { | ||||
| 209 | my @all_methods = $meta->get_all_methods; | ||||
| 210 | my @matched_methods | ||||
| 211 | = grep { $_->name =~ @{$args}[0] } @all_methods; | ||||
| 212 | $meta->$add_modifier_method( $_->name, $code ) | ||||
| 213 | for @matched_methods; | ||||
| 214 | } | ||||
| 215 | elsif ($method_modifier_type eq 'ARRAY') { | ||||
| 216 | $meta->$add_modifier_method( $_, $code ) for @{$args->[0]}; | ||||
| 217 | } | ||||
| 218 | else { | ||||
| 219 | $meta->throw_error( | ||||
| 220 | sprintf( | ||||
| 221 | "Methods passed to %s must be provided as a list, arrayref or regex, not %s", | ||||
| 222 | $modifier_name, | ||||
| 223 | $method_modifier_type, | ||||
| 224 | ) | ||||
| 225 | ); | ||||
| 226 | } | ||||
| 227 | } | ||||
| 228 | else { | ||||
| 229 | 34 | 317µs | 17 | 24.1ms | $meta->$add_modifier_method( $_, $code ) for @{$args}; # spent 10.9ms making 10 calls to Class::MOP::Class::add_around_method_modifier, avg 1.09ms/call
# spent 6.46ms making 2 calls to Class::MOP::Class::__ANON__::SERIAL::5::add_around_method_modifier, avg 3.23ms/call
# spent 5.71ms making 4 calls to Class::MOP::Class::add_before_method_modifier, avg 1.43ms/call
# spent 1.05ms making 1 call to Class::MOP::Class::add_after_method_modifier |
| 230 | } | ||||
| 231 | } | ||||
| 232 | |||||
| 233 | sub english_list { | ||||
| 234 | my @items = sort @_; | ||||
| 235 | |||||
| 236 | return $items[0] if @items == 1; | ||||
| 237 | return "$items[0] and $items[1]" if @items == 2; | ||||
| 238 | |||||
| 239 | my $tail = pop @items; | ||||
| 240 | my $list = join ', ', @items; | ||||
| 241 | $list .= ', and ' . $tail; | ||||
| 242 | |||||
| 243 | return $list; | ||||
| 244 | } | ||||
| 245 | |||||
| 246 | # spent 3.36ms within Moose::Util::_caller_info which was called 100 times, avg 34µs/call:
# 92 times (3.14ms+0s) by Moose::has at line 67 of Moose.pm, avg 34µs/call
# 8 times (218µs+0s) by Moose::Role::has at line 45 of Moose/Role.pm, avg 27µs/call | ||||
| 247 | 100 | 353µs | my $level = @_ ? ($_[0] + 1) : 2; | ||
| 248 | 100 | 159µs | my %info; | ||
| 249 | 100 | 2.20ms | @info{qw(package file line)} = caller($level); | ||
| 250 | 100 | 820µs | return \%info; | ||
| 251 | } | ||||
| 252 | |||||
| 253 | sub _create_alias { | ||||
| 254 | my ($type, $name, $trait, $for) = @_; | ||||
| 255 | my $package = _build_alias_package_name($type, $name, $trait); | ||||
| 256 | Class::MOP::Class->initialize($package)->add_method( | ||||
| 257 | register_implementation => sub { $for } | ||||
| 258 | ); | ||||
| 259 | } | ||||
| 260 | |||||
| 261 | sub meta_attribute_alias { | ||||
| 262 | my ($to, $from) = @_; | ||||
| 263 | $from ||= caller; | ||||
| 264 | my $meta = Class::MOP::class_of($from); | ||||
| 265 | my $trait = $meta->isa('Moose::Meta::Role'); | ||||
| 266 | _create_alias('Attribute', $to, $trait, $from); | ||||
| 267 | } | ||||
| 268 | |||||
| 269 | sub meta_class_alias { | ||||
| 270 | my ($to, $from) = @_; | ||||
| 271 | $from ||= caller; | ||||
| 272 | my $meta = Class::MOP::class_of($from); | ||||
| 273 | my $trait = $meta->isa('Moose::Meta::Role'); | ||||
| 274 | _create_alias('Class', $to, $trait, $from); | ||||
| 275 | } | ||||
| 276 | |||||
| 277 | 1 | 29µs | 1; | ||
| 278 | |||||
| 279 | __END__ | ||||
| 280 | |||||
| 281 | =pod | ||||
| 282 | |||||
| 283 | =head1 NAME | ||||
| 284 | |||||
| 285 | Moose::Util - Utilities for working with Moose classes | ||||
| 286 | |||||
| 287 | =head1 SYNOPSIS | ||||
| 288 | |||||
| 289 | use Moose::Util qw/find_meta does_role search_class_by_role/; | ||||
| 290 | |||||
| 291 | my $meta = find_meta($object) || die "No metaclass found"; | ||||
| 292 | |||||
| 293 | if (does_role($object, $role)) { | ||||
| 294 | print "The object can do $role!\n"; | ||||
| 295 | } | ||||
| 296 | |||||
| 297 | my $class = search_class_by_role($object, 'FooRole'); | ||||
| 298 | print "Nearest class with 'FooRole' is $class\n"; | ||||
| 299 | |||||
| 300 | =head1 DESCRIPTION | ||||
| 301 | |||||
| 302 | This module provides a set of utility functions. Many of these | ||||
| 303 | functions are intended for use in Moose itself or MooseX modules, but | ||||
| 304 | some of them may be useful for use in your own code. | ||||
| 305 | |||||
| 306 | =head1 EXPORTED FUNCTIONS | ||||
| 307 | |||||
| 308 | =over 4 | ||||
| 309 | |||||
| 310 | =item B<find_meta($class_or_obj)> | ||||
| 311 | |||||
| 312 | This method takes a class name or object and attempts to find a | ||||
| 313 | metaclass for the class, if one exists. It will B<not> create one if it | ||||
| 314 | does not yet exist. | ||||
| 315 | |||||
| 316 | =item B<does_role($class_or_obj, $role_or_obj)> | ||||
| 317 | |||||
| 318 | Returns true if C<$class_or_obj> does the given C<$role_or_obj>. The role can | ||||
| 319 | be provided as a name or a L<Moose::Meta::Role> object. | ||||
| 320 | |||||
| 321 | The class must already have a metaclass for this to work. If it doesn't, this | ||||
| 322 | function simply returns false. | ||||
| 323 | |||||
| 324 | =item B<search_class_by_role($class_or_obj, $role_or_obj)> | ||||
| 325 | |||||
| 326 | Returns the first class in the class's precedence list that does | ||||
| 327 | C<$role_or_obj>, if any. The role can be either a name or a | ||||
| 328 | L<Moose::Meta::Role> object. | ||||
| 329 | |||||
| 330 | The class must already have a metaclass for this to work. | ||||
| 331 | |||||
| 332 | =item B<apply_all_roles($applicant, @roles)> | ||||
| 333 | |||||
| 334 | This function applies one or more roles to the given C<$applicant> The | ||||
| 335 | applicant can be a role name, class name, or object. | ||||
| 336 | |||||
| 337 | The C<$applicant> must already have a metaclass object. | ||||
| 338 | |||||
| 339 | The list of C<@roles> should a list of names or L<Moose::Meta::Role> objects, | ||||
| 340 | each of which can be followed by an optional hash reference of options | ||||
| 341 | (C<-excludes> and C<-alias>). | ||||
| 342 | |||||
| 343 | =item B<ensure_all_roles($applicant, @roles)> | ||||
| 344 | |||||
| 345 | This function is similar to L</apply_all_roles>, but only applies roles that | ||||
| 346 | C<$applicant> does not already consume. | ||||
| 347 | |||||
| 348 | =item B<get_all_attribute_values($meta, $instance)> | ||||
| 349 | |||||
| 350 | Returns a hash reference containing all of the C<$instance>'s | ||||
| 351 | attributes. The keys are attribute names. | ||||
| 352 | |||||
| 353 | =item B<get_all_init_args($meta, $instance)> | ||||
| 354 | |||||
| 355 | Returns a hash reference containing all of the C<init_arg> values for | ||||
| 356 | the instance's attributes. The values are the associated attribute | ||||
| 357 | values. If an attribute does not have a defined C<init_arg>, it is | ||||
| 358 | skipped. | ||||
| 359 | |||||
| 360 | This could be useful in cloning an object. | ||||
| 361 | |||||
| 362 | =item B<resolve_metaclass_alias($category, $name, %options)> | ||||
| 363 | |||||
| 364 | =item B<resolve_metatrait_alias($category, $name, %options)> | ||||
| 365 | |||||
| 366 | Resolves a short name to a full class name. Short names are often used | ||||
| 367 | when specifying the C<metaclass> or C<traits> option for an attribute: | ||||
| 368 | |||||
| 369 | has foo => ( | ||||
| 370 | metaclass => "Bar", | ||||
| 371 | ); | ||||
| 372 | |||||
| 373 | The name resolution mechanism is covered in | ||||
| 374 | L<Moose/Metaclass and Trait Name Resolution>. | ||||
| 375 | |||||
| 376 | =item B<english_list(@items)> | ||||
| 377 | |||||
| 378 | Given a list of scalars, turns them into a proper list in English | ||||
| 379 | ("one and two", "one, two, three, and four"). This is used to help us | ||||
| 380 | make nicer error messages. | ||||
| 381 | |||||
| 382 | =item B<meta_class_alias($to[, $from])> | ||||
| 383 | |||||
| 384 | =item B<meta_attribute_alias($to[, $from])> | ||||
| 385 | |||||
| 386 | Create an alias from the class C<$from> (or the current package, if | ||||
| 387 | C<$from> is unspecified), so that | ||||
| 388 | L<Moose/Metaclass and Trait Name Resolution> works properly. | ||||
| 389 | |||||
| 390 | =back | ||||
| 391 | |||||
| 392 | =head1 TODO | ||||
| 393 | |||||
| 394 | Here is a list of possible functions to write | ||||
| 395 | |||||
| 396 | =over 4 | ||||
| 397 | |||||
| 398 | =item discovering original method from modified method | ||||
| 399 | |||||
| 400 | =item search for origin class of a method or attribute | ||||
| 401 | |||||
| 402 | =back | ||||
| 403 | |||||
| 404 | =head1 BUGS | ||||
| 405 | |||||
| 406 | See L<Moose/BUGS> for details on reporting bugs. | ||||
| 407 | |||||
| 408 | =head1 AUTHOR | ||||
| 409 | |||||
| 410 | Anders Nor Berle E<lt>debolaz@gmail.comE<gt> | ||||
| 411 | |||||
| 412 | B<with contributions from:> | ||||
| 413 | |||||
| 414 | Robert (phaylon) Sedlacek | ||||
| 415 | |||||
| 416 | Stevan Little | ||||
| 417 | |||||
| 418 | =head1 COPYRIGHT AND LICENSE | ||||
| 419 | |||||
| 420 | Copyright 2007-2009 by Infinity Interactive, Inc. | ||||
| 421 | |||||
| 422 | L<http://www.iinteractive.com> | ||||
| 423 | |||||
| 424 | This library is free software; you can redistribute it and/or modify | ||||
| 425 | it under the same terms as Perl itself. | ||||
| 426 | |||||
| 427 | =cut | ||||
| 428 |