| Filename | /home/doy/perl5/perlbrew/perls/perl-5.10.1/lib/site_perl/5.10.1/x86_64-linux/Moose/Meta/TypeCoercion.pm |
| Statements | Executed 22 statements in 2.78ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 15.7ms | 353ms | Moose::Meta::TypeCoercion::BEGIN@8 |
| 1 | 1 | 1 | 75µs | 93µs | Moose::Meta::TypeCoercion::BEGIN@4 |
| 1 | 1 | 1 | 44µs | 124µs | Moose::Meta::TypeCoercion::BEGIN@5 |
| 1 | 1 | 1 | 39µs | 3.39ms | Moose::Meta::TypeCoercion::BEGIN@6 |
| 1 | 1 | 1 | 24µs | 24µs | Moose::Meta::TypeCoercion::BEGIN@9 |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeCoercion::__ANON__[:17] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeCoercion::__ANON__[:67] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeCoercion::add_type_coercions |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeCoercion::coerce |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeCoercion::compile_type_coercion |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeCoercion::has_coercion_for_type |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeCoercion::new |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | |||||
| 2 | package Moose::Meta::TypeCoercion; | ||||
| 3 | |||||
| 4 | 3 | 101µs | 2 | 111µs | # spent 93µs (75+18) within Moose::Meta::TypeCoercion::BEGIN@4 which was called:
# once (75µs+18µs) by Moose::BEGIN@21 at line 4 # spent 93µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@4
# spent 18µs making 1 call to strict::import |
| 5 | 3 | 107µs | 2 | 205µs | # spent 124µs (44+81) within Moose::Meta::TypeCoercion::BEGIN@5 which was called:
# once (44µs+81µs) by Moose::BEGIN@21 at line 5 # spent 124µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@5
# spent 80µs making 1 call to warnings::import |
| 6 | 3 | 169µs | 2 | 6.75ms | # spent 3.39ms (39µs+3.36) within Moose::Meta::TypeCoercion::BEGIN@6 which was called:
# once (39µs+3.36ms) by Moose::BEGIN@21 at line 6 # spent 3.39ms making 1 call to Moose::Meta::TypeCoercion::BEGIN@6
# spent 3.36ms making 1 call to metaclass::import |
| 7 | |||||
| 8 | 3 | 509µs | 1 | 353ms | # spent 353ms (15.7+338) within Moose::Meta::TypeCoercion::BEGIN@8 which was called:
# once (15.7ms+338ms) by Moose::BEGIN@21 at line 8 # spent 353ms making 1 call to Moose::Meta::TypeCoercion::BEGIN@8 |
| 9 | 3 | 1.64ms | 1 | 24µs | # spent 24µs within Moose::Meta::TypeCoercion::BEGIN@9 which was called:
# once (24µs+0s) by Moose::BEGIN@21 at line 9 # spent 24µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@9 |
| 10 | |||||
| 11 | 1 | 4µs | our $VERSION = '1.19'; | ||
| 12 | 1 | 85µs | $VERSION = eval $VERSION; # spent 13µs executing statements in string eval | ||
| 13 | 1 | 5µs | our $AUTHORITY = 'cpan:STEVAN'; | ||
| 14 | |||||
| 15 | __PACKAGE__->meta->add_attribute('type_coercion_map' => ( | ||||
| 16 | reader => 'type_coercion_map', | ||||
| 17 | default => sub { [] } | ||||
| 18 | 1 | 41µs | 2 | 2.28ms | )); # spent 2.14ms making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 137µs making 1 call to Moose::Meta::TypeCoercion::meta |
| 19 | |||||
| 20 | 1 | 45µs | 3 | 19.6ms | __PACKAGE__->meta->add_attribute( # spent 13.0ms making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 6.51ms making 1 call to Moose::Meta::Attribute::new
# spent 84µs making 1 call to Moose::Meta::TypeCoercion::meta |
| 21 | Moose::Meta::Attribute->new('type_constraint' => ( | ||||
| 22 | reader => 'type_constraint', | ||||
| 23 | weak_ref => 1 | ||||
| 24 | )) | ||||
| 25 | ); | ||||
| 26 | |||||
| 27 | # private accessor | ||||
| 28 | 1 | 25µs | 2 | 2.17ms | __PACKAGE__->meta->add_attribute('compiled_type_coercion' => ( # spent 2.09ms making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 81µs making 1 call to Moose::Meta::TypeCoercion::meta |
| 29 | accessor => '_compiled_type_coercion' | ||||
| 30 | )); | ||||
| 31 | |||||
| 32 | sub new { | ||||
| 33 | my $class = shift; | ||||
| 34 | my $self = Class::MOP::class_of($class)->new_object(@_); | ||||
| 35 | $self->compile_type_coercion; | ||||
| 36 | return $self; | ||||
| 37 | } | ||||
| 38 | |||||
| 39 | sub compile_type_coercion { | ||||
| 40 | my $self = shift; | ||||
| 41 | my @coercion_map = @{$self->type_coercion_map}; | ||||
| 42 | my @coercions; | ||||
| 43 | while (@coercion_map) { | ||||
| 44 | my ($constraint_name, $action) = splice(@coercion_map, 0, 2); | ||||
| 45 | my $type_constraint = ref $constraint_name ? $constraint_name : Moose::Util::TypeConstraints::find_or_parse_type_constraint($constraint_name); | ||||
| 46 | |||||
| 47 | unless ( defined $type_constraint ) { | ||||
| 48 | require Moose; | ||||
| 49 | Moose->throw_error("Could not find the type constraint ($constraint_name) to coerce from"); | ||||
| 50 | } | ||||
| 51 | |||||
| 52 | push @coercions => [ | ||||
| 53 | $type_constraint->_compiled_type_constraint, | ||||
| 54 | $action | ||||
| 55 | ]; | ||||
| 56 | } | ||||
| 57 | $self->_compiled_type_coercion(sub { | ||||
| 58 | my $thing = shift; | ||||
| 59 | foreach my $coercion (@coercions) { | ||||
| 60 | my ($constraint, $converter) = @$coercion; | ||||
| 61 | if ($constraint->($thing)) { | ||||
| 62 | local $_ = $thing; | ||||
| 63 | return $converter->($thing); | ||||
| 64 | } | ||||
| 65 | } | ||||
| 66 | return $thing; | ||||
| 67 | }); | ||||
| 68 | } | ||||
| 69 | |||||
| 70 | sub has_coercion_for_type { | ||||
| 71 | my ($self, $type_name) = @_; | ||||
| 72 | my %coercion_map = @{$self->type_coercion_map}; | ||||
| 73 | exists $coercion_map{$type_name} ? 1 : 0; | ||||
| 74 | } | ||||
| 75 | |||||
| 76 | sub add_type_coercions { | ||||
| 77 | my ($self, @new_coercion_map) = @_; | ||||
| 78 | |||||
| 79 | my $coercion_map = $self->type_coercion_map; | ||||
| 80 | my %has_coercion = @$coercion_map; | ||||
| 81 | |||||
| 82 | while (@new_coercion_map) { | ||||
| 83 | my ($constraint_name, $action) = splice(@new_coercion_map, 0, 2); | ||||
| 84 | |||||
| 85 | if ( exists $has_coercion{$constraint_name} ) { | ||||
| 86 | require Moose; | ||||
| 87 | Moose->throw_error("A coercion action already exists for '$constraint_name'") | ||||
| 88 | } | ||||
| 89 | |||||
| 90 | push @{$coercion_map} => ($constraint_name, $action); | ||||
| 91 | } | ||||
| 92 | |||||
| 93 | # and re-compile ... | ||||
| 94 | $self->compile_type_coercion; | ||||
| 95 | } | ||||
| 96 | |||||
| 97 | sub coerce { $_[0]->_compiled_type_coercion->($_[1]) } | ||||
| 98 | |||||
| 99 | |||||
| 100 | 1 | 46µs | 1; | ||
| 101 | |||||
| 102 | __END__ |