| Filename | /Users/ap13/perl5/lib/perl5/darwin-2level/Moose/Util/MetaRole.pm |
| Statements | Executed 15 statements in 1.35ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 5.17ms | 6.77ms | Moose::Util::MetaRole::BEGIN@10 |
| 1 | 1 | 1 | 32µs | 116µs | Moose::Util::MetaRole::BEGIN@8 |
| 1 | 1 | 1 | 27µs | 56µs | Moose::Util::MetaRole::BEGIN@4 |
| 1 | 1 | 1 | 16µs | 28µs | Moose::Util::MetaRole::BEGIN@5 |
| 1 | 1 | 1 | 16µs | 29µs | Moose::Util::MetaRole::BEGIN@9 |
| 1 | 1 | 1 | 16µs | 63µs | Moose::Util::MetaRole::BEGIN@6 |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::MetaRole::__ANON__[:105] |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::MetaRole::__ANON__[:59] |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::MetaRole::_make_new_class |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::MetaRole::_make_new_metaclass |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::MetaRole::_metathing_for |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::MetaRole::apply_base_class_roles |
| 0 | 0 | 0 | 0s | 0s | Moose::Util::MetaRole::apply_metaroles |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package Moose::Util::MetaRole; | ||||
| 2 | 1 | 1µs | our $VERSION = '2.1403'; | ||
| 3 | |||||
| 4 | 2 | 46µs | 2 | 86µs | # spent 56µs (27+29) within Moose::Util::MetaRole::BEGIN@4 which was called:
# once (27µs+29µs) by Moose::Exporter::BEGIN@10 at line 4 # spent 56µs making 1 call to Moose::Util::MetaRole::BEGIN@4
# spent 29µs making 1 call to strict::import |
| 5 | 2 | 45µs | 2 | 39µs | # spent 28µs (16+11) within Moose::Util::MetaRole::BEGIN@5 which was called:
# once (16µs+11µs) by Moose::Exporter::BEGIN@10 at line 5 # spent 28µs making 1 call to Moose::Util::MetaRole::BEGIN@5
# spent 11µs making 1 call to warnings::import |
| 6 | 2 | 55µs | 2 | 110µs | # spent 63µs (16+47) within Moose::Util::MetaRole::BEGIN@6 which was called:
# once (16µs+47µs) by Moose::Exporter::BEGIN@10 at line 6 # spent 63µs making 1 call to Moose::Util::MetaRole::BEGIN@6
# spent 47µs making 1 call to Exporter::import |
| 7 | |||||
| 8 | 3 | 82µs | 3 | 157µs | # spent 116µs (32+85) within Moose::Util::MetaRole::BEGIN@8 which was called:
# once (32µs+85µs) by Moose::Exporter::BEGIN@10 at line 8 # spent 116µs making 1 call to Moose::Util::MetaRole::BEGIN@8
# spent 24µs making 1 call to UNIVERSAL::VERSION
# spent 17µs making 1 call to List::Util::import |
| 9 | 2 | 44µs | 2 | 41µs | # spent 29µs (16+12) within Moose::Util::MetaRole::BEGIN@9 which was called:
# once (16µs+12µs) by Moose::Exporter::BEGIN@10 at line 9 # spent 29µs making 1 call to Moose::Util::MetaRole::BEGIN@9
# spent 12µs making 1 call to Package::DeprecationManager::__ANON__[Package/DeprecationManager.pm:61] |
| 10 | 2 | 1.07ms | 2 | 7.04ms | # spent 6.77ms (5.17+1.60) within Moose::Util::MetaRole::BEGIN@10 which was called:
# once (5.17ms+1.60ms) by Moose::Exporter::BEGIN@10 at line 10 # spent 6.77ms making 1 call to Moose::Util::MetaRole::BEGIN@10
# spent 271µs making 1 call to Sub::Exporter::__ANON__[Sub/Exporter.pm:337] |
| 11 | |||||
| 12 | sub apply_metaroles { | ||||
| 13 | my %args = @_; | ||||
| 14 | |||||
| 15 | my $for = _metathing_for( $args{for} ); | ||||
| 16 | |||||
| 17 | if ( $for->isa('Moose::Meta::Role') ) { | ||||
| 18 | return _make_new_metaclass( $for, $args{role_metaroles}, 'role' ); | ||||
| 19 | } | ||||
| 20 | else { | ||||
| 21 | return _make_new_metaclass( $for, $args{class_metaroles}, 'class' ); | ||||
| 22 | } | ||||
| 23 | } | ||||
| 24 | |||||
| 25 | sub _metathing_for { | ||||
| 26 | my $passed = shift; | ||||
| 27 | |||||
| 28 | my $found | ||||
| 29 | = blessed $passed | ||||
| 30 | ? $passed | ||||
| 31 | : Class::MOP::class_of($passed); | ||||
| 32 | |||||
| 33 | return $found | ||||
| 34 | if defined $found | ||||
| 35 | && blessed $found | ||||
| 36 | && ( $found->isa('Moose::Meta::Role') | ||||
| 37 | || $found->isa('Moose::Meta::Class') ); | ||||
| 38 | |||||
| 39 | local $Carp::CarpLevel = $Carp::CarpLevel + 1; | ||||
| 40 | |||||
| 41 | throw_exception( InvalidArgPassedToMooseUtilMetaRole => argument => $passed ); | ||||
| 42 | } | ||||
| 43 | |||||
| 44 | sub _make_new_metaclass { | ||||
| 45 | my $for = shift; | ||||
| 46 | my $roles = shift; | ||||
| 47 | my $primary = shift; | ||||
| 48 | |||||
| 49 | return $for unless keys %{$roles}; | ||||
| 50 | |||||
| 51 | my $new_metaclass | ||||
| 52 | = exists $roles->{$primary} | ||||
| 53 | ? _make_new_class( ref $for, $roles->{$primary} ) | ||||
| 54 | : blessed $for; | ||||
| 55 | |||||
| 56 | my %classes; | ||||
| 57 | |||||
| 58 | for my $key ( grep { $_ ne $primary } keys %{$roles} ) { | ||||
| 59 | my $attr = first {$_} | ||||
| 60 | map { $for->meta->find_attribute_by_name($_) } ( | ||||
| 61 | $key . '_metaclass', | ||||
| 62 | $key . '_class' | ||||
| 63 | ); | ||||
| 64 | |||||
| 65 | my $reader = $attr->get_read_method; | ||||
| 66 | |||||
| 67 | $classes{ $attr->init_arg } | ||||
| 68 | = _make_new_class( $for->$reader(), $roles->{$key} ); | ||||
| 69 | } | ||||
| 70 | |||||
| 71 | my $new_meta = $new_metaclass->reinitialize( $for, %classes ); | ||||
| 72 | |||||
| 73 | return $new_meta; | ||||
| 74 | } | ||||
| 75 | |||||
| 76 | sub apply_base_class_roles { | ||||
| 77 | my %args = @_; | ||||
| 78 | |||||
| 79 | my $meta = _metathing_for( $args{for} || $args{for_class} ); | ||||
| 80 | throw_exception( CannotApplyBaseClassRolesToRole => params => \%args, | ||||
| 81 | role_name => $meta->name, | ||||
| 82 | ) | ||||
| 83 | if $meta->isa('Moose::Meta::Role'); | ||||
| 84 | |||||
| 85 | my $new_base = _make_new_class( | ||||
| 86 | $meta->name, | ||||
| 87 | $args{roles}, | ||||
| 88 | [ $meta->superclasses() ], | ||||
| 89 | ); | ||||
| 90 | |||||
| 91 | $meta->superclasses($new_base) | ||||
| 92 | if $new_base ne $meta->name(); | ||||
| 93 | } | ||||
| 94 | |||||
| 95 | sub _make_new_class { | ||||
| 96 | my $existing_class = shift; | ||||
| 97 | my $roles = shift; | ||||
| 98 | my $superclasses = shift || [$existing_class]; | ||||
| 99 | |||||
| 100 | return $existing_class unless $roles; | ||||
| 101 | |||||
| 102 | my $meta = Class::MOP::Class->initialize($existing_class); | ||||
| 103 | |||||
| 104 | return $existing_class | ||||
| 105 | if $meta->can('does_role') && all { $meta->does_role($_) } | ||||
| 106 | grep { !ref $_ } @{$roles}; | ||||
| 107 | |||||
| 108 | return Moose::Meta::Class->create_anon_class( | ||||
| 109 | superclasses => $superclasses, | ||||
| 110 | roles => $roles, | ||||
| 111 | cache => 1, | ||||
| 112 | )->name(); | ||||
| 113 | } | ||||
| 114 | |||||
| 115 | 1 | 7µs | 1; | ||
| 116 | |||||
| 117 | # ABSTRACT: Apply roles to any metaclass, as well as the object base class | ||||
| 118 | |||||
| 119 | __END__ |