Filename | /2home/ss5/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/x86_64-linux/Moose/Util/MetaRole.pm |
Statements | Executed 28 statements in 728µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 22µs | 70µs | BEGIN@15 | Moose::Util::MetaRole::
1 | 1 | 1 | 9µs | 9µs | BEGIN@2 | Moose::Util::MetaRole::
1 | 1 | 1 | 9µs | 17µs | BEGIN@16 | Moose::Util::MetaRole::
1 | 1 | 1 | 9µs | 13µs | BEGIN@9 | Moose::Util::MetaRole::
1 | 1 | 1 | 8µs | 26µs | BEGIN@10 | Moose::Util::MetaRole::
1 | 1 | 1 | 8µs | 28µs | BEGIN@13 | Moose::Util::MetaRole::
1 | 1 | 1 | 7µs | 31µs | BEGIN@11 | Moose::Util::MetaRole::
1 | 1 | 1 | 6µs | 24µs | BEGIN@14 | Moose::Util::MetaRole::
1 | 1 | 1 | 6µs | 22µs | BEGIN@17 | 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 9µs within Moose::Util::MetaRole::BEGIN@2 which was called:
# once (9µs+0s) by Moose::Exporter::BEGIN@15 at line 4 | ||||
3 | 1 | 4µs | $Moose::Util::MetaRole::AUTHORITY = 'cpan:STEVAN'; | ||
4 | 1 | 23µs | 1 | 9µs | } # spent 9µs making 1 call to Moose::Util::MetaRole::BEGIN@2 |
5 | { | ||||
6 | 2 | 1µs | $Moose::Util::MetaRole::VERSION = '2.0602'; | ||
7 | } | ||||
8 | |||||
9 | 3 | 19µs | 2 | 18µs | # spent 13µs (9+5) within Moose::Util::MetaRole::BEGIN@9 which was called:
# once (9µs+5µs) by Moose::Exporter::BEGIN@15 at line 9 # spent 13µs making 1 call to Moose::Util::MetaRole::BEGIN@9
# spent 5µs making 1 call to strict::import |
10 | 3 | 19µs | 2 | 44µs | # spent 26µs (8+18) within Moose::Util::MetaRole::BEGIN@10 which was called:
# once (8µs+18µs) by Moose::Exporter::BEGIN@15 at line 10 # spent 26µs making 1 call to Moose::Util::MetaRole::BEGIN@10
# spent 18µs making 1 call to warnings::import |
11 | 3 | 20µs | 2 | 55µs | # spent 31µs (7+24) within Moose::Util::MetaRole::BEGIN@11 which was called:
# once (7µs+24µs) by Moose::Exporter::BEGIN@15 at line 11 # spent 31µs making 1 call to Moose::Util::MetaRole::BEGIN@11
# spent 24µs making 1 call to Exporter::import |
12 | |||||
13 | 3 | 19µs | 2 | 49µs | # spent 28µs (8+21) within Moose::Util::MetaRole::BEGIN@13 which was called:
# once (8µs+21µs) by Moose::Exporter::BEGIN@15 at line 13 # spent 28µs making 1 call to Moose::Util::MetaRole::BEGIN@13
# spent 21µs making 1 call to Exporter::import |
14 | 3 | 18µs | 2 | 41µs | # spent 24µs (6+18) within Moose::Util::MetaRole::BEGIN@14 which was called:
# once (6µs+18µs) by Moose::Exporter::BEGIN@15 at line 14 # spent 24µs making 1 call to Moose::Util::MetaRole::BEGIN@14
# spent 18µs making 1 call to Exporter::import |
15 | 3 | 22µs | 2 | 118µs | # spent 70µs (22+48) within Moose::Util::MetaRole::BEGIN@15 which was called:
# once (22µs+48µs) by Moose::Exporter::BEGIN@15 at line 15 # spent 70µs making 1 call to Moose::Util::MetaRole::BEGIN@15
# spent 48µs making 1 call to Exporter::import |
16 | 3 | 20µs | 2 | 26µs | # spent 17µs (9+8) within Moose::Util::MetaRole::BEGIN@16 which was called:
# once (9µs+8µs) by Moose::Exporter::BEGIN@15 at line 16 # spent 17µs making 1 call to Moose::Util::MetaRole::BEGIN@16
# spent 8µs making 1 call to Package::DeprecationManager::__ANON__[Package/DeprecationManager.pm:61] |
17 | 3 | 560µs | 2 | 39µs | # spent 22µs (6+17) within Moose::Util::MetaRole::BEGIN@17 which was called:
# once (6µs+17µs) by Moose::Exporter::BEGIN@15 at line 17 # spent 22µs making 1 call to Moose::Util::MetaRole::BEGIN@17
# spent 17µ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 | 3µs | 1; | ||
142 | |||||
143 | # ABSTRACT: Apply roles to any metaclass, as well as the object base class | ||||
144 | |||||
- - | |||||
147 | =pod | ||||
148 | |||||
149 | =head1 NAME | ||||
150 | |||||
151 | Moose::Util::MetaRole - Apply roles to any metaclass, as well as the object base class | ||||
152 | |||||
153 | =head1 VERSION | ||||
154 | |||||
155 | version 2.0602 | ||||
156 | |||||
157 | =head1 SYNOPSIS | ||||
158 | |||||
159 | package MyApp::Moose; | ||||
160 | |||||
161 | use Moose (); | ||||
162 | use Moose::Exporter; | ||||
163 | use Moose::Util::MetaRole; | ||||
164 | |||||
165 | use MyApp::Role::Meta::Class; | ||||
166 | use MyApp::Role::Meta::Method::Constructor; | ||||
167 | use MyApp::Role::Object; | ||||
168 | |||||
169 | Moose::Exporter->setup_import_methods( also => 'Moose' ); | ||||
170 | |||||
171 | sub init_meta { | ||||
172 | shift; | ||||
173 | my %args = @_; | ||||
174 | |||||
175 | Moose->init_meta(%args); | ||||
176 | |||||
177 | Moose::Util::MetaRole::apply_metaroles( | ||||
178 | for => $args{for_class}, | ||||
179 | class_metaroles => { | ||||
180 | class => => ['MyApp::Role::Meta::Class'], | ||||
181 | constructor => ['MyApp::Role::Meta::Method::Constructor'], | ||||
182 | }, | ||||
183 | ); | ||||
184 | |||||
185 | Moose::Util::MetaRole::apply_base_class_roles( | ||||
186 | for => $args{for_class}, | ||||
187 | roles => ['MyApp::Role::Object'], | ||||
188 | ); | ||||
189 | |||||
190 | return $args{for_class}->meta(); | ||||
191 | } | ||||
192 | |||||
193 | =head1 DESCRIPTION | ||||
194 | |||||
195 | This utility module is designed to help authors of Moose extensions | ||||
196 | write extensions that are able to cooperate with other Moose | ||||
197 | extensions. To do this, you must write your extensions as roles, which | ||||
198 | can then be dynamically applied to the caller's metaclasses. | ||||
199 | |||||
200 | This module makes sure to preserve any existing superclasses and roles | ||||
201 | already set for the meta objects, which means that any number of | ||||
202 | extensions can apply roles in any order. | ||||
203 | |||||
204 | =head1 USAGE | ||||
205 | |||||
206 | The easiest way to use this module is through L<Moose::Exporter>, which can | ||||
207 | generate the appropriate C<init_meta> method for you, and make sure it is | ||||
208 | called when imported. | ||||
209 | |||||
210 | =head1 FUNCTIONS | ||||
211 | |||||
212 | This module provides two functions. | ||||
213 | |||||
214 | =head2 apply_metaroles( ... ) | ||||
215 | |||||
216 | This function will apply roles to one or more metaclasses for the specified | ||||
217 | class. It will return a new metaclass object for the class or role passed in | ||||
218 | the "for" parameter. | ||||
219 | |||||
220 | It accepts the following parameters: | ||||
221 | |||||
222 | =over 4 | ||||
223 | |||||
224 | =item * for => $name | ||||
225 | |||||
226 | This specifies the class or for which to alter the meta classes. This can be a | ||||
227 | package name, or an appropriate meta-object (a L<Moose::Meta::Class> or | ||||
228 | L<Moose::Meta::Role>). | ||||
229 | |||||
230 | =item * class_metaroles => \%roles | ||||
231 | |||||
232 | This is a hash reference specifying which metaroles will be applied to the | ||||
233 | class metaclass and its contained metaclasses and helper classes. | ||||
234 | |||||
235 | Each key should in turn point to an array reference of role names. | ||||
236 | |||||
237 | It accepts the following keys: | ||||
238 | |||||
239 | =over 8 | ||||
240 | |||||
241 | =item class | ||||
242 | |||||
243 | =item attribute | ||||
244 | |||||
245 | =item method | ||||
246 | |||||
247 | =item wrapped_method | ||||
248 | |||||
249 | =item instance | ||||
250 | |||||
251 | =item constructor | ||||
252 | |||||
253 | =item destructor | ||||
254 | |||||
255 | =item error | ||||
256 | |||||
257 | =back | ||||
258 | |||||
259 | =item * role_metaroles => \%roles | ||||
260 | |||||
261 | This is a hash reference specifying which metaroles will be applied to the | ||||
262 | role metaclass and its contained metaclasses and helper classes. | ||||
263 | |||||
264 | It accepts the following keys: | ||||
265 | |||||
266 | =over 8 | ||||
267 | |||||
268 | =item role | ||||
269 | |||||
270 | =item attribute | ||||
271 | |||||
272 | =item method | ||||
273 | |||||
274 | =item required_method | ||||
275 | |||||
276 | =item conflicting_method | ||||
277 | |||||
278 | =item application_to_class | ||||
279 | |||||
280 | =item application_to_role | ||||
281 | |||||
282 | =item application_to_instance | ||||
283 | |||||
284 | =item application_role_summation | ||||
285 | |||||
286 | =item applied_attribute | ||||
287 | |||||
288 | =back | ||||
289 | |||||
290 | =back | ||||
291 | |||||
292 | =head2 apply_base_class_roles( for => $class, roles => \@roles ) | ||||
293 | |||||
294 | This function will apply the specified roles to the object's base class. | ||||
295 | |||||
296 | =head1 BUGS | ||||
297 | |||||
298 | See L<Moose/BUGS> for details on reporting bugs. | ||||
299 | |||||
300 | =head1 AUTHOR | ||||
301 | |||||
302 | Moose is maintained by the Moose Cabal, along with the help of many contributors. See L<Moose/CABAL> and L<Moose/CONTRIBUTORS> for details. | ||||
303 | |||||
304 | =head1 COPYRIGHT AND LICENSE | ||||
305 | |||||
306 | This software is copyright (c) 2012 by Infinity Interactive, Inc.. | ||||
307 | |||||
308 | This is free software; you can redistribute it and/or modify it under | ||||
309 | the same terms as the Perl 5 programming language system itself. | ||||
310 | |||||
311 | =cut | ||||
312 | |||||
313 | |||||
314 | __END__ |