← Index
NYTProf Performance Profile   « block view • line view • sub view »
For conv.pl
  Run on Sun Nov 14 22:50:31 2010
Reported on Sun Nov 14 22:51:24 2010

Filename/usr/share/perl5/Class/Accessor.pm
StatementsExecuted 116 statements in 2.00ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
111268µs368µsClass::Accessor::::_mk_accessorsClass::Accessor::_mk_accessors
61144µs44µsClass::Accessor::::make_accessorClass::Accessor::make_accessor
61131µs31µsClass::Accessor::::accessor_name_forClass::Accessor::accessor_name_for
61125µs25µsClass::Accessor::::mutator_name_forClass::Accessor::mutator_name_for
11123µs29µsClass::Accessor::::BEGIN@3Class::Accessor::BEGIN@3
11116µs384µsClass::Accessor::::mk_accessorsClass::Accessor::mk_accessors
11113µs47µsClass::Accessor::::BEGIN@150Class::Accessor::BEGIN@150
11110µs10µsClass::Accessor::::BEGIN@460Class::Accessor::BEGIN@460
0000s0sClass::Accessor::::__ANON__[:395]Class::Accessor::__ANON__[:395]
0000s0sClass::Accessor::::__ANON__[:422]Class::Accessor::__ANON__[:422]
0000s0sClass::Accessor::::__ANON__[:449]Class::Accessor::__ANON__[:449]
0000s0sClass::Accessor::::_carpClass::Accessor::_carp
0000s0sClass::Accessor::::_croakClass::Accessor::_croak
0000s0sClass::Accessor::::best_practice_accessor_name_forClass::Accessor::best_practice_accessor_name_for
0000s0sClass::Accessor::::best_practice_mutator_name_forClass::Accessor::best_practice_mutator_name_for
0000s0sClass::Accessor::::follow_best_practiceClass::Accessor::follow_best_practice
0000s0sClass::Accessor::::getClass::Accessor::get
0000s0sClass::Accessor::::make_ro_accessorClass::Accessor::make_ro_accessor
0000s0sClass::Accessor::::make_wo_accessorClass::Accessor::make_wo_accessor
0000s0sClass::Accessor::::mk_ro_accessorsClass::Accessor::mk_ro_accessors
0000s0sClass::Accessor::::mk_wo_accessorsClass::Accessor::mk_wo_accessors
0000s0sClass::Accessor::::newClass::Accessor::new
0000s0sClass::Accessor::::setClass::Accessor::set
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Class::Accessor;
2125µsrequire 5.00502;
33194µs235µs
# spent 29µs (23+6) within Class::Accessor::BEGIN@3 which was called: # once (23µs+6µs) by base::import at line 3
use strict;
# spent 29µs making 1 call to Class::Accessor::BEGIN@3 # spent 6µs making 1 call to strict::import
412µs$Class::Accessor::VERSION = '0.31';
5
6=head1 NAME
7
- -
115sub new {
116 my($proto, $fields) = @_;
117 my($class) = ref $proto || $proto;
118
119 $fields = {} unless defined $fields;
120
121 # make a copy of $fields.
122 bless {%$fields}, $class;
123}
124
125=head2 mk_accessors
126
- -
142
# spent 384µs (16+368) within Class::Accessor::mk_accessors which was called: # once (16µs+368µs) by MARC::Charset::Table::BEGIN@38 at line 10 of MARC/Charset/Code.pm
sub mk_accessors {
14313µs my($self, @fields) = @_;
144
145111µs1368µs $self->_mk_accessors('rw', @fields);
# spent 368µs making 1 call to Class::Accessor::_mk_accessors
146}
147
148
149{
15041.23ms281µs
# spent 47µs (13+34) within Class::Accessor::BEGIN@150 which was called: # once (13µs+34µs) by base::import at line 150
no strict 'refs';
# spent 47µs making 1 call to Class::Accessor::BEGIN@150 # spent 34µs making 1 call to strict::unimport
151
152
# spent 368µs (268+100) within Class::Accessor::_mk_accessors which was called: # once (268µs+100µs) by Class::Accessor::mk_accessors at line 145
sub _mk_accessors {
15314µs my($self, $access, @fields) = @_;
15411µs my $class = ref $self || $self;
15512µs my $ra = $access eq 'rw' || $access eq 'ro';
15611µs my $wa = $access eq 'rw' || $access eq 'wo';
157
15817µs foreach my $field (@fields) {
159631µs631µs my $accessor_name = $self->accessor_name_for($field);
# spent 31µs making 6 calls to Class::Accessor::accessor_name_for, avg 5µs/call
160629µs625µs my $mutator_name = $self->mutator_name_for($field);
# spent 25µs making 6 calls to Class::Accessor::mutator_name_for, avg 4µs/call
16166µs if( $accessor_name eq 'DESTROY' or $mutator_name eq 'DESTROY' ) {
162 $self->_carp("Having a data accessor named DESTROY in '$class' is unwise.");
163 }
164620µs if ($accessor_name eq $mutator_name) {
16565µs my $accessor;
166627µs644µs if ($ra && $wa) {
# spent 44µs making 6 calls to Class::Accessor::make_accessor, avg 7µs/call
167 $accessor = $self->make_accessor($field);
168 } elsif ($ra) {
169 $accessor = $self->make_ro_accessor($field);
170 } else {
171 $accessor = $self->make_wo_accessor($field);
172 }
173633µs unless (defined &{"${class}::$accessor_name"}) {
174 *{"${class}::$accessor_name"} = $accessor;
175 }
176616µs if ($accessor_name eq $field) {
177 # the old behaviour
178610µs my $alias = "_${field}_accessor";
179640µs *{"${class}::$alias"} = $accessor unless defined &{"${class}::$alias"};
180 }
181 } else {
182 if ($ra and not defined &{"${class}::$accessor_name"}) {
183 *{"${class}::$accessor_name"} = $self->make_ro_accessor($field);
184 }
185 if ($wa and not defined &{"${class}::$mutator_name"}) {
186 *{"${class}::$mutator_name"} = $self->make_wo_accessor($field);
187 }
188 }
189 }
190 }
191
192 sub follow_best_practice {
193 my($self) = @_;
194 my $class = ref $self || $self;
195 *{"${class}::accessor_name_for"} = \&best_practice_accessor_name_for;
196 *{"${class}::mutator_name_for"} = \&best_practice_mutator_name_for;
197 }
198
199}
200
201=head2 mk_ro_accessors
202
- -
221sub mk_ro_accessors {
222 my($self, @fields) = @_;
223
224 $self->_mk_accessors('ro', @fields);
225}
226
227=head2 mk_wo_accessors
228
- -
249sub mk_wo_accessors {
250 my($self, @fields) = @_;
251
252 $self->_mk_accessors('wo', @fields);
253}
254
255=head1 DETAILS
256
- -
292sub best_practice_accessor_name_for {
293 my ($class, $field) = @_;
294 return "get_$field";
295}
296
297sub best_practice_mutator_name_for {
298 my ($class, $field) = @_;
299 return "set_$field";
300}
301
302
# spent 31µs within Class::Accessor::accessor_name_for which was called 6 times, avg 5µs/call: # 6 times (31µs+0s) by Class::Accessor::_mk_accessors at line 159, avg 5µs/call
sub accessor_name_for {
303611µs my ($class, $field) = @_;
304632µs return $field;
305}
306
307
# spent 25µs within Class::Accessor::mutator_name_for which was called 6 times, avg 4µs/call: # 6 times (25µs+0s) by Class::Accessor::_mk_accessors at line 160, avg 4µs/call
sub mutator_name_for {
30868µs my ($class, $field) = @_;
309635µs return $field;
310}
311
312=head2 Modifying the behavior of the accessor
313
- -
331sub set {
332 my($self, $key) = splice(@_, 0, 2);
333
334 if(@_ == 1) {
335 $self->{$key} = $_[0];
336 }
337 elsif(@_ > 1) {
338 $self->{$key} = [@_];
339 }
340 else {
341 $self->_croak("Wrong number of arguments received");
342 }
343}
344
345=head2 get
346
- -
356sub get {
357 my $self = shift;
358
359 if(@_ == 1) {
360 return $self->{$_[0]};
361 }
362 elsif( @_ > 1 ) {
363 return @{$self}{@_};
364 }
365 else {
366 $self->_croak("Wrong number of arguments received");
367 }
368}
369
370=head2 make_accessor
371
- -
382
# spent 44µs within Class::Accessor::make_accessor which was called 6 times, avg 7µs/call: # 6 times (44µs+0s) by Class::Accessor::_mk_accessors at line 166, avg 7µs/call
sub make_accessor {
383610µs my ($class, $field) = @_;
384
385 # Build a closure around $field.
386 return sub {
387 my $self = shift;
388
389 if(@_) {
390 return $self->set($field, @_);
391 }
392 else {
393 return $self->get($field);
394 }
395643µs };
396}
397
398=head2 make_ro_accessor
399
- -
409sub make_ro_accessor {
410 my($class, $field) = @_;
411
412 return sub {
413 my $self = shift;
414
415 if (@_) {
416 my $caller = caller;
417 $self->_croak("'$caller' cannot alter the value of '$field' on objects of class '$class'");
418 }
419 else {
420 return $self->get($field);
421 }
422 };
423}
424
425=head2 make_wo_accessor
426
- -
436sub make_wo_accessor {
437 my($class, $field) = @_;
438
439 return sub {
440 my $self = shift;
441
442 unless (@_) {
443 my $caller = caller;
444 $self->_croak("'$caller' cannot access the value of '$field' on objects of class '$class'");
445 }
446 else {
447 return $self->set($field, @_);
448 }
449 };
450}
451
452=head1 EXCEPTIONS
453
- -
4603160µs110µs
# spent 10µs within Class::Accessor::BEGIN@460 which was called: # once (10µs+0s) by base::import at line 460
use Carp ();
# spent 10µs making 1 call to Class::Accessor::BEGIN@460
461
462sub _carp {
463 my ($self, $msg) = @_;
464 Carp::carp($msg || $self);
465 return;
466}
467
468sub _croak {
469 my ($self, $msg) = @_;
470 Carp::croak($msg || $self);
471 return;
472}
473
474=head1 EFFICIENCY
475
- -
67515µs1;