Filename | /opt/perl-5.18.1/lib/site_perl/5.18.1/darwin-thread-multi-2level/Moose/Meta/Role.pm |
Statements | Executed 82 statements in 4.76ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 1.00ms | 3.81ms | BEGIN@19 | Moose::Meta::Role::
1 | 1 | 1 | 862µs | 1.91ms | BEGIN@22 | Moose::Meta::Role::
1 | 1 | 1 | 397µs | 1.49ms | BEGIN@21 | Moose::Meta::Role::
1 | 1 | 1 | 299µs | 433µs | BEGIN@20 | Moose::Meta::Role::
1 | 1 | 1 | 18µs | 90µs | BEGIN@16 | Moose::Meta::Role::
1 | 1 | 1 | 11µs | 11µs | BEGIN@2 | Moose::Meta::Role::
1 | 1 | 1 | 10µs | 47µs | BEGIN@13 | Moose::Meta::Role::
1 | 1 | 1 | 9µs | 37µs | BEGIN@14 | Moose::Meta::Role::
1 | 1 | 1 | 9µs | 14µs | BEGIN@10 | Moose::Meta::Role::
1 | 1 | 1 | 9µs | 36µs | BEGIN@15 | Moose::Meta::Role::
1 | 1 | 1 | 9µs | 192µs | BEGIN@27 | Moose::Meta::Role::
1 | 1 | 1 | 8µs | 23µs | BEGIN@9 | Moose::Meta::Role::
1 | 1 | 1 | 8µs | 503µs | BEGIN@11 | Moose::Meta::Role::
1 | 1 | 1 | 8µs | 184µs | BEGIN@24 | Moose::Meta::Role::
1 | 1 | 1 | 5µs | 5µs | BEGIN@23 | Moose::Meta::Role::
1 | 1 | 1 | 5µs | 5µs | BEGIN@18 | Moose::Meta::Role::
1 | 1 | 1 | 5µs | 5µs | BEGIN@25 | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | __ANON__[:104] | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | __ANON__[:109] | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | __ANON__[:114] | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | __ANON__[:119] | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | __ANON__[:124] | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | __ANON__[:308] | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | __ANON__[:318] | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | __ANON__[:326] | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | __ANON__[:345] | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | __ANON__[:354] | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | __ANON__[:400] | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | __ANON__[:583] | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | __ANON__[:91] | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | __ANON__[:99] | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | _anon_cache_key | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | _anon_package_prefix | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | _attach_attribute | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | _meta_method_class | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | _restore_metaobjects_from | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | _role_for_combination | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | add_attribute | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | add_conflicting_method | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | add_override_method_modifier | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | add_required_methods | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | add_role | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | apply | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | calculate_all_roles | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | combine | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | composition_class_roles | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | consumers | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | create | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | create_anon_role | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | does_role | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | find_method_by_name | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | get_method_modifier_list | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | get_override_method_modifier | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | has_override_method_modifier | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | initialize | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | is_anon_role | Moose::Meta::Role::
0 | 0 | 0 | 0s | 0s | reinitialize | Moose::Meta::Role::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Moose::Meta::Role; | ||||
2 | # spent 11µs within Moose::Meta::Role::BEGIN@2 which was called:
# once (11µs+0s) by Moose::BEGIN@35 at line 4 | ||||
3 | 1 | 7µs | $Moose::Meta::Role::AUTHORITY = 'cpan:STEVAN'; | ||
4 | 1 | 38µs | 1 | 11µs | } # spent 11µs making 1 call to Moose::Meta::Role::BEGIN@2 |
5 | { | ||||
6 | 2 | 1µs | $Moose::Meta::Role::VERSION = '2.1005'; | ||
7 | } | ||||
8 | |||||
9 | 2 | 29µs | 2 | 38µs | # spent 23µs (8+15) within Moose::Meta::Role::BEGIN@9 which was called:
# once (8µs+15µs) by Moose::BEGIN@35 at line 9 # spent 23µs making 1 call to Moose::Meta::Role::BEGIN@9
# spent 15µs making 1 call to strict::import |
10 | 2 | 27µs | 2 | 18µs | # spent 14µs (9+5) within Moose::Meta::Role::BEGIN@10 which was called:
# once (9µs+5µs) by Moose::BEGIN@35 at line 10 # spent 14µs making 1 call to Moose::Meta::Role::BEGIN@10
# spent 5µs making 1 call to warnings::import |
11 | 2 | 38µs | 2 | 998µs | # spent 503µs (8+495) within Moose::Meta::Role::BEGIN@11 which was called:
# once (8µs+495µs) by Moose::BEGIN@35 at line 11 # spent 503µs making 1 call to Moose::Meta::Role::BEGIN@11
# spent 495µs making 1 call to metaclass::import |
12 | |||||
13 | 2 | 34µs | 2 | 84µs | # spent 47µs (10+37) within Moose::Meta::Role::BEGIN@13 which was called:
# once (10µs+37µs) by Moose::BEGIN@35 at line 13 # spent 47µs making 1 call to Moose::Meta::Role::BEGIN@13
# spent 37µs making 1 call to Exporter::import |
14 | 2 | 32µs | 2 | 65µs | # spent 37µs (9+28) within Moose::Meta::Role::BEGIN@14 which was called:
# once (9µs+28µs) by Moose::BEGIN@35 at line 14 # spent 37µs making 1 call to Moose::Meta::Role::BEGIN@14
# spent 28µs making 1 call to Exporter::import |
15 | 2 | 32µs | 2 | 63µs | # spent 36µs (9+27) within Moose::Meta::Role::BEGIN@15 which was called:
# once (9µs+27µs) by Moose::BEGIN@35 at line 15 # spent 36µs making 1 call to Moose::Meta::Role::BEGIN@15
# spent 27µs making 1 call to Exporter::import |
16 | 2 | 37µs | 2 | 136µs | # spent 90µs (18+72) within Moose::Meta::Role::BEGIN@16 which was called:
# once (18µs+72µs) by Moose::BEGIN@35 at line 16 # spent 90µs making 1 call to Moose::Meta::Role::BEGIN@16
# spent 47µs making 1 call to Sub::Exporter::Progressive::__ANON__[Sub/Exporter/Progressive.pm:40] |
17 | |||||
18 | 2 | 27µs | 1 | 5µs | # spent 5µs within Moose::Meta::Role::BEGIN@18 which was called:
# once (5µs+0s) by Moose::BEGIN@35 at line 18 # spent 5µs making 1 call to Moose::Meta::Role::BEGIN@18 |
19 | 2 | 157µs | 1 | 3.81ms | # spent 3.81ms (1.00+2.81) within Moose::Meta::Role::BEGIN@19 which was called:
# once (1.00ms+2.81ms) by Moose::BEGIN@35 at line 19 # spent 3.81ms making 1 call to Moose::Meta::Role::BEGIN@19 |
20 | 2 | 143µs | 1 | 433µs | # spent 433µs (299+134) within Moose::Meta::Role::BEGIN@20 which was called:
# once (299µs+134µs) by Moose::BEGIN@35 at line 20 # spent 433µs making 1 call to Moose::Meta::Role::BEGIN@20 |
21 | 2 | 130µs | 1 | 1.49ms | # spent 1.49ms (397µs+1.09) within Moose::Meta::Role::BEGIN@21 which was called:
# once (397µs+1.09ms) by Moose::BEGIN@35 at line 21 # spent 1.49ms making 1 call to Moose::Meta::Role::BEGIN@21 |
22 | 2 | 141µs | 1 | 1.91ms | # spent 1.91ms (862µs+1.05) within Moose::Meta::Role::BEGIN@22 which was called:
# once (862µs+1.05ms) by Moose::BEGIN@35 at line 22 # spent 1.91ms making 1 call to Moose::Meta::Role::BEGIN@22 |
23 | 2 | 33µs | 1 | 5µs | # spent 5µs within Moose::Meta::Role::BEGIN@23 which was called:
# once (5µs+0s) by Moose::BEGIN@35 at line 23 # spent 5µs making 1 call to Moose::Meta::Role::BEGIN@23 |
24 | 2 | 36µs | 2 | 360µs | # spent 184µs (8+176) within Moose::Meta::Role::BEGIN@24 which was called:
# once (8µs+176µs) by Moose::BEGIN@35 at line 24 # spent 184µs making 1 call to Moose::Meta::Role::BEGIN@24
# spent 176µs making 1 call to Sub::Exporter::__ANON__[Sub/Exporter.pm:337] |
25 | 2 | 36µs | 1 | 5µs | # spent 5µs within Moose::Meta::Role::BEGIN@25 which was called:
# once (5µs+0s) by Moose::BEGIN@35 at line 25 # spent 5µs making 1 call to Moose::Meta::Role::BEGIN@25 |
26 | |||||
27 | 1 | 500ns | # spent 192µs (9+183) within Moose::Meta::Role::BEGIN@27 which was called:
# once (9µs+183µs) by Moose::BEGIN@35 at line 29 | ||
28 | 'Class::MOP::Mixin::HasAttributes', | ||||
29 | 1 | 3.60ms | 2 | 375µs | 'Class::MOP::Mixin::HasMethods'; # spent 192µs making 1 call to Moose::Meta::Role::BEGIN@27
# spent 183µs making 1 call to base::import |
30 | |||||
31 | 1 | 2µs | 1 | 7.51ms | Class::MOP::MiniTrait::apply(__PACKAGE__, 'Moose::Meta::Object::Trait'); # spent 7.51ms making 1 call to Class::MOP::MiniTrait::apply |
32 | |||||
33 | ## ------------------------------------------------------------------ | ||||
34 | ## NOTE: | ||||
35 | ## I normally don't do this, but I am doing | ||||
36 | ## a whole bunch of meta-programmin' in this | ||||
37 | ## module, so it just makes sense. For a clearer | ||||
38 | ## picture of what is going on in the next | ||||
39 | ## several lines of code, look at the really | ||||
40 | ## big comment at the end of this file (right | ||||
41 | ## before the POD). | ||||
42 | ## - SL | ||||
43 | ## ------------------------------------------------------------------ | ||||
44 | |||||
45 | 1 | 2µs | 1 | 15µs | my $META = __PACKAGE__->meta; # spent 15µs making 1 call to Moose::Meta::Role::meta |
46 | |||||
47 | ## ------------------------------------------------------------------ | ||||
48 | ## attributes ... | ||||
49 | |||||
50 | # NOTE: | ||||
51 | # since roles are lazy, we hold all the attributes | ||||
52 | # of the individual role in 'stasis' until which | ||||
53 | # time when it is applied to a class. This means | ||||
54 | # keeping a lot of things in hash maps, so we are | ||||
55 | # using a little of that meta-programmin' magic | ||||
56 | # here and saving lots of extra typin'. And since | ||||
57 | # many of these attributes above require similar | ||||
58 | # functionality to support them, so we again use | ||||
59 | # the wonders of meta-programmin' to deliver a | ||||
60 | # very compact solution to this normally verbose | ||||
61 | # problem. | ||||
62 | # - SL | ||||
63 | |||||
64 | 1 | 8µs | foreach my $action ( | ||
65 | { | ||||
66 | name => 'excluded_roles_map', | ||||
67 | attr_reader => 'get_excluded_roles_map' , | ||||
68 | methods => { | ||||
69 | add => 'add_excluded_roles', | ||||
70 | get_keys => 'get_excluded_roles_list', | ||||
71 | existence => 'excludes_role', | ||||
72 | } | ||||
73 | }, | ||||
74 | { | ||||
75 | name => 'required_methods', | ||||
76 | attr_reader => 'get_required_methods_map', | ||||
77 | methods => { | ||||
78 | remove => 'remove_required_methods', | ||||
79 | get_values => 'get_required_method_list', | ||||
80 | existence => 'requires_method', | ||||
81 | } | ||||
82 | }, | ||||
83 | ) { | ||||
84 | |||||
85 | 2 | 1µs | my $attr_reader = $action->{attr_reader}; | ||
86 | 2 | 400ns | my $methods = $action->{methods}; | ||
87 | |||||
88 | # create the attribute | ||||
89 | $META->add_attribute($action->{name} => ( | ||||
90 | reader => $attr_reader, | ||||
91 | default => sub { {} }, | ||||
92 | 2 | 9µs | 4 | 788µs | Class::MOP::_definition_context(), # spent 775µs making 2 calls to Class::MOP::Mixin::HasAttributes::add_attribute, avg 387µs/call
# spent 14µs making 2 calls to Class::MOP::_definition_context, avg 7µs/call |
93 | )); | ||||
94 | |||||
95 | # create some helper methods | ||||
96 | $META->add_method($methods->{add} => sub { | ||||
97 | my ($self, @values) = @_; | ||||
98 | $self->$attr_reader->{$_} = undef foreach @values; | ||||
99 | 2 | 5µs | 1 | 58µs | }) if exists $methods->{add}; # spent 58µs making 1 call to Class::MOP::Mixin::HasMethods::add_method |
100 | |||||
101 | $META->add_method($methods->{get_keys} => sub { | ||||
102 | my ($self) = @_; | ||||
103 | keys %{$self->$attr_reader}; | ||||
104 | 2 | 4µs | 1 | 54µs | }) if exists $methods->{get_keys}; # spent 54µs making 1 call to Class::MOP::Mixin::HasMethods::add_method |
105 | |||||
106 | $META->add_method($methods->{get_values} => sub { | ||||
107 | my ($self) = @_; | ||||
108 | values %{$self->$attr_reader}; | ||||
109 | 2 | 5µs | 1 | 57µs | }) if exists $methods->{get_values}; # spent 57µs making 1 call to Class::MOP::Mixin::HasMethods::add_method |
110 | |||||
111 | $META->add_method($methods->{get} => sub { | ||||
112 | my ($self, $name) = @_; | ||||
113 | $self->$attr_reader->{$name} | ||||
114 | 2 | 600ns | }) if exists $methods->{get}; | ||
115 | |||||
116 | $META->add_method($methods->{existence} => sub { | ||||
117 | my ($self, $name) = @_; | ||||
118 | exists $self->$attr_reader->{$name} ? 1 : 0; | ||||
119 | 2 | 7µs | 2 | 104µs | }) if exists $methods->{existence}; # spent 104µs making 2 calls to Class::MOP::Mixin::HasMethods::add_method, avg 52µs/call |
120 | |||||
121 | $META->add_method($methods->{remove} => sub { | ||||
122 | my ($self, @values) = @_; | ||||
123 | delete $self->$attr_reader->{$_} foreach @values; | ||||
124 | 2 | 6µs | 1 | 54µs | }) if exists $methods->{remove}; # spent 54µs making 1 call to Class::MOP::Mixin::HasMethods::add_method |
125 | } | ||||
126 | |||||
127 | $META->add_attribute( | ||||
128 | 1 | 3µs | 2 | 387µs | 'method_metaclass', # spent 380µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 6µs making 1 call to Class::MOP::_definition_context |
129 | reader => 'method_metaclass', | ||||
130 | default => 'Moose::Meta::Role::Method', | ||||
131 | Class::MOP::_definition_context(), | ||||
132 | ); | ||||
133 | |||||
134 | 1 | 3µs | 2 | 389µs | $META->add_attribute( # spent 383µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 7µs making 1 call to Class::MOP::_definition_context |
135 | 'required_method_metaclass', | ||||
136 | reader => 'required_method_metaclass', | ||||
137 | default => 'Moose::Meta::Role::Method::Required', | ||||
138 | Class::MOP::_definition_context(), | ||||
139 | ); | ||||
140 | |||||
141 | 1 | 3µs | 2 | 376µs | $META->add_attribute( # spent 369µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 7µs making 1 call to Class::MOP::_definition_context |
142 | 'conflicting_method_metaclass', | ||||
143 | reader => 'conflicting_method_metaclass', | ||||
144 | default => 'Moose::Meta::Role::Method::Conflicting', | ||||
145 | Class::MOP::_definition_context(), | ||||
146 | ); | ||||
147 | |||||
148 | 1 | 3µs | 2 | 367µs | $META->add_attribute( # spent 361µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 6µs making 1 call to Class::MOP::_definition_context |
149 | 'application_to_class_class', | ||||
150 | reader => 'application_to_class_class', | ||||
151 | default => 'Moose::Meta::Role::Application::ToClass', | ||||
152 | Class::MOP::_definition_context(), | ||||
153 | ); | ||||
154 | |||||
155 | 1 | 3µs | 2 | 389µs | $META->add_attribute( # spent 383µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 6µs making 1 call to Class::MOP::_definition_context |
156 | 'application_to_role_class', | ||||
157 | reader => 'application_to_role_class', | ||||
158 | default => 'Moose::Meta::Role::Application::ToRole', | ||||
159 | Class::MOP::_definition_context(), | ||||
160 | ); | ||||
161 | |||||
162 | 1 | 3µs | 2 | 371µs | $META->add_attribute( # spent 364µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 7µs making 1 call to Class::MOP::_definition_context |
163 | 'application_to_instance_class', | ||||
164 | reader => 'application_to_instance_class', | ||||
165 | default => 'Moose::Meta::Role::Application::ToInstance', | ||||
166 | Class::MOP::_definition_context(), | ||||
167 | ); | ||||
168 | |||||
169 | 1 | 3µs | 2 | 385µs | $META->add_attribute( # spent 378µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 6µs making 1 call to Class::MOP::_definition_context |
170 | 'applied_attribute_metaclass', | ||||
171 | reader => 'applied_attribute_metaclass', | ||||
172 | default => 'Moose::Meta::Attribute', | ||||
173 | Class::MOP::_definition_context(), | ||||
174 | ); | ||||
175 | |||||
176 | # More or less copied from Moose::Meta::Class | ||||
177 | sub initialize { | ||||
178 | my $class = shift; | ||||
179 | my @args = @_; | ||||
180 | unshift @args, 'package' if @args % 2; | ||||
181 | my %opts = @args; | ||||
182 | my $package = delete $opts{package}; | ||||
183 | return Class::MOP::get_metaclass_by_name($package) | ||||
184 | || $class->SUPER::initialize($package, | ||||
185 | 'attribute_metaclass' => 'Moose::Meta::Role::Attribute', | ||||
186 | %opts, | ||||
187 | ); | ||||
188 | } | ||||
189 | |||||
190 | sub reinitialize { | ||||
191 | my $self = shift; | ||||
192 | my $pkg = shift; | ||||
193 | |||||
194 | my $meta = blessed $pkg ? $pkg : Class::MOP::class_of($pkg); | ||||
195 | |||||
196 | my %existing_classes; | ||||
197 | if ($meta) { | ||||
198 | %existing_classes = map { $_ => $meta->$_() } qw( | ||||
199 | attribute_metaclass | ||||
200 | method_metaclass | ||||
201 | wrapped_method_metaclass | ||||
202 | required_method_metaclass | ||||
203 | conflicting_method_metaclass | ||||
204 | application_to_class_class | ||||
205 | application_to_role_class | ||||
206 | application_to_instance_class | ||||
207 | applied_attribute_metaclass | ||||
208 | ); | ||||
209 | } | ||||
210 | |||||
211 | my %options = @_; | ||||
212 | $options{weaken} = Class::MOP::metaclass_is_weak($meta->name) | ||||
213 | if !exists $options{weaken} | ||||
214 | && blessed($meta) | ||||
215 | && $meta->isa('Moose::Meta::Role'); | ||||
216 | |||||
217 | # don't need to remove generated metaobjects here yet, since we don't | ||||
218 | # yet generate anything in roles. this may change in the future though... | ||||
219 | # keep an eye on that | ||||
220 | my $new_meta = $self->SUPER::reinitialize( | ||||
221 | $pkg, | ||||
222 | %existing_classes, | ||||
223 | %options, | ||||
224 | ); | ||||
225 | $new_meta->_restore_metaobjects_from($meta) | ||||
226 | if $meta && $meta->isa('Moose::Meta::Role'); | ||||
227 | return $new_meta; | ||||
228 | } | ||||
229 | |||||
230 | sub _restore_metaobjects_from { | ||||
231 | my $self = shift; | ||||
232 | my ($old_meta) = @_; | ||||
233 | |||||
234 | $self->_restore_metamethods_from($old_meta); | ||||
235 | $self->_restore_metaattributes_from($old_meta); | ||||
236 | |||||
237 | for my $role ( @{ $old_meta->get_roles } ) { | ||||
238 | $self->add_role($role); | ||||
239 | } | ||||
240 | } | ||||
241 | |||||
242 | sub add_attribute { | ||||
243 | my $self = shift; | ||||
244 | |||||
245 | if (blessed $_[0] && ! $_[0]->isa('Moose::Meta::Role::Attribute') ) { | ||||
246 | my $class = ref $_[0]; | ||||
247 | Moose->throw_error( "Cannot add a $class as an attribute to a role" ); | ||||
248 | } | ||||
249 | elsif (!blessed($_[0]) && defined($_[0]) && $_[0] =~ /^\+(.*)/) { | ||||
250 | Moose->throw_error( "has '+attr' is not supported in roles" ); | ||||
251 | } | ||||
252 | |||||
253 | return $self->SUPER::add_attribute(@_); | ||||
254 | } | ||||
255 | |||||
256 | sub _attach_attribute { | ||||
257 | my ( $self, $attribute ) = @_; | ||||
258 | |||||
259 | $attribute->attach_to_role($self); | ||||
260 | } | ||||
261 | |||||
262 | sub add_required_methods { | ||||
263 | my $self = shift; | ||||
264 | |||||
265 | for (@_) { | ||||
266 | my $method = $_; | ||||
267 | if (!blessed($method)) { | ||||
268 | $method = $self->required_method_metaclass->new( | ||||
269 | name => $method, | ||||
270 | ); | ||||
271 | } | ||||
272 | $self->get_required_methods_map->{$method->name} = $method; | ||||
273 | } | ||||
274 | } | ||||
275 | |||||
276 | sub add_conflicting_method { | ||||
277 | my $self = shift; | ||||
278 | |||||
279 | my $method; | ||||
280 | if (@_ == 1 && blessed($_[0])) { | ||||
281 | $method = shift; | ||||
282 | } | ||||
283 | else { | ||||
284 | $method = $self->conflicting_method_metaclass->new(@_); | ||||
285 | } | ||||
286 | |||||
287 | $self->add_required_methods($method); | ||||
288 | } | ||||
289 | |||||
290 | ## ------------------------------------------------------------------ | ||||
291 | ## method modifiers | ||||
292 | |||||
293 | # NOTE: | ||||
294 | # the before/around/after method modifiers are | ||||
295 | # stored by name, but there can be many methods | ||||
296 | # then associated with that name. So again we have | ||||
297 | # lots of similar functionality, so we can do some | ||||
298 | # meta-programmin' and save some time. | ||||
299 | # - SL | ||||
300 | |||||
301 | 1 | 900ns | foreach my $modifier_type (qw[ before around after ]) { | ||
302 | |||||
303 | 3 | 3µs | my $attr_reader = "get_${modifier_type}_method_modifiers_map"; | ||
304 | |||||
305 | # create the attribute ... | ||||
306 | $META->add_attribute("${modifier_type}_method_modifiers" => ( | ||||
307 | reader => $attr_reader, | ||||
308 | default => sub { {} }, | ||||
309 | 3 | 14µs | 6 | 1.16ms | Class::MOP::_definition_context(), # spent 1.14ms making 3 calls to Class::MOP::Mixin::HasAttributes::add_attribute, avg 381µs/call
# spent 20µs making 3 calls to Class::MOP::_definition_context, avg 7µs/call |
310 | )); | ||||
311 | |||||
312 | # and some helper methods ... | ||||
313 | $META->add_method("get_${modifier_type}_method_modifiers" => sub { | ||||
314 | my ($self, $method_name) = @_; | ||||
315 | #return () unless exists $self->$attr_reader->{$method_name}; | ||||
316 | my $mm = $self->$attr_reader->{$method_name}; | ||||
317 | $mm ? @$mm : (); | ||||
318 | 3 | 13µs | 3 | 168µs | }); # spent 168µs making 3 calls to Class::MOP::Mixin::HasMethods::add_method, avg 56µs/call |
319 | |||||
320 | $META->add_method("has_${modifier_type}_method_modifiers" => sub { | ||||
321 | my ($self, $method_name) = @_; | ||||
322 | # NOTE: | ||||
323 | # for now we assume that if it exists,.. | ||||
324 | # it has at least one modifier in it | ||||
325 | (exists $self->$attr_reader->{$method_name}) ? 1 : 0; | ||||
326 | 3 | 12µs | 3 | 154µs | }); # spent 154µs making 3 calls to Class::MOP::Mixin::HasMethods::add_method, avg 51µs/call |
327 | |||||
328 | $META->add_method("add_${modifier_type}_method_modifier" => sub { | ||||
329 | my ($self, $method_name, $method) = @_; | ||||
330 | |||||
331 | $self->$attr_reader->{$method_name} = [] | ||||
332 | unless exists $self->$attr_reader->{$method_name}; | ||||
333 | |||||
334 | my $modifiers = $self->$attr_reader->{$method_name}; | ||||
335 | |||||
336 | # NOTE: | ||||
337 | # check to see that we aren't adding the | ||||
338 | # same code twice. We err in favor of the | ||||
339 | # first on here, this may not be as expected | ||||
340 | foreach my $modifier (@{$modifiers}) { | ||||
341 | return if $modifier == $method; | ||||
342 | } | ||||
343 | |||||
344 | push @{$modifiers} => $method; | ||||
345 | 3 | 14µs | 3 | 153µs | }); # spent 153µs making 3 calls to Class::MOP::Mixin::HasMethods::add_method, avg 51µs/call |
346 | |||||
347 | } | ||||
348 | |||||
349 | ## ------------------------------------------------------------------ | ||||
350 | ## override method modifiers | ||||
351 | |||||
352 | $META->add_attribute('override_method_modifiers' => ( | ||||
353 | reader => 'get_override_method_modifiers_map', | ||||
354 | default => sub { {} }, | ||||
355 | 1 | 5µs | 2 | 394µs | Class::MOP::_definition_context(), # spent 388µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 7µs making 1 call to Class::MOP::_definition_context |
356 | )); | ||||
357 | |||||
358 | # NOTE: | ||||
359 | # these are a little different because there | ||||
360 | # can only be one per name, whereas the other | ||||
361 | # method modifiers can have multiples. | ||||
362 | # - SL | ||||
363 | |||||
364 | sub add_override_method_modifier { | ||||
365 | my ($self, $method_name, $method) = @_; | ||||
366 | (!$self->has_method($method_name)) | ||||
367 | || Moose->throw_error("Cannot add an override of method '$method_name' " . | ||||
368 | "because there is a local version of '$method_name'"); | ||||
369 | $self->get_override_method_modifiers_map->{$method_name} = $method; | ||||
370 | } | ||||
371 | |||||
372 | sub has_override_method_modifier { | ||||
373 | my ($self, $method_name) = @_; | ||||
374 | # NOTE: | ||||
375 | # for now we assume that if it exists,.. | ||||
376 | # it has at least one modifier in it | ||||
377 | (exists $self->get_override_method_modifiers_map->{$method_name}) ? 1 : 0; | ||||
378 | } | ||||
379 | |||||
380 | sub get_override_method_modifier { | ||||
381 | my ($self, $method_name) = @_; | ||||
382 | $self->get_override_method_modifiers_map->{$method_name}; | ||||
383 | } | ||||
384 | |||||
385 | ## general list accessor ... | ||||
386 | |||||
387 | sub get_method_modifier_list { | ||||
388 | my ($self, $modifier_type) = @_; | ||||
389 | my $accessor = "get_${modifier_type}_method_modifiers_map"; | ||||
390 | keys %{$self->$accessor}; | ||||
391 | } | ||||
392 | |||||
393 | sub _meta_method_class { 'Moose::Meta::Method::Meta' } | ||||
394 | |||||
395 | ## ------------------------------------------------------------------ | ||||
396 | ## subroles | ||||
397 | |||||
398 | $META->add_attribute('roles' => ( | ||||
399 | reader => 'get_roles', | ||||
400 | default => sub { [] }, | ||||
401 | 1 | 5µs | 2 | 370µs | Class::MOP::_definition_context(), # spent 363µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 6µs making 1 call to Class::MOP::_definition_context |
402 | )); | ||||
403 | |||||
404 | sub add_role { | ||||
405 | my ($self, $role) = @_; | ||||
406 | (blessed($role) && $role->isa('Moose::Meta::Role')) | ||||
407 | || Moose->throw_error("Roles must be instances of Moose::Meta::Role"); | ||||
408 | push @{$self->get_roles} => $role; | ||||
409 | $self->reset_package_cache_flag; | ||||
410 | } | ||||
411 | |||||
412 | sub calculate_all_roles { | ||||
413 | my $self = shift; | ||||
414 | my %seen; | ||||
415 | grep { | ||||
416 | !$seen{$_->name}++ | ||||
417 | } ($self, map { | ||||
418 | $_->calculate_all_roles | ||||
419 | } @{ $self->get_roles }); | ||||
420 | } | ||||
421 | |||||
422 | sub does_role { | ||||
423 | my ($self, $role) = @_; | ||||
424 | (defined $role) | ||||
425 | || Moose->throw_error("You must supply a role name to look for"); | ||||
426 | my $role_name = blessed $role ? $role->name : $role; | ||||
427 | # if we are it,.. then return true | ||||
428 | return 1 if $role_name eq $self->name; | ||||
429 | # otherwise.. check our children | ||||
430 | foreach my $role (@{$self->get_roles}) { | ||||
431 | return 1 if $role->does_role($role_name); | ||||
432 | } | ||||
433 | return 0; | ||||
434 | } | ||||
435 | |||||
436 | sub find_method_by_name { (shift)->get_method(@_) } | ||||
437 | |||||
438 | ## ------------------------------------------------------------------ | ||||
439 | ## role construction | ||||
440 | ## ------------------------------------------------------------------ | ||||
441 | |||||
442 | sub apply { | ||||
443 | my ($self, $other, %args) = @_; | ||||
444 | |||||
445 | (blessed($other)) | ||||
446 | || Moose->throw_error("You must pass in an blessed instance"); | ||||
447 | |||||
448 | my $application_class; | ||||
449 | if ($other->isa('Moose::Meta::Role')) { | ||||
450 | $application_class = $self->application_to_role_class; | ||||
451 | } | ||||
452 | elsif ($other->isa('Moose::Meta::Class')) { | ||||
453 | $application_class = $self->application_to_class_class; | ||||
454 | } | ||||
455 | else { | ||||
456 | $application_class = $self->application_to_instance_class; | ||||
457 | } | ||||
458 | |||||
459 | load_class($application_class); | ||||
460 | |||||
461 | if ( exists $args{'-excludes'} ) { | ||||
462 | # I wish we had coercion here :) | ||||
463 | $args{'-excludes'} = ( | ||||
464 | ref $args{'-excludes'} eq 'ARRAY' | ||||
465 | ? $args{'-excludes'} | ||||
466 | : [ $args{'-excludes'} ] | ||||
467 | ); | ||||
468 | } | ||||
469 | |||||
470 | return $application_class->new(%args)->apply($self, $other, \%args); | ||||
471 | } | ||||
472 | |||||
473 | sub composition_class_roles { } | ||||
474 | |||||
475 | sub combine { | ||||
476 | my ($class, @role_specs) = @_; | ||||
477 | |||||
478 | require Moose::Meta::Role::Composite; | ||||
479 | |||||
480 | my (@roles, %role_params); | ||||
481 | while (@role_specs) { | ||||
482 | my ($role, $params) = @{ splice @role_specs, 0, 1 }; | ||||
483 | my $requested_role | ||||
484 | = blessed $role | ||||
485 | ? $role | ||||
486 | : Class::MOP::class_of($role); | ||||
487 | |||||
488 | my $actual_role = $requested_role->_role_for_combination($params); | ||||
489 | push @roles => $actual_role; | ||||
490 | |||||
491 | next unless defined $params; | ||||
492 | $role_params{$actual_role->name} = $params; | ||||
493 | } | ||||
494 | |||||
495 | my $c = Moose::Meta::Role::Composite->new(roles => \@roles); | ||||
496 | return $c->apply_params(\%role_params); | ||||
497 | } | ||||
498 | |||||
499 | sub _role_for_combination { | ||||
500 | my ($self, $params) = @_; | ||||
501 | return $self; | ||||
502 | } | ||||
503 | |||||
504 | sub create { | ||||
505 | my $class = shift; | ||||
506 | my @args = @_; | ||||
507 | |||||
508 | unshift @args, 'package' if @args % 2 == 1; | ||||
509 | my %options = @args; | ||||
510 | |||||
511 | (ref $options{attributes} eq 'HASH') | ||||
512 | || confess "You must pass a HASH ref of attributes" | ||||
513 | if exists $options{attributes}; | ||||
514 | |||||
515 | (ref $options{methods} eq 'HASH') | ||||
516 | || confess "You must pass a HASH ref of methods" | ||||
517 | if exists $options{methods}; | ||||
518 | |||||
519 | (ref $options{roles} eq 'ARRAY') | ||||
520 | || confess "You must pass an ARRAY ref of roles" | ||||
521 | if exists $options{roles}; | ||||
522 | |||||
523 | my $package = delete $options{package}; | ||||
524 | my $roles = delete $options{roles}; | ||||
525 | my $attributes = delete $options{attributes}; | ||||
526 | my $methods = delete $options{methods}; | ||||
527 | my $meta_name = exists $options{meta_name} | ||||
528 | ? delete $options{meta_name} | ||||
529 | : 'meta'; | ||||
530 | |||||
531 | my $meta = $class->SUPER::create($package => %options); | ||||
532 | |||||
533 | $meta->_add_meta_method($meta_name) | ||||
534 | if defined $meta_name; | ||||
535 | |||||
536 | if (defined $attributes) { | ||||
537 | foreach my $attribute_name (keys %{$attributes}) { | ||||
538 | my $attr = $attributes->{$attribute_name}; | ||||
539 | $meta->add_attribute( | ||||
540 | $attribute_name => blessed $attr ? $attr : %{$attr} ); | ||||
541 | } | ||||
542 | } | ||||
543 | |||||
544 | if (defined $methods) { | ||||
545 | foreach my $method_name (keys %{$methods}) { | ||||
546 | $meta->add_method($method_name, $methods->{$method_name}); | ||||
547 | } | ||||
548 | } | ||||
549 | |||||
550 | if ($roles) { | ||||
551 | Moose::Util::apply_all_roles($meta, @$roles); | ||||
552 | } | ||||
553 | |||||
554 | return $meta; | ||||
555 | } | ||||
556 | |||||
557 | sub consumers { | ||||
558 | my $self = shift; | ||||
559 | my @consumers; | ||||
560 | for my $meta (Class::MOP::get_all_metaclass_instances) { | ||||
561 | next if $meta->name eq $self->name; | ||||
562 | next unless $meta->isa('Moose::Meta::Class') | ||||
563 | || $meta->isa('Moose::Meta::Role'); | ||||
564 | push @consumers, $meta->name | ||||
565 | if $meta->does_role($self->name); | ||||
566 | } | ||||
567 | return @consumers; | ||||
568 | } | ||||
569 | |||||
570 | # XXX: something more intelligent here? | ||||
571 | sub _anon_package_prefix { 'Moose::Meta::Role::__ANON__::SERIAL::' } | ||||
572 | |||||
573 | sub create_anon_role { shift->create_anon(@_) } | ||||
574 | sub is_anon_role { shift->is_anon(@_) } | ||||
575 | |||||
576 | sub _anon_cache_key { | ||||
577 | my $class = shift; | ||||
578 | my %options = @_; | ||||
579 | |||||
580 | # XXX fix this duplication (see MMC::_anon_cache_key | ||||
581 | my $roles = Data::OptList::mkopt(($options{roles} || []), { | ||||
582 | moniker => 'role', | ||||
583 | val_test => sub { ref($_[0]) eq 'HASH' }, | ||||
584 | }); | ||||
585 | |||||
586 | my @role_keys; | ||||
587 | for my $role_spec (@$roles) { | ||||
588 | my ($role, $params) = @$role_spec; | ||||
589 | $params = { %$params }; | ||||
590 | |||||
591 | my $key = blessed($role) ? $role->name : $role; | ||||
592 | |||||
593 | if ($params && %$params) { | ||||
594 | my $alias = delete $params->{'-alias'} | ||||
595 | || delete $params->{'alias'} | ||||
596 | || {}; | ||||
597 | my $excludes = delete $params->{'-excludes'} | ||||
598 | || delete $params->{'excludes'} | ||||
599 | || []; | ||||
600 | $excludes = [$excludes] unless ref($excludes) eq 'ARRAY'; | ||||
601 | |||||
602 | if (%$params) { | ||||
603 | warn "Roles with parameters cannot be cached. Consider " | ||||
604 | . "applying the parameters before calling " | ||||
605 | . "create_anon_class, or using 'weaken => 0' instead"; | ||||
606 | return; | ||||
607 | } | ||||
608 | |||||
609 | my $alias_key = join('%', | ||||
610 | map { $_ => $alias->{$_} } sort keys %$alias | ||||
611 | ); | ||||
612 | my $excludes_key = join('%', | ||||
613 | sort @$excludes | ||||
614 | ); | ||||
615 | $key .= '<' . join('+', 'a', $alias_key, 'e', $excludes_key) . '>'; | ||||
616 | } | ||||
617 | |||||
618 | push @role_keys, $key; | ||||
619 | } | ||||
620 | |||||
621 | # Makes something like Role|Role::1 | ||||
622 | return join('|', sort @role_keys); | ||||
623 | } | ||||
624 | |||||
625 | ##################################################################### | ||||
626 | ## NOTE: | ||||
627 | ## This is Moose::Meta::Role as defined by Moose (plus the use of | ||||
628 | ## MooseX::AttributeHelpers module). It is here as a reference to | ||||
629 | ## make it easier to see what is happening above with all the meta | ||||
630 | ## programming. - SL | ||||
631 | ##################################################################### | ||||
632 | # | ||||
633 | # has 'roles' => ( | ||||
634 | # metaclass => 'Array', | ||||
635 | # reader => 'get_roles', | ||||
636 | # isa => 'ArrayRef[Moose::Meta::Role]', | ||||
637 | # default => sub { [] }, | ||||
638 | # provides => { | ||||
639 | # 'push' => 'add_role', | ||||
640 | # } | ||||
641 | # ); | ||||
642 | # | ||||
643 | # has 'excluded_roles_map' => ( | ||||
644 | # metaclass => 'Hash', | ||||
645 | # reader => 'get_excluded_roles_map', | ||||
646 | # isa => 'HashRef[Str]', | ||||
647 | # provides => { | ||||
648 | # # Not exactly set, cause it sets multiple | ||||
649 | # 'set' => 'add_excluded_roles', | ||||
650 | # 'keys' => 'get_excluded_roles_list', | ||||
651 | # 'exists' => 'excludes_role', | ||||
652 | # } | ||||
653 | # ); | ||||
654 | # | ||||
655 | # has 'required_methods' => ( | ||||
656 | # metaclass => 'Hash', | ||||
657 | # reader => 'get_required_methods_map', | ||||
658 | # isa => 'HashRef[Moose::Meta::Role::Method::Required]', | ||||
659 | # provides => { | ||||
660 | # # not exactly set, or delete since it works for multiple | ||||
661 | # 'set' => 'add_required_methods', | ||||
662 | # 'delete' => 'remove_required_methods', | ||||
663 | # 'keys' => 'get_required_method_list', | ||||
664 | # 'exists' => 'requires_method', | ||||
665 | # } | ||||
666 | # ); | ||||
667 | # | ||||
668 | # # the before, around and after modifiers are | ||||
669 | # # HASH keyed by method-name, with ARRAY of | ||||
670 | # # CODE refs to apply in that order | ||||
671 | # | ||||
672 | # has 'before_method_modifiers' => ( | ||||
673 | # metaclass => 'Hash', | ||||
674 | # reader => 'get_before_method_modifiers_map', | ||||
675 | # isa => 'HashRef[ArrayRef[CodeRef]]', | ||||
676 | # provides => { | ||||
677 | # 'keys' => 'get_before_method_modifiers', | ||||
678 | # 'exists' => 'has_before_method_modifiers', | ||||
679 | # # This actually makes sure there is an | ||||
680 | # # ARRAY at the given key, and pushed onto | ||||
681 | # # it. It also checks for duplicates as well | ||||
682 | # # 'add' => 'add_before_method_modifier' | ||||
683 | # } | ||||
684 | # ); | ||||
685 | # | ||||
686 | # has 'after_method_modifiers' => ( | ||||
687 | # metaclass => 'Hash', | ||||
688 | # reader =>'get_after_method_modifiers_map', | ||||
689 | # isa => 'HashRef[ArrayRef[CodeRef]]', | ||||
690 | # provides => { | ||||
691 | # 'keys' => 'get_after_method_modifiers', | ||||
692 | # 'exists' => 'has_after_method_modifiers', | ||||
693 | # # This actually makes sure there is an | ||||
694 | # # ARRAY at the given key, and pushed onto | ||||
695 | # # it. It also checks for duplicates as well | ||||
696 | # # 'add' => 'add_after_method_modifier' | ||||
697 | # } | ||||
698 | # ); | ||||
699 | # | ||||
700 | # has 'around_method_modifiers' => ( | ||||
701 | # metaclass => 'Hash', | ||||
702 | # reader =>'get_around_method_modifiers_map', | ||||
703 | # isa => 'HashRef[ArrayRef[CodeRef]]', | ||||
704 | # provides => { | ||||
705 | # 'keys' => 'get_around_method_modifiers', | ||||
706 | # 'exists' => 'has_around_method_modifiers', | ||||
707 | # # This actually makes sure there is an | ||||
708 | # # ARRAY at the given key, and pushed onto | ||||
709 | # # it. It also checks for duplicates as well | ||||
710 | # # 'add' => 'add_around_method_modifier' | ||||
711 | # } | ||||
712 | # ); | ||||
713 | # | ||||
714 | # # override is similar to the other modifiers | ||||
715 | # # except that it is not an ARRAY of code refs | ||||
716 | # # but instead just a single name->code mapping | ||||
717 | # | ||||
718 | # has 'override_method_modifiers' => ( | ||||
719 | # metaclass => 'Hash', | ||||
720 | # reader =>'get_override_method_modifiers_map', | ||||
721 | # isa => 'HashRef[CodeRef]', | ||||
722 | # provides => { | ||||
723 | # 'keys' => 'get_override_method_modifier', | ||||
724 | # 'exists' => 'has_override_method_modifier', | ||||
725 | # 'add' => 'add_override_method_modifier', # checks for local method .. | ||||
726 | # } | ||||
727 | # ); | ||||
728 | # | ||||
729 | ##################################################################### | ||||
730 | |||||
731 | |||||
732 | 1 | 41µs | 1; | ||
733 | |||||
734 | # ABSTRACT: The Moose Role metaclass | ||||
735 | |||||
736 | __END__ |