File | /usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Class/MOP/Method/Constructor.pm |
Statements Executed | 4171 |
Statement Execution Time | 7.83ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
267 | 1 | 1 | 4.47ms | 8.22ms | _generate_slot_initializer | Class::MOP::Method::Constructor::
31 | 1 | 1 | 1.53ms | 22.6ms | _generate_constructor_method_inline | Class::MOP::Method::Constructor::
381 | 4 | 2 | 638µs | 4.51ms | _meta_instance | Class::MOP::Method::Constructor::
31 | 1 | 1 | 506µs | 23.9ms | new | Class::MOP::Method::Constructor::
31 | 1 | 1 | 476µs | 476µs | _new | Class::MOP::Method::Constructor::
31 | 1 | 1 | 221µs | 22.9ms | _initialize_body | Class::MOP::Method::Constructor::
42 | 6 | 2 | 221µs | 1.95ms | _attributes | Class::MOP::Method::Constructor::
124 | 4 | 2 | 114µs | 114µs | associated_metaclass | Class::MOP::Method::Constructor::
32 | 2 | 2 | 43µs | 43µs | options | Class::MOP::Method::Constructor::
1 | 1 | 1 | 21µs | 25µs | BEGIN@4 | Class::MOP::Method::Constructor::
1 | 1 | 1 | 11µs | 59µs | BEGIN@7 | Class::MOP::Method::Constructor::
1 | 1 | 1 | 10µs | 24µs | BEGIN@5 | Class::MOP::Method::Constructor::
1 | 1 | 1 | 9µs | 54µs | BEGIN@8 | Class::MOP::Method::Constructor::
1 | 1 | 1 | 9µs | 1.06ms | BEGIN@14 | Class::MOP::Method::Constructor::
0 | 0 | 0 | 0s | 0s | __ANON__[:97] | Class::MOP::Method::Constructor::
0 | 0 | 0 | 0s | 0s | _generate_constructor_method | Class::MOP::Method::Constructor::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | |||||
2 | package Class::MOP::Method::Constructor; | ||||
3 | |||||
4 | 3 | 30µs | 2 | 29µs | # spent 25µs (21+4) within Class::MOP::Method::Constructor::BEGIN@4 which was called
# once (21µs+4µs) by Class::MOP::Class::BEGIN@10 at line 4 # spent 25µs making 1 call to Class::MOP::Method::Constructor::BEGIN@4
# spent 4µs making 1 call to strict::import |
5 | 3 | 41µs | 2 | 38µs | # spent 24µs (10+14) within Class::MOP::Method::Constructor::BEGIN@5 which was called
# once (10µs+14µs) by Class::MOP::Class::BEGIN@10 at line 5 # spent 24µs making 1 call to Class::MOP::Method::Constructor::BEGIN@5
# spent 14µs making 1 call to warnings::import |
6 | |||||
7 | 3 | 36µs | 2 | 107µs | # spent 59µs (11+48) within Class::MOP::Method::Constructor::BEGIN@7 which was called
# once (11µs+48µs) by Class::MOP::Class::BEGIN@10 at line 7 # spent 59µs making 1 call to Class::MOP::Method::Constructor::BEGIN@7
# spent 48µs making 1 call to Exporter::import |
8 | 3 | 61µs | 2 | 98µs | # spent 54µs (9+44) within Class::MOP::Method::Constructor::BEGIN@8 which was called
# once (9µs+44µs) by Class::MOP::Class::BEGIN@10 at line 8 # spent 54µs making 1 call to Class::MOP::Method::Constructor::BEGIN@8
# spent 44µs making 1 call to Exporter::import |
9 | |||||
10 | 1 | 900ns | our $VERSION = '0.98'; | ||
11 | 1 | 19µs | $VERSION = eval $VERSION; | ||
12 | 1 | 500ns | our $AUTHORITY = 'cpan:STEVAN'; | ||
13 | |||||
14 | 3 | 919µs | 2 | 2.12ms | # spent 1.06ms (9µs+1.06) within Class::MOP::Method::Constructor::BEGIN@14 which was called
# once (9µs+1.06ms) by Class::MOP::Class::BEGIN@10 at line 14 # spent 1.06ms making 1 call to Class::MOP::Method::Constructor::BEGIN@14
# spent 1.05ms making 1 call to base::import |
15 | |||||
16 | # spent 23.9ms (506µs+23.4) within Class::MOP::Method::Constructor::new which was called 31 times, avg 772µs/call:
# 31 times (506µs+23.4ms) by Class::MOP::Class::_inline_constructor at line 1055 of Class/MOP/Class.pm, avg 772µs/call | ||||
17 | 31 | 11µs | my $class = shift; | ||
18 | 31 | 65µs | my %options = @_; | ||
19 | |||||
20 | 31 | 179µs | 62 | 49µs | (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class')) # spent 27µs making 31 calls to UNIVERSAL::isa, avg 868ns/call
# spent 22µs making 31 calls to Scalar::Util::blessed, avg 703ns/call |
21 | || confess "You must pass a metaclass instance if you want to inline" | ||||
22 | if $options{is_inline}; | ||||
23 | |||||
24 | 31 | 11µs | ($options{package_name} && $options{name}) | ||
25 | || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT"; | ||||
26 | |||||
27 | 31 | 44µs | 31 | 476µs | my $self = $class->_new(\%options); # spent 476µs making 31 calls to Class::MOP::Method::Constructor::_new, avg 15µs/call |
28 | |||||
29 | # we don't want this creating | ||||
30 | # a cycle in the code, if not | ||||
31 | # needed | ||||
32 | 31 | 98µs | 31 | 36µs | weaken($self->{'associated_metaclass'}); # spent 36µs making 31 calls to Scalar::Util::weaken, avg 1µs/call |
33 | |||||
34 | 31 | 36µs | 31 | 22.9ms | $self->_initialize_body; # spent 22.9ms making 31 calls to Class::MOP::Method::Constructor::_initialize_body, avg 738µs/call |
35 | |||||
36 | 31 | 83µs | return $self; | ||
37 | } | ||||
38 | |||||
39 | # spent 476µs within Class::MOP::Method::Constructor::_new which was called 31 times, avg 15µs/call:
# 31 times (476µs+0s) by Class::MOP::Method::Constructor::new at line 27, avg 15µs/call | ||||
40 | 31 | 7µs | my $class = shift; | ||
41 | |||||
42 | 31 | 10µs | return Class::MOP::Class->initialize($class)->new_object(@_) | ||
43 | if $class ne __PACKAGE__; | ||||
44 | |||||
45 | 31 | 16µs | my $params = @_ == 1 ? $_[0] : {@_}; | ||
46 | |||||
47 | 31 | 467µs | return bless { | ||
48 | # inherited from Class::MOP::Method | ||||
49 | body => $params->{body}, | ||||
50 | # associated_metaclass => $params->{associated_metaclass}, # overriden | ||||
51 | package_name => $params->{package_name}, | ||||
52 | name => $params->{name}, | ||||
53 | original_method => $params->{original_method}, | ||||
54 | |||||
55 | # inherited from Class::MOP::Generated | ||||
56 | is_inline => $params->{is_inline} || 0, | ||||
57 | definition_context => $params->{definition_context}, | ||||
58 | |||||
59 | # inherited from Class::MOP::Inlined | ||||
60 | _expected_method_class => $params->{_expected_method_class}, | ||||
61 | |||||
62 | # defined in this subclass | ||||
63 | options => $params->{options} || {}, | ||||
64 | associated_metaclass => $params->{metaclass}, | ||||
65 | }, $class; | ||||
66 | } | ||||
67 | |||||
68 | ## accessors | ||||
69 | |||||
70 | 32 | 74µs | # spent 43µs within Class::MOP::Method::Constructor::options which was called 32 times, avg 1µs/call:
# 31 times (40µs+0s) by Class::MOP::Method::Constructor::_generate_constructor_method_inline at line 119, avg 1µs/call
# once (2µs+0s) by Moose::Meta::Method::Constructor::_initialize_body at line 75 of Moose/Meta/Method/Constructor.pm | ||
71 | 124 | 236µs | # spent 114µs within Class::MOP::Method::Constructor::associated_metaclass which was called 124 times, avg 918ns/call:
# 31 times (34µs+0s) by Class::MOP::Method::Inlined::can_be_inlined at line 34 of Class/MOP/Method/Inlined.pm, avg 1µs/call
# 31 times (30µs+0s) by Class::MOP::Method::Constructor::_generate_constructor_method_inline at line 109, avg 965ns/call
# 31 times (27µs+0s) by Class::MOP::Method::Constructor::_attributes at line 82, avg 861ns/call
# 31 times (23µs+0s) by Class::MOP::Method::Constructor::_meta_instance at line 77, avg 739ns/call | ||
72 | |||||
73 | ## cached values ... | ||||
74 | |||||
75 | # spent 4.51ms (638µs+3.87) within Class::MOP::Method::Constructor::_meta_instance which was called 381 times, avg 12µs/call:
# 343 times (444µs+0s) by Class::MOP::Method::Constructor::_generate_slot_initializer at line 159, avg 1µs/call
# 31 times (174µs+3.87ms) by Class::MOP::Method::Constructor::_generate_constructor_method_inline at line 113, avg 130µs/call
# 6 times (8µs+0s) by Moose::Meta::Method::Constructor::_generate_slot_assignment at line 272 of Moose/Meta/Method/Constructor.pm, avg 1µs/call
# once (12µs+2µs) by Moose::Meta::Method::Constructor::_generate_instance at line 121 of Moose/Meta/Method/Constructor.pm | ||||
76 | 381 | 58µs | my $self = shift; | ||
77 | 381 | 774µs | 64 | 3.87ms | $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance; # spent 3.84ms making 32 calls to Class::MOP::Class::get_meta_instance, avg 120µs/call
# spent 23µs making 31 calls to Class::MOP::Method::Constructor::associated_metaclass, avg 739ns/call
# spent 1µs making 1 call to Class::MOP::Method::associated_metaclass |
78 | } | ||||
79 | |||||
80 | # spent 1.95ms (221µs+1.73) within Class::MOP::Method::Constructor::_attributes which was called 42 times, avg 47µs/call:
# 31 times (200µs+1.70ms) by Class::MOP::Method::Constructor::_generate_constructor_method_inline at line 116, avg 61µs/call
# 4 times (5µs+0s) by Moose::Meta::Method::Constructor::_generate_slot_initializer at line 194 of Moose/Meta/Method/Constructor.pm, avg 1µs/call
# 4 times (5µs+0s) by Moose::Meta::Method::Constructor::_generate_triggers at line 163 of Moose/Meta/Method/Constructor.pm, avg 1µs/call
# once (9µs+33µs) by Moose::Meta::Method::Constructor::_generate_slot_initializers at line 129 of Moose/Meta/Method/Constructor.pm
# once (1µs+0s) by Moose::Meta::Method::Constructor::_generate_triggers at line 162 of Moose/Meta/Method/Constructor.pm
# once (1µs+0s) by Moose::Meta::Method::Constructor::_initialize_body at line 86 of Moose/Meta/Method/Constructor.pm | ||||
81 | 42 | 9µs | my $self = shift; | ||
82 | 42 | 170µs | 64 | 1.73ms | $self->{'attributes'} ||= [ $self->associated_metaclass->get_all_attributes ] # spent 1.71ms making 32 calls to Class::MOP::Class::get_all_attributes, avg 53µs/call
# spent 27µs making 31 calls to Class::MOP::Method::Constructor::associated_metaclass, avg 861ns/call
# spent 800ns making 1 call to Class::MOP::Method::associated_metaclass |
83 | } | ||||
84 | |||||
85 | ## method | ||||
86 | |||||
87 | # spent 22.9ms (221µs+22.6) within Class::MOP::Method::Constructor::_initialize_body which was called 31 times, avg 738µs/call:
# 31 times (221µs+22.6ms) by Class::MOP::Method::Constructor::new at line 34, avg 738µs/call | ||||
88 | 31 | 7µs | my $self = shift; | ||
89 | 31 | 8µs | my $method_name = '_generate_constructor_method'; | ||
90 | |||||
91 | 31 | 36µs | 31 | 38µs | $method_name .= '_inline' if $self->is_inline; # spent 38µs making 31 calls to Class::MOP::Method::Generated::is_inline, avg 1µs/call |
92 | |||||
93 | 31 | 119µs | 31 | 22.6ms | $self->{'body'} = $self->$method_name; # spent 22.6ms making 31 calls to Class::MOP::Method::Constructor::_generate_constructor_method_inline, avg 729µs/call |
94 | } | ||||
95 | |||||
96 | sub _generate_constructor_method { | ||||
97 | return sub { Class::MOP::Class->initialize(shift)->new_object(@_) } | ||||
98 | } | ||||
99 | |||||
100 | # spent 22.6ms (1.53+21.1) within Class::MOP::Method::Constructor::_generate_constructor_method_inline which was called 31 times, avg 729µs/call:
# 31 times (1.53ms+21.1ms) by Class::MOP::Method::Constructor::_initialize_body at line 93, avg 729µs/call | ||||
101 | 31 | 8µs | my $self = shift; | ||
102 | |||||
103 | 31 | 7µs | my $close_over = {}; | ||
104 | |||||
105 | 31 | 4µs | my $source = 'sub {'; | ||
106 | 31 | 3µs | $source .= "\n" . 'my $class = shift;'; | ||
107 | |||||
108 | 31 | 4µs | $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)'; | ||
109 | 31 | 102µs | 62 | 46µs | $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';'; # spent 30µs making 31 calls to Class::MOP::Method::Constructor::associated_metaclass, avg 965ns/call
# spent 16µs making 31 calls to Class::MOP::Package::name, avg 529ns/call |
110 | |||||
111 | 31 | 5µs | $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};'; | ||
112 | |||||
113 | 31 | 81µs | 62 | 4.10ms | $source .= "\n" . 'my $instance = ' . $self->_meta_instance->inline_create_instance('$class'); # spent 4.04ms making 31 calls to Class::MOP::Method::Constructor::_meta_instance, avg 130µs/call
# spent 63µs making 31 calls to Class::MOP::Instance::inline_create_instance, avg 2µs/call |
114 | 267 | 252µs | 267 | 8.22ms | $source .= ";\n" . (join ";\n" => map { # spent 8.22ms making 267 calls to Class::MOP::Method::Constructor::_generate_slot_initializer, avg 31µs/call |
115 | $self->_generate_slot_initializer($_, $close_over) | ||||
116 | 31 | 313µs | 31 | 1.90ms | } @{ $self->_attributes }); # spent 1.90ms making 31 calls to Class::MOP::Method::Constructor::_attributes, avg 61µs/call |
117 | 31 | 6µs | $source .= ";\n" . 'return $instance'; | ||
118 | 31 | 4µs | $source .= ";\n" . '}'; | ||
119 | 31 | 44µs | 31 | 40µs | warn $source if $self->options->{debug}; # spent 40µs making 31 calls to Class::MOP::Method::Constructor::options, avg 1µs/call |
120 | |||||
121 | 31 | 57µs | 31 | 6.77ms | my ( $code, $e ) = $self->_eval_closure( # spent 6.77ms making 31 calls to Class::MOP::Method::Generated::_eval_closure, avg 218µs/call |
122 | $close_over, | ||||
123 | $source | ||||
124 | ); | ||||
125 | 31 | 5µs | confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e" if $e; | ||
126 | |||||
127 | 31 | 73µs | return $code; | ||
128 | } | ||||
129 | |||||
130 | # spent 8.22ms (4.47+3.75) within Class::MOP::Method::Constructor::_generate_slot_initializer which was called 267 times, avg 31µs/call:
# 267 times (4.47ms+3.75ms) by Class::MOP::Method::Constructor::_generate_constructor_method_inline at line 114, avg 31µs/call | ||||
131 | 267 | 57µs | my $self = shift; | ||
132 | 267 | 31µs | my $attr = shift; | ||
133 | 267 | 32µs | my $close = shift; | ||
134 | |||||
135 | 267 | 8µs | my $default; | ||
136 | 267 | 434µs | 447 | 547µs | if ($attr->has_default) { # spent 345µs making 267 calls to Class::MOP::Mixin::AttributeCore::has_default, avg 1µs/call
# spent 202µs making 180 calls to Class::MOP::Mixin::AttributeCore::has_builder, avg 1µs/call |
137 | # NOTE: | ||||
138 | # default values can either be CODE refs | ||||
139 | # in which case we need to call them. Or | ||||
140 | # they can be scalars (strings/numbers) | ||||
141 | # in which case we can just deal with them | ||||
142 | # in the code we eval. | ||||
143 | 87 | 89µs | 87 | 205µs | if ($attr->is_default_a_coderef) { # spent 205µs making 87 calls to Class::MOP::Mixin::AttributeCore::is_default_a_coderef, avg 2µs/call |
144 | 58 | 45µs | my $idx = @{$close->{'@defaults'}||=[]}; | ||
145 | 58 | 81µs | 58 | 101µs | push(@{$close->{'@defaults'}}, $attr->default); # spent 101µs making 58 calls to Class::MOP::Mixin::AttributeCore::default, avg 2µs/call |
146 | 58 | 41µs | $default = '$defaults[' . $idx . ']->($instance)'; | ||
147 | } | ||||
148 | else { | ||||
149 | 29 | 34µs | 29 | 61µs | $default = $attr->default; # spent 61µs making 29 calls to Class::MOP::Mixin::AttributeCore::default, avg 2µs/call |
150 | # make sure to quote strings ... | ||||
151 | 29 | 103µs | 29 | 34µs | unless (looks_like_number($default)) { # spent 34µs making 29 calls to Scalar::Util::looks_like_number, avg 1µs/call |
152 | $default = "'$default'"; | ||||
153 | } | ||||
154 | } | ||||
155 | } elsif( $attr->has_builder ) { | ||||
156 | $default = '$instance->'.$attr->builder; | ||||
157 | } | ||||
158 | |||||
159 | 267 | 2.27ms | 1296 | 2.80ms | if ( defined(my $init_arg = $attr->init_arg) ) { # spent 1.87ms making 343 calls to Class::MOP::Instance::inline_set_slot_value, avg 5µs/call
# spent 444µs making 343 calls to Class::MOP::Method::Constructor::_meta_instance, avg 1µs/call
# spent 318µs making 267 calls to Class::MOP::Mixin::AttributeCore::init_arg, avg 1µs/call
# spent 169µs making 343 calls to Class::MOP::Mixin::AttributeCore::name, avg 491ns/call |
160 | return ( | ||||
161 | 'if(exists $params->{\'' . $init_arg . '\'}){' . "\n" . | ||||
162 | $self->_meta_instance->inline_set_slot_value( | ||||
163 | '$instance', | ||||
164 | $attr->name, | ||||
165 | '$params->{\'' . $init_arg . '\'}' ) . "\n" . | ||||
166 | '} ' . (!defined $default ? '' : 'else {' . "\n" . | ||||
167 | $self->_meta_instance->inline_set_slot_value( | ||||
168 | '$instance', | ||||
169 | $attr->name, | ||||
170 | $default ) . "\n" . | ||||
171 | '}') | ||||
172 | ); | ||||
173 | } elsif ( defined $default ) { | ||||
174 | return ( | ||||
175 | $self->_meta_instance->inline_set_slot_value( | ||||
176 | '$instance', | ||||
177 | $attr->name, | ||||
178 | $default ) . "\n" | ||||
179 | ); | ||||
180 | 1 | 2µs | } else { return '' } | ||
181 | } | ||||
182 | |||||
183 | 1 | 5µs | 1; | ||
184 | |||||
185 | __END__ | ||||
186 | |||||
187 | =pod | ||||
188 | |||||
189 | =head1 NAME | ||||
190 | |||||
191 | Class::MOP::Method::Constructor - Method Meta Object for constructors | ||||
192 | |||||
193 | =head1 SYNOPSIS | ||||
194 | |||||
195 | use Class::MOP::Method::Constructor; | ||||
196 | |||||
197 | my $constructor = Class::MOP::Method::Constructor->new( | ||||
198 | metaclass => $metaclass, | ||||
199 | options => { | ||||
200 | debug => 1, # this is all for now | ||||
201 | }, | ||||
202 | ); | ||||
203 | |||||
204 | # calling the constructor ... | ||||
205 | $constructor->body->execute($metaclass->name, %params); | ||||
206 | |||||
207 | =head1 DESCRIPTION | ||||
208 | |||||
209 | This is a subclass of C<Class::MOP::Method> which generates | ||||
210 | constructor methods. | ||||
211 | |||||
212 | =head1 METHODS | ||||
213 | |||||
214 | =over 4 | ||||
215 | |||||
216 | =item B<< Class::MOP::Method::Constructor->new(%options) >> | ||||
217 | |||||
218 | This creates a new constructor object. It accepts a hash reference of | ||||
219 | options. | ||||
220 | |||||
221 | =over 8 | ||||
222 | |||||
223 | =item * metaclass | ||||
224 | |||||
225 | This should be a L<Class::MOP::Class> object. It is required. | ||||
226 | |||||
227 | =item * name | ||||
228 | |||||
229 | The method name (without a package name). This is required. | ||||
230 | |||||
231 | =item * package_name | ||||
232 | |||||
233 | The package name for the method. This is required. | ||||
234 | |||||
235 | =item * is_inline | ||||
236 | |||||
237 | This indicates whether or not the constructor should be inlined. This | ||||
238 | defaults to false. | ||||
239 | |||||
240 | =back | ||||
241 | |||||
242 | =item B<< $metamethod->is_inline >> | ||||
243 | |||||
244 | Returns a boolean indicating whether or not the constructor is | ||||
245 | inlined. | ||||
246 | |||||
247 | =item B<< $metamethod->associated_metaclass >> | ||||
248 | |||||
249 | This returns the L<Class::MOP::Class> object for the method. | ||||
250 | |||||
251 | =back | ||||
252 | |||||
253 | =head1 AUTHORS | ||||
254 | |||||
255 | Stevan Little E<lt>stevan@iinteractive.comE<gt> | ||||
256 | |||||
257 | =head1 COPYRIGHT AND LICENSE | ||||
258 | |||||
259 | Copyright 2006-2010 by Infinity Interactive, Inc. | ||||
260 | |||||
261 | L<http://www.iinteractive.com> | ||||
262 | |||||
263 | This library is free software; you can redistribute it and/or modify | ||||
264 | it under the same terms as Perl itself. | ||||
265 | |||||
266 | =cut | ||||
267 |