| Filename | /usr/local/lib/perl/5.18.2/Moose/Util/MetaRole.pm |
| Statements | Executed 15 statements in 617µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 2.22ms | 2.98ms | Moose::Util::MetaRole::BEGIN@10 |
| 1 | 1 | 1 | 18µs | 38µs | Moose::Util::MetaRole::BEGIN@6 |
| 1 | 1 | 1 | 18µs | 60µs | Moose::Util::MetaRole::BEGIN@8 |
| 1 | 1 | 1 | 11µs | 24µs | Moose::Util::MetaRole::BEGIN@4 |
| 1 | 1 | 1 | 8µs | 14µs | Moose::Util::MetaRole::BEGIN@9 |
| 1 | 1 | 1 | 7µs | 13µs | Moose::Util::MetaRole::BEGIN@5 |
| 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 | 400ns | our $VERSION = '2.1604'; | ||
| 3 | |||||
| 4 | 2 | 21µs | 2 | 37µs | # spent 24µs (11+13) within Moose::Util::MetaRole::BEGIN@4 which was called:
# once (11µs+13µs) by Moose::Exporter::BEGIN@10 at line 4 # spent 24µs making 1 call to Moose::Util::MetaRole::BEGIN@4
# spent 13µs making 1 call to strict::import |
| 5 | 2 | 20µs | 2 | 19µs | # spent 13µs (7+6) within Moose::Util::MetaRole::BEGIN@5 which was called:
# once (7µs+6µs) by Moose::Exporter::BEGIN@10 at line 5 # spent 13µs making 1 call to Moose::Util::MetaRole::BEGIN@5
# spent 6µs making 1 call to warnings::import |
| 6 | 2 | 28µs | 2 | 58µs | # spent 38µs (18+20) within Moose::Util::MetaRole::BEGIN@6 which was called:
# once (18µs+20µs) by Moose::Exporter::BEGIN@10 at line 6 # spent 38µs making 1 call to Moose::Util::MetaRole::BEGIN@6
# spent 20µs making 1 call to Exporter::import |
| 7 | |||||
| 8 | 3 | 44µs | 3 | 82µs | # spent 60µs (18+42) within Moose::Util::MetaRole::BEGIN@8 which was called:
# once (18µs+42µs) by Moose::Exporter::BEGIN@10 at line 8 # spent 60µs making 1 call to Moose::Util::MetaRole::BEGIN@8
# spent 14µs making 1 call to UNIVERSAL::VERSION
# spent 8µs making 1 call to List::Util::import |
| 9 | 2 | 20µs | 2 | 19µs | # spent 14µs (8+6) within Moose::Util::MetaRole::BEGIN@9 which was called:
# once (8µs+6µs) by Moose::Exporter::BEGIN@10 at line 9 # spent 14µs making 1 call to Moose::Util::MetaRole::BEGIN@9
# spent 6µs making 1 call to Package::DeprecationManager::__ANON__[Package/DeprecationManager.pm:61] |
| 10 | 2 | 482µs | 2 | 3.09ms | # spent 2.98ms (2.22+756µs) within Moose::Util::MetaRole::BEGIN@10 which was called:
# once (2.22ms+756µs) by Moose::Exporter::BEGIN@10 at line 10 # spent 2.98ms making 1 call to Moose::Util::MetaRole::BEGIN@10
# spent 115µ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__ |