| Filename | /home/ss5/perl5/perlbrew/perls/perl-5.22.0/lib/site_perl/5.22.0/x86_64-linux/Moose/Util/MetaRole.pm |
| Statements | Executed 15 statements in 484µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 1.68ms | 3.98ms | Moose::Util::MetaRole::BEGIN@10 |
| 1 | 1 | 1 | 14µs | 18µs | Moose::Util::MetaRole::BEGIN@4 |
| 1 | 1 | 1 | 13µs | 51µs | Moose::Util::MetaRole::BEGIN@8 |
| 1 | 1 | 1 | 9µs | 19µs | Moose::Util::MetaRole::BEGIN@5 |
| 1 | 1 | 1 | 8µs | 54µs | Moose::Util::MetaRole::BEGIN@6 |
| 1 | 1 | 1 | 6µs | 13µs | Moose::Util::MetaRole::BEGIN@9 |
| 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 | 700ns | our $VERSION = '2.1605'; | ||
| 3 | |||||
| 4 | 2 | 26µs | 2 | 21µs | # spent 18µs (14+3) within Moose::Util::MetaRole::BEGIN@4 which was called:
# once (14µs+3µs) by Moose::Exporter::BEGIN@10 at line 4 # spent 18µs making 1 call to Moose::Util::MetaRole::BEGIN@4
# spent 3µs making 1 call to strict::import |
| 5 | 2 | 20µs | 2 | 28µs | # spent 19µs (9+10) within Moose::Util::MetaRole::BEGIN@5 which was called:
# once (9µs+10µs) by Moose::Exporter::BEGIN@10 at line 5 # spent 19µs making 1 call to Moose::Util::MetaRole::BEGIN@5
# spent 10µs making 1 call to warnings::import |
| 6 | 2 | 26µs | 2 | 101µs | # spent 54µs (8+47) within Moose::Util::MetaRole::BEGIN@6 which was called:
# once (8µs+47µs) by Moose::Exporter::BEGIN@10 at line 6 # spent 54µs making 1 call to Moose::Util::MetaRole::BEGIN@6
# spent 46µs making 1 call to Exporter::import |
| 7 | |||||
| 8 | 3 | 34µs | 3 | 70µs | # spent 51µs (13+37) within Moose::Util::MetaRole::BEGIN@8 which was called:
# once (13µs+37µs) by Moose::Exporter::BEGIN@10 at line 8 # spent 51µs making 1 call to Moose::Util::MetaRole::BEGIN@8
# spent 11µs making 1 call to UNIVERSAL::VERSION
# spent 8µs making 1 call to List::Util::import |
| 9 | 2 | 17µs | 2 | 20µs | # spent 13µs (6+7) within Moose::Util::MetaRole::BEGIN@9 which was called:
# once (6µs+7µs) by Moose::Exporter::BEGIN@10 at line 9 # spent 13µs making 1 call to Moose::Util::MetaRole::BEGIN@9
# spent 7µs making 1 call to Package::DeprecationManager::__ANON__[Package/DeprecationManager.pm:87] |
| 10 | 2 | 356µs | 2 | 4.06ms | # spent 3.98ms (1.68+2.30) within Moose::Util::MetaRole::BEGIN@10 which was called:
# once (1.68ms+2.30ms) by Moose::Exporter::BEGIN@10 at line 10 # spent 3.98ms making 1 call to Moose::Util::MetaRole::BEGIN@10
# spent 80µ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 | 2µs | 1; | ||
| 116 | |||||
| 117 | # ABSTRACT: Apply roles to any metaclass, as well as the object base class | ||||
| 118 | |||||
| 119 | __END__ |