Filename | /Users/dde/perl5/perlbrew/perls/5.18.0t/lib/site_perl/5.18.0/darwin-thread-multi-2level/Moose/Util/MetaRole.pm |
Statements | Executed 20 statements in 688µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 8µs | 8µs | BEGIN@2 | Moose::Util::MetaRole::
1 | 1 | 1 | 7µs | 12µs | BEGIN@16 | Moose::Util::MetaRole::
1 | 1 | 1 | 6µs | 26µs | BEGIN@11 | Moose::Util::MetaRole::
1 | 1 | 1 | 6µs | 11µs | BEGIN@10 | Moose::Util::MetaRole::
1 | 1 | 1 | 6µs | 24µs | BEGIN@13 | Moose::Util::MetaRole::
1 | 1 | 1 | 6µs | 22µs | BEGIN@15 | Moose::Util::MetaRole::
1 | 1 | 1 | 6µs | 22µs | BEGIN@17 | Moose::Util::MetaRole::
1 | 1 | 1 | 6µs | 17µs | BEGIN@9 | Moose::Util::MetaRole::
1 | 1 | 1 | 6µs | 22µs | BEGIN@14 | Moose::Util::MetaRole::
0 | 0 | 0 | 0s | 0s | __ANON__[:131] | Moose::Util::MetaRole::
0 | 0 | 0 | 0s | 0s | __ANON__[:87] | Moose::Util::MetaRole::
0 | 0 | 0 | 0s | 0s | _make_new_class | Moose::Util::MetaRole::
0 | 0 | 0 | 0s | 0s | _make_new_metaclass | Moose::Util::MetaRole::
0 | 0 | 0 | 0s | 0s | _metathing_for | Moose::Util::MetaRole::
0 | 0 | 0 | 0s | 0s | apply_base_class_roles | Moose::Util::MetaRole::
0 | 0 | 0 | 0s | 0s | apply_metaroles | Moose::Util::MetaRole::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Moose::Util::MetaRole; | ||||
2 | # spent 8µs within Moose::Util::MetaRole::BEGIN@2 which was called:
# once (8µs+0s) by Moose::Exporter::BEGIN@15 at line 4 | ||||
3 | 1 | 4µs | $Moose::Util::MetaRole::AUTHORITY = 'cpan:STEVAN'; | ||
4 | 1 | 26µs | 1 | 8µs | } # spent 8µs making 1 call to Moose::Util::MetaRole::BEGIN@2 |
5 | { | ||||
6 | 2 | 800ns | $Moose::Util::MetaRole::VERSION = '2.1005'; | ||
7 | } | ||||
8 | |||||
9 | 2 | 20µs | 2 | 28µs | # spent 17µs (6+11) within Moose::Util::MetaRole::BEGIN@9 which was called:
# once (6µs+11µs) by Moose::Exporter::BEGIN@15 at line 9 # spent 17µs making 1 call to Moose::Util::MetaRole::BEGIN@9
# spent 11µs making 1 call to strict::import |
10 | 2 | 20µs | 2 | 15µs | # spent 11µs (6+4) within Moose::Util::MetaRole::BEGIN@10 which was called:
# once (6µs+4µs) by Moose::Exporter::BEGIN@15 at line 10 # spent 11µs making 1 call to Moose::Util::MetaRole::BEGIN@10
# spent 4µs making 1 call to warnings::import |
11 | 2 | 22µs | 2 | 46µs | # spent 26µs (6+20) within Moose::Util::MetaRole::BEGIN@11 which was called:
# once (6µs+20µs) by Moose::Exporter::BEGIN@15 at line 11 # spent 26µs making 1 call to Moose::Util::MetaRole::BEGIN@11
# spent 20µs making 1 call to Exporter::import |
12 | |||||
13 | 2 | 21µs | 2 | 42µs | # spent 24µs (6+18) within Moose::Util::MetaRole::BEGIN@13 which was called:
# once (6µs+18µs) by Moose::Exporter::BEGIN@15 at line 13 # spent 24µs making 1 call to Moose::Util::MetaRole::BEGIN@13
# spent 18µs making 1 call to Exporter::import |
14 | 2 | 20µs | 2 | 39µs | # spent 22µs (6+17) within Moose::Util::MetaRole::BEGIN@14 which was called:
# once (6µs+17µs) by Moose::Exporter::BEGIN@15 at line 14 # spent 22µs making 1 call to Moose::Util::MetaRole::BEGIN@14
# spent 17µs making 1 call to Exporter::import |
15 | 2 | 19µs | 2 | 38µs | # spent 22µs (6+16) within Moose::Util::MetaRole::BEGIN@15 which was called:
# once (6µs+16µs) by Moose::Exporter::BEGIN@15 at line 15 # spent 22µs making 1 call to Moose::Util::MetaRole::BEGIN@15
# spent 16µs making 1 call to Exporter::import |
16 | 2 | 20µs | 2 | 18µs | # spent 12µs (7+5) within Moose::Util::MetaRole::BEGIN@16 which was called:
# once (7µs+5µs) by Moose::Exporter::BEGIN@15 at line 16 # spent 12µs making 1 call to Moose::Util::MetaRole::BEGIN@16
# spent 5µs making 1 call to Package::DeprecationManager::__ANON__[Package/DeprecationManager.pm:61] |
17 | 2 | 511µs | 2 | 37µs | # spent 22µs (6+16) within Moose::Util::MetaRole::BEGIN@17 which was called:
# once (6µs+16µs) by Moose::Exporter::BEGIN@15 at line 17 # spent 22µs making 1 call to Moose::Util::MetaRole::BEGIN@17
# spent 16µs making 1 call to Exporter::import |
18 | |||||
19 | sub apply_metaroles { | ||||
20 | my %args = @_; | ||||
21 | |||||
22 | my $for = _metathing_for( $args{for} ); | ||||
23 | |||||
24 | if ( $for->isa('Moose::Meta::Role') ) { | ||||
25 | return _make_new_metaclass( $for, $args{role_metaroles}, 'role' ); | ||||
26 | } | ||||
27 | else { | ||||
28 | return _make_new_metaclass( $for, $args{class_metaroles}, 'class' ); | ||||
29 | } | ||||
30 | } | ||||
31 | |||||
32 | sub _metathing_for { | ||||
33 | my $passed = shift; | ||||
34 | |||||
35 | my $found | ||||
36 | = blessed $passed | ||||
37 | ? $passed | ||||
38 | : Class::MOP::class_of($passed); | ||||
39 | |||||
40 | return $found | ||||
41 | if defined $found | ||||
42 | && blessed $found | ||||
43 | && ( $found->isa('Moose::Meta::Role') | ||||
44 | || $found->isa('Moose::Meta::Class') ); | ||||
45 | |||||
46 | local $Carp::CarpLevel = $Carp::CarpLevel + 1; | ||||
47 | |||||
48 | my $error_start | ||||
49 | = 'When using Moose::Util::MetaRole, you must pass a Moose class name,' | ||||
50 | . ' role name, metaclass object, or metarole object.'; | ||||
51 | |||||
52 | if ( defined $found && blessed $found ) { | ||||
53 | croak $error_start | ||||
54 | . " You passed $passed, and we resolved this to a " | ||||
55 | . ( blessed $found ) | ||||
56 | . ' object.'; | ||||
57 | } | ||||
58 | |||||
59 | if ( defined $passed && !defined $found ) { | ||||
60 | croak $error_start | ||||
61 | . " You passed $passed, and this did not resolve to a metaclass or metarole." | ||||
62 | . ' Maybe you need to call Moose->init_meta to initialize the metaclass first?'; | ||||
63 | } | ||||
64 | |||||
65 | if ( !defined $passed ) { | ||||
66 | croak $error_start | ||||
67 | . " You passed an undef." | ||||
68 | . ' Maybe you need to call Moose->init_meta to initialize the metaclass first?'; | ||||
69 | } | ||||
70 | } | ||||
71 | |||||
72 | sub _make_new_metaclass { | ||||
73 | my $for = shift; | ||||
74 | my $roles = shift; | ||||
75 | my $primary = shift; | ||||
76 | |||||
77 | return $for unless keys %{$roles}; | ||||
78 | |||||
79 | my $new_metaclass | ||||
80 | = exists $roles->{$primary} | ||||
81 | ? _make_new_class( ref $for, $roles->{$primary} ) | ||||
82 | : blessed $for; | ||||
83 | |||||
84 | my %classes; | ||||
85 | |||||
86 | for my $key ( grep { $_ ne $primary } keys %{$roles} ) { | ||||
87 | my $attr = first {$_} | ||||
88 | map { $for->meta->find_attribute_by_name($_) } ( | ||||
89 | $key . '_metaclass', | ||||
90 | $key . '_class' | ||||
91 | ); | ||||
92 | |||||
93 | my $reader = $attr->get_read_method; | ||||
94 | |||||
95 | $classes{ $attr->init_arg } | ||||
96 | = _make_new_class( $for->$reader(), $roles->{$key} ); | ||||
97 | } | ||||
98 | |||||
99 | my $new_meta = $new_metaclass->reinitialize( $for, %classes ); | ||||
100 | |||||
101 | return $new_meta; | ||||
102 | } | ||||
103 | |||||
104 | sub apply_base_class_roles { | ||||
105 | my %args = @_; | ||||
106 | |||||
107 | my $meta = _metathing_for( $args{for} || $args{for_class} ); | ||||
108 | croak 'You can only apply base class roles to a Moose class, not a role.' | ||||
109 | if $meta->isa('Moose::Meta::Role'); | ||||
110 | |||||
111 | my $new_base = _make_new_class( | ||||
112 | $meta->name, | ||||
113 | $args{roles}, | ||||
114 | [ $meta->superclasses() ], | ||||
115 | ); | ||||
116 | |||||
117 | $meta->superclasses($new_base) | ||||
118 | if $new_base ne $meta->name(); | ||||
119 | } | ||||
120 | |||||
121 | sub _make_new_class { | ||||
122 | my $existing_class = shift; | ||||
123 | my $roles = shift; | ||||
124 | my $superclasses = shift || [$existing_class]; | ||||
125 | |||||
126 | return $existing_class unless $roles; | ||||
127 | |||||
128 | my $meta = Class::MOP::Class->initialize($existing_class); | ||||
129 | |||||
130 | return $existing_class | ||||
131 | if $meta->can('does_role') && all { $meta->does_role($_) } | ||||
132 | grep { !ref $_ } @{$roles}; | ||||
133 | |||||
134 | return Moose::Meta::Class->create_anon_class( | ||||
135 | superclasses => $superclasses, | ||||
136 | roles => $roles, | ||||
137 | cache => 1, | ||||
138 | )->name(); | ||||
139 | } | ||||
140 | |||||
141 | 1 | 2µs | 1; | ||
142 | |||||
143 | # ABSTRACT: Apply roles to any metaclass, as well as the object base class | ||||
144 | |||||
145 | __END__ |