| Filename | /home/doy/coding/src/Moose/blib/lib//Moose/Meta/TypeCoercion.pm |
| Statements | Executed 22 statements in 2.94ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 20.2ms | 396ms | Moose::Meta::TypeCoercion::BEGIN@8 |
| 1 | 1 | 1 | 105µs | 131µs | Moose::Meta::TypeCoercion::BEGIN@4 |
| 1 | 1 | 1 | 58µs | 143µs | Moose::Meta::TypeCoercion::BEGIN@5 |
| 1 | 1 | 1 | 40µs | 3.00ms | 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 | 142µs | 2 | 156µs | # spent 131µs (105+25) within Moose::Meta::TypeCoercion::BEGIN@4 which was called:
# once (105µs+25µs) by Moose::BEGIN@21 at line 4 # spent 131µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@4
# spent 25µs making 1 call to strict::import |
| 5 | 3 | 131µs | 2 | 227µs | # spent 143µs (58+85) within Moose::Meta::TypeCoercion::BEGIN@5 which was called:
# once (58µs+85µs) by Moose::BEGIN@21 at line 5 # spent 143µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@5
# spent 85µs making 1 call to warnings::import |
| 6 | 3 | 136µs | 2 | 5.96ms | # spent 3.00ms (40µs+2.96) within Moose::Meta::TypeCoercion::BEGIN@6 which was called:
# once (40µs+2.96ms) by Moose::BEGIN@21 at line 6 # spent 3.00ms making 1 call to Moose::Meta::TypeCoercion::BEGIN@6
# spent 2.96ms making 1 call to metaclass::import |
| 7 | |||||
| 8 | 3 | 571µs | 1 | 396ms | # spent 396ms (20.2+376) within Moose::Meta::TypeCoercion::BEGIN@8 which was called:
# once (20.2ms+376ms) by Moose::BEGIN@21 at line 8 # spent 396ms making 1 call to Moose::Meta::TypeCoercion::BEGIN@8 |
| 9 | 3 | 1.73ms | 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 | 63µs | $VERSION = eval $VERSION; # spent 10µs executing statements in string eval | ||
| 13 | 1 | 4µs | our $AUTHORITY = 'cpan:STEVAN'; | ||
| 14 | |||||
| 15 | __PACKAGE__->meta->add_attribute('type_coercion_map' => ( | ||||
| 16 | reader => 'type_coercion_map', | ||||
| 17 | default => sub { [] } | ||||
| 18 | 1 | 34µs | 2 | 2.16ms | )); # spent 2.03ms making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 132µs making 1 call to Moose::Meta::TypeCoercion::meta |
| 19 | |||||
| 20 | 1 | 46µs | 3 | 19.3ms | __PACKAGE__->meta->add_attribute( # spent 12.9ms making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 6.26ms making 1 call to Moose::Meta::Attribute::new
# spent 86µ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.05ms | __PACKAGE__->meta->add_attribute('compiled_type_coercion' => ( # spent 1.97ms making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 79µ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__ |