| Filename | /home/ss5/perl5/perlbrew/perls/perl-5.22.0/lib/site_perl/5.22.0/x86_64-linux/Moose/Meta/TypeCoercion.pm |
| Statements | Executed 17 statements in 454µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 3.15ms | 79.6ms | Moose::Meta::TypeCoercion::BEGIN@8 |
| 1 | 1 | 1 | 15µs | 408µs | Moose::Meta::TypeCoercion::BEGIN@6 |
| 1 | 1 | 1 | 9µs | 11µs | Moose::Meta::TypeCoercion::BEGIN@4 |
| 1 | 1 | 1 | 5µs | 144µs | Moose::Meta::TypeCoercion::BEGIN@11 |
| 1 | 1 | 1 | 5µs | 9µs | Moose::Meta::TypeCoercion::BEGIN@5 |
| 1 | 1 | 1 | 3µs | 3µs | Moose::Meta::TypeCoercion::BEGIN@9 |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeCoercion::__ANON__[:15] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeCoercion::__ANON__[:69] |
| 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 | package Moose::Meta::TypeCoercion; | ||||
| 2 | 1 | 500ns | our $VERSION = '2.1605'; | ||
| 3 | |||||
| 4 | 2 | 25µs | 2 | 13µs | # spent 11µs (9+2) within Moose::Meta::TypeCoercion::BEGIN@4 which was called:
# once (9µs+2µs) by Moose::BEGIN@26 at line 4 # spent 11µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@4
# spent 2µs making 1 call to strict::import |
| 5 | 2 | 12µs | 2 | 13µs | # spent 9µs (5+4) within Moose::Meta::TypeCoercion::BEGIN@5 which was called:
# once (5µs+4µs) by Moose::BEGIN@26 at line 5 # spent 9µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@5
# spent 4µs making 1 call to warnings::import |
| 6 | 2 | 35µs | 2 | 800µs | # spent 408µs (15+392) within Moose::Meta::TypeCoercion::BEGIN@6 which was called:
# once (15µs+392µs) by Moose::BEGIN@26 at line 6 # spent 408µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@6
# spent 392µs making 1 call to metaclass::import |
| 7 | |||||
| 8 | 2 | 71µs | 1 | 79.6ms | # spent 79.6ms (3.15+76.4) within Moose::Meta::TypeCoercion::BEGIN@8 which was called:
# once (3.15ms+76.4ms) by Moose::BEGIN@26 at line 8 # spent 79.6ms making 1 call to Moose::Meta::TypeCoercion::BEGIN@8 |
| 9 | 2 | 15µs | 1 | 3µs | # spent 3µs within Moose::Meta::TypeCoercion::BEGIN@9 which was called:
# once (3µs+0s) by Moose::BEGIN@26 at line 9 # spent 3µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@9 |
| 10 | |||||
| 11 | 2 | 280µs | 2 | 284µs | # spent 144µs (5+139) within Moose::Meta::TypeCoercion::BEGIN@11 which was called:
# once (5µs+139µs) by Moose::BEGIN@26 at line 11 # spent 144µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@11
# spent 139µs making 1 call to Sub::Exporter::__ANON__[Sub/Exporter.pm:337] |
| 12 | |||||
| 13 | __PACKAGE__->meta->add_attribute('type_coercion_map' => ( | ||||
| 14 | reader => 'type_coercion_map', | ||||
| 15 | default => sub { [] }, | ||||
| 16 | 1 | 4µs | 3 | 220µs | Class::MOP::_definition_context(), # spent 203µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 13µs making 1 call to Moose::Meta::TypeCoercion::meta
# spent 4µs making 1 call to Class::MOP::_definition_context |
| 17 | )); | ||||
| 18 | |||||
| 19 | 1 | 4µs | 4 | 1.80ms | __PACKAGE__->meta->add_attribute( # spent 1.26ms making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 529µs making 1 call to Moose::Meta::Attribute::new
# spent 6µs making 1 call to Moose::Meta::TypeCoercion::meta
# spent 3µs making 1 call to Class::MOP::_definition_context |
| 20 | Moose::Meta::Attribute->new('type_constraint' => ( | ||||
| 21 | reader => 'type_constraint', | ||||
| 22 | weak_ref => 1, | ||||
| 23 | Class::MOP::_definition_context(), | ||||
| 24 | )) | ||||
| 25 | ); | ||||
| 26 | |||||
| 27 | # private accessor | ||||
| 28 | 1 | 2µs | 3 | 197µs | __PACKAGE__->meta->add_attribute('compiled_type_coercion' => ( # spent 189µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 5µs making 1 call to Moose::Meta::TypeCoercion::meta
# spent 4µs making 1 call to Class::MOP::_definition_context |
| 29 | accessor => '_compiled_type_coercion', | ||||
| 30 | Class::MOP::_definition_context(), | ||||
| 31 | )); | ||||
| 32 | |||||
| 33 | sub new { | ||||
| 34 | my $class = shift; | ||||
| 35 | my $self = Class::MOP::class_of($class)->new_object(@_); | ||||
| 36 | $self->compile_type_coercion; | ||||
| 37 | return $self; | ||||
| 38 | } | ||||
| 39 | |||||
| 40 | sub compile_type_coercion { | ||||
| 41 | my $self = shift; | ||||
| 42 | my @coercion_map = @{$self->type_coercion_map}; | ||||
| 43 | my @coercions; | ||||
| 44 | while (@coercion_map) { | ||||
| 45 | my ($constraint_name, $action) = splice(@coercion_map, 0, 2); | ||||
| 46 | my $type_constraint = ref $constraint_name ? $constraint_name : Moose::Util::TypeConstraints::find_or_parse_type_constraint($constraint_name); | ||||
| 47 | |||||
| 48 | unless ( defined $type_constraint ) { | ||||
| 49 | throw_exception( CouldNotFindTypeConstraintToCoerceFrom => constraint_name => $constraint_name, | ||||
| 50 | instance => $self | ||||
| 51 | ); | ||||
| 52 | } | ||||
| 53 | |||||
| 54 | push @coercions => [ | ||||
| 55 | $type_constraint->_compiled_type_constraint, | ||||
| 56 | $action | ||||
| 57 | ]; | ||||
| 58 | } | ||||
| 59 | $self->_compiled_type_coercion(sub { | ||||
| 60 | my $thing = shift; | ||||
| 61 | foreach my $coercion (@coercions) { | ||||
| 62 | my ($constraint, $converter) = @$coercion; | ||||
| 63 | if ($constraint->($thing)) { | ||||
| 64 | local $_ = $thing; | ||||
| 65 | return $converter->($thing); | ||||
| 66 | } | ||||
| 67 | } | ||||
| 68 | return $thing; | ||||
| 69 | }); | ||||
| 70 | } | ||||
| 71 | |||||
| 72 | sub has_coercion_for_type { | ||||
| 73 | my ($self, $type_name) = @_; | ||||
| 74 | my %coercion_map = @{$self->type_coercion_map}; | ||||
| 75 | exists $coercion_map{$type_name} ? 1 : 0; | ||||
| 76 | } | ||||
| 77 | |||||
| 78 | sub add_type_coercions { | ||||
| 79 | my ($self, @new_coercion_map) = @_; | ||||
| 80 | |||||
| 81 | my $coercion_map = $self->type_coercion_map; | ||||
| 82 | my %has_coercion = @$coercion_map; | ||||
| 83 | |||||
| 84 | while (@new_coercion_map) { | ||||
| 85 | my ($constraint_name, $action) = splice(@new_coercion_map, 0, 2); | ||||
| 86 | |||||
| 87 | if ( exists $has_coercion{$constraint_name} ) { | ||||
| 88 | throw_exception( CoercionAlreadyExists => constraint_name => $constraint_name, | ||||
| 89 | instance => $self | ||||
| 90 | ); | ||||
| 91 | } | ||||
| 92 | |||||
| 93 | push @{$coercion_map} => ($constraint_name, $action); | ||||
| 94 | } | ||||
| 95 | |||||
| 96 | # and re-compile ... | ||||
| 97 | $self->compile_type_coercion; | ||||
| 98 | } | ||||
| 99 | |||||
| 100 | sub coerce { $_[0]->_compiled_type_coercion->($_[1]) } | ||||
| 101 | |||||
| 102 | |||||
| 103 | 1 | 5µs | 1; | ||
| 104 | |||||
| 105 | # ABSTRACT: The Moose Type Coercion metaclass | ||||
| 106 | |||||
| 107 | __END__ |