Filename | /Users/ap13/perl5/lib/perl5/darwin-2level/Moose/Meta/TypeCoercion.pm |
Statements | Executed 17 statements in 753µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 6.14ms | 97.4ms | BEGIN@8 | Moose::Meta::TypeCoercion::
1 | 1 | 1 | 16µs | 34µs | BEGIN@4 | Moose::Meta::TypeCoercion::
1 | 1 | 1 | 10µs | 210µs | BEGIN@11 | Moose::Meta::TypeCoercion::
1 | 1 | 1 | 9µs | 16µs | BEGIN@5 | Moose::Meta::TypeCoercion::
1 | 1 | 1 | 8µs | 619µs | BEGIN@6 | Moose::Meta::TypeCoercion::
1 | 1 | 1 | 6µs | 6µs | BEGIN@9 | Moose::Meta::TypeCoercion::
0 | 0 | 0 | 0s | 0s | __ANON__[:15] | Moose::Meta::TypeCoercion::
0 | 0 | 0 | 0s | 0s | __ANON__[:69] | Moose::Meta::TypeCoercion::
0 | 0 | 0 | 0s | 0s | add_type_coercions | Moose::Meta::TypeCoercion::
0 | 0 | 0 | 0s | 0s | coerce | Moose::Meta::TypeCoercion::
0 | 0 | 0 | 0s | 0s | compile_type_coercion | Moose::Meta::TypeCoercion::
0 | 0 | 0 | 0s | 0s | has_coercion_for_type | Moose::Meta::TypeCoercion::
0 | 0 | 0 | 0s | 0s | new | Moose::Meta::TypeCoercion::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Moose::Meta::TypeCoercion; | ||||
2 | 1 | 800ns | our $VERSION = '2.1403'; | ||
3 | |||||
4 | 2 | 25µs | 2 | 52µs | # spent 34µs (16+18) within Moose::Meta::TypeCoercion::BEGIN@4 which was called:
# once (16µs+18µs) by Moose::BEGIN@26 at line 4 # spent 34µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@4
# spent 18µs making 1 call to strict::import |
5 | 2 | 22µs | 2 | 23µs | # spent 16µs (9+7) within Moose::Meta::TypeCoercion::BEGIN@5 which was called:
# once (9µs+7µs) by Moose::BEGIN@26 at line 5 # spent 16µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@5
# spent 7µs making 1 call to warnings::import |
6 | 2 | 27µs | 2 | 1.23ms | # spent 619µs (8+612) within Moose::Meta::TypeCoercion::BEGIN@6 which was called:
# once (8µs+612µs) by Moose::BEGIN@26 at line 6 # spent 619µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@6
# spent 612µs making 1 call to metaclass::import |
7 | |||||
8 | 2 | 142µs | 1 | 97.4ms | # spent 97.4ms (6.14+91.3) within Moose::Meta::TypeCoercion::BEGIN@8 which was called:
# once (6.14ms+91.3ms) by Moose::BEGIN@26 at line 8 # spent 97.4ms making 1 call to Moose::Meta::TypeCoercion::BEGIN@8 |
9 | 2 | 28µs | 1 | 6µs | # spent 6µs within Moose::Meta::TypeCoercion::BEGIN@9 which was called:
# once (6µs+0s) by Moose::BEGIN@26 at line 9 # spent 6µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@9 |
10 | |||||
11 | 2 | 456µs | 2 | 409µs | # spent 210µs (10+200) within Moose::Meta::TypeCoercion::BEGIN@11 which was called:
# once (10µs+200µs) by Moose::BEGIN@26 at line 11 # spent 210µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@11
# spent 200µ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 | 8µs | 3 | 395µs | Class::MOP::_definition_context(), # spent 368µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 20µs making 1 call to Moose::Meta::TypeCoercion::meta
# spent 6µs making 1 call to Class::MOP::_definition_context |
17 | )); | ||||
18 | |||||
19 | 1 | 12µs | 4 | 3.81ms | __PACKAGE__->meta->add_attribute( # spent 2.74ms making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 1.05ms making 1 call to Moose::Meta::Attribute::new
# spent 12µs making 1 call to Moose::Meta::TypeCoercion::meta
# spent 6µ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 | 8µs | 3 | 514µs | __PACKAGE__->meta->add_attribute('compiled_type_coercion' => ( # spent 492µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 14µs making 1 call to Moose::Meta::TypeCoercion::meta
# spent 9µ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 | 25µs | 1; | ||
104 | |||||
105 | # ABSTRACT: The Moose Type Coercion metaclass | ||||
106 | |||||
107 | __END__ |