| Filename | /2home/ss5/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/x86_64-linux/Moose/Meta/TypeCoercion.pm |
| Statements | Executed 22 statements in 812µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 5.05ms | 91.8ms | Moose::Meta::TypeCoercion::BEGIN@14 |
| 1 | 1 | 1 | 9µs | 9µs | Moose::Meta::TypeCoercion::BEGIN@3 |
| 1 | 1 | 1 | 9µs | 14µs | Moose::Meta::TypeCoercion::BEGIN@10 |
| 1 | 1 | 1 | 8µs | 25µs | Moose::Meta::TypeCoercion::BEGIN@11 |
| 1 | 1 | 1 | 8µs | 686µs | Moose::Meta::TypeCoercion::BEGIN@12 |
| 1 | 1 | 1 | 7µs | 7µs | Moose::Meta::TypeCoercion::BEGIN@15 |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeCoercion::__ANON__[:19] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeCoercion::__ANON__[:72] |
| 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 | # spent 9µs within Moose::Meta::TypeCoercion::BEGIN@3 which was called:
# once (9µs+0s) by Moose::BEGIN@30 at line 5 | ||||
| 4 | 1 | 8µs | $Moose::Meta::TypeCoercion::AUTHORITY = 'cpan:STEVAN'; | ||
| 5 | 1 | 25µs | 1 | 9µs | } # spent 9µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@3 |
| 6 | { | ||||
| 7 | 2 | 1µs | $Moose::Meta::TypeCoercion::VERSION = '2.0602'; | ||
| 8 | } | ||||
| 9 | |||||
| 10 | 3 | 19µs | 2 | 19µs | # spent 14µs (9+5) within Moose::Meta::TypeCoercion::BEGIN@10 which was called:
# once (9µs+5µs) by Moose::BEGIN@30 at line 10 # spent 14µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@10
# spent 5µs making 1 call to strict::import |
| 11 | 3 | 19µs | 2 | 42µs | # spent 25µs (8+17) within Moose::Meta::TypeCoercion::BEGIN@11 which was called:
# once (8µs+17µs) by Moose::BEGIN@30 at line 11 # spent 25µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@11
# spent 17µs making 1 call to warnings::import |
| 12 | 3 | 28µs | 2 | 1.36ms | # spent 686µs (8+678) within Moose::Meta::TypeCoercion::BEGIN@12 which was called:
# once (8µs+678µs) by Moose::BEGIN@30 at line 12 # spent 686µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@12
# spent 678µs making 1 call to metaclass::import |
| 13 | |||||
| 14 | 3 | 150µs | 1 | 91.8ms | # spent 91.8ms (5.05+86.8) within Moose::Meta::TypeCoercion::BEGIN@14 which was called:
# once (5.05ms+86.8ms) by Moose::BEGIN@30 at line 14 # spent 91.8ms making 1 call to Moose::Meta::TypeCoercion::BEGIN@14 |
| 15 | 3 | 516µs | 1 | 7µs | # spent 7µs within Moose::Meta::TypeCoercion::BEGIN@15 which was called:
# once (7µs+0s) by Moose::BEGIN@30 at line 15 # spent 7µs making 1 call to Moose::Meta::TypeCoercion::BEGIN@15 |
| 16 | |||||
| 17 | __PACKAGE__->meta->add_attribute('type_coercion_map' => ( | ||||
| 18 | reader => 'type_coercion_map', | ||||
| 19 | default => sub { [] }, | ||||
| 20 | 1 | 8µs | 3 | 429µs | Class::MOP::_definition_context(), # spent 395µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 24µs making 1 call to Moose::Meta::TypeCoercion::meta
# spent 10µs making 1 call to Class::MOP::_definition_context |
| 21 | )); | ||||
| 22 | |||||
| 23 | 1 | 11µs | 4 | 3.88ms | __PACKAGE__->meta->add_attribute( # spent 2.85ms making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 1.02ms making 1 call to Moose::Meta::Attribute::new
# spent 11µs making 1 call to Moose::Meta::TypeCoercion::meta
# spent 5µs making 1 call to Class::MOP::_definition_context |
| 24 | Moose::Meta::Attribute->new('type_constraint' => ( | ||||
| 25 | reader => 'type_constraint', | ||||
| 26 | weak_ref => 1, | ||||
| 27 | Class::MOP::_definition_context(), | ||||
| 28 | )) | ||||
| 29 | ); | ||||
| 30 | |||||
| 31 | # private accessor | ||||
| 32 | 1 | 9µs | 3 | 401µs | __PACKAGE__->meta->add_attribute('compiled_type_coercion' => ( # spent 376µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 14µs making 1 call to Moose::Meta::TypeCoercion::meta
# spent 11µs making 1 call to Class::MOP::_definition_context |
| 33 | accessor => '_compiled_type_coercion', | ||||
| 34 | Class::MOP::_definition_context(), | ||||
| 35 | )); | ||||
| 36 | |||||
| 37 | sub new { | ||||
| 38 | my $class = shift; | ||||
| 39 | my $self = Class::MOP::class_of($class)->new_object(@_); | ||||
| 40 | $self->compile_type_coercion; | ||||
| 41 | return $self; | ||||
| 42 | } | ||||
| 43 | |||||
| 44 | sub compile_type_coercion { | ||||
| 45 | my $self = shift; | ||||
| 46 | my @coercion_map = @{$self->type_coercion_map}; | ||||
| 47 | my @coercions; | ||||
| 48 | while (@coercion_map) { | ||||
| 49 | my ($constraint_name, $action) = splice(@coercion_map, 0, 2); | ||||
| 50 | my $type_constraint = ref $constraint_name ? $constraint_name : Moose::Util::TypeConstraints::find_or_parse_type_constraint($constraint_name); | ||||
| 51 | |||||
| 52 | unless ( defined $type_constraint ) { | ||||
| 53 | require Moose; | ||||
| 54 | Moose->throw_error("Could not find the type constraint ($constraint_name) to coerce from"); | ||||
| 55 | } | ||||
| 56 | |||||
| 57 | push @coercions => [ | ||||
| 58 | $type_constraint->_compiled_type_constraint, | ||||
| 59 | $action | ||||
| 60 | ]; | ||||
| 61 | } | ||||
| 62 | $self->_compiled_type_coercion(sub { | ||||
| 63 | my $thing = shift; | ||||
| 64 | foreach my $coercion (@coercions) { | ||||
| 65 | my ($constraint, $converter) = @$coercion; | ||||
| 66 | if ($constraint->($thing)) { | ||||
| 67 | local $_ = $thing; | ||||
| 68 | return $converter->($thing); | ||||
| 69 | } | ||||
| 70 | } | ||||
| 71 | return $thing; | ||||
| 72 | }); | ||||
| 73 | } | ||||
| 74 | |||||
| 75 | sub has_coercion_for_type { | ||||
| 76 | my ($self, $type_name) = @_; | ||||
| 77 | my %coercion_map = @{$self->type_coercion_map}; | ||||
| 78 | exists $coercion_map{$type_name} ? 1 : 0; | ||||
| 79 | } | ||||
| 80 | |||||
| 81 | sub add_type_coercions { | ||||
| 82 | my ($self, @new_coercion_map) = @_; | ||||
| 83 | |||||
| 84 | my $coercion_map = $self->type_coercion_map; | ||||
| 85 | my %has_coercion = @$coercion_map; | ||||
| 86 | |||||
| 87 | while (@new_coercion_map) { | ||||
| 88 | my ($constraint_name, $action) = splice(@new_coercion_map, 0, 2); | ||||
| 89 | |||||
| 90 | if ( exists $has_coercion{$constraint_name} ) { | ||||
| 91 | require Moose; | ||||
| 92 | Moose->throw_error("A coercion action already exists for '$constraint_name'") | ||||
| 93 | } | ||||
| 94 | |||||
| 95 | push @{$coercion_map} => ($constraint_name, $action); | ||||
| 96 | } | ||||
| 97 | |||||
| 98 | # and re-compile ... | ||||
| 99 | $self->compile_type_coercion; | ||||
| 100 | } | ||||
| 101 | |||||
| 102 | sub coerce { $_[0]->_compiled_type_coercion->($_[1]) } | ||||
| 103 | |||||
| 104 | |||||
| 105 | 1 | 19µs | 1; | ||
| 106 | |||||
| 107 | # ABSTRACT: The Moose Type Coercion metaclass | ||||
| 108 | |||||
| - - | |||||
| 111 | =pod | ||||
| 112 | |||||
| 113 | =head1 NAME | ||||
| 114 | |||||
| 115 | Moose::Meta::TypeCoercion - The Moose Type Coercion metaclass | ||||
| 116 | |||||
| 117 | =head1 VERSION | ||||
| 118 | |||||
| 119 | version 2.0602 | ||||
| 120 | |||||
| 121 | =head1 DESCRIPTION | ||||
| 122 | |||||
| 123 | A type coercion object is basically a mapping of one or more type | ||||
| 124 | constraints and the associated coercions subroutines. | ||||
| 125 | |||||
| 126 | It's unlikely that you will need to instantiate an object of this | ||||
| 127 | class directly, as it's part of the deep internals of Moose. | ||||
| 128 | |||||
| 129 | =head1 METHODS | ||||
| 130 | |||||
| 131 | =over 4 | ||||
| 132 | |||||
| 133 | =item B<< Moose::Meta::TypeCoercion->new(%options) >> | ||||
| 134 | |||||
| 135 | Creates a new type coercion object, based on the options provided. | ||||
| 136 | |||||
| 137 | =over 8 | ||||
| 138 | |||||
| 139 | =item * type_constraint | ||||
| 140 | |||||
| 141 | This is the L<Moose::Meta::TypeConstraint> object for the type that is | ||||
| 142 | being coerced I<to>. | ||||
| 143 | |||||
| 144 | =back | ||||
| 145 | |||||
| 146 | =item B<< $coercion->type_coercion_map >> | ||||
| 147 | |||||
| 148 | This returns the map of type constraints to coercions as an array | ||||
| 149 | reference. The values of the array alternate between type names and | ||||
| 150 | subroutine references which implement the coercion. | ||||
| 151 | |||||
| 152 | The value is an array reference because coercions are tried in the | ||||
| 153 | order they are added. | ||||
| 154 | |||||
| 155 | =item B<< $coercion->type_constraint >> | ||||
| 156 | |||||
| 157 | This returns the L<Moose::Meta::TypeConstraint> that was passed to the | ||||
| 158 | constructor. | ||||
| 159 | |||||
| 160 | =item B<< $coercion->has_coercion_for_type($type_name) >> | ||||
| 161 | |||||
| 162 | Returns true if the coercion can coerce the named type. | ||||
| 163 | |||||
| 164 | =item B<< $coercion->add_type_coercions( $type_name => $sub, ... ) >> | ||||
| 165 | |||||
| 166 | This method takes a list of type names and subroutine references. If | ||||
| 167 | the coercion already has a mapping for a given type, it throws an | ||||
| 168 | exception. | ||||
| 169 | |||||
| 170 | Coercions are actually | ||||
| 171 | |||||
| 172 | =item B<< $coercion->coerce($value) >> | ||||
| 173 | |||||
| 174 | This method takes a value and applies the first valid coercion it | ||||
| 175 | finds. | ||||
| 176 | |||||
| 177 | This means that if the value could belong to more than type in the | ||||
| 178 | coercion object, the first coercion added is used. | ||||
| 179 | |||||
| 180 | =item B<< Moose::Meta::TypeCoercion->meta >> | ||||
| 181 | |||||
| 182 | This will return a L<Class::MOP::Class> instance for this class. | ||||
| 183 | |||||
| 184 | =back | ||||
| 185 | |||||
| 186 | =head1 BUGS | ||||
| 187 | |||||
| 188 | See L<Moose/BUGS> for details on reporting bugs. | ||||
| 189 | |||||
| 190 | =head1 AUTHOR | ||||
| 191 | |||||
| 192 | Moose is maintained by the Moose Cabal, along with the help of many contributors. See L<Moose/CABAL> and L<Moose/CONTRIBUTORS> for details. | ||||
| 193 | |||||
| 194 | =head1 COPYRIGHT AND LICENSE | ||||
| 195 | |||||
| 196 | This software is copyright (c) 2012 by Infinity Interactive, Inc.. | ||||
| 197 | |||||
| 198 | This is free software; you can redistribute it and/or modify it under | ||||
| 199 | the same terms as the Perl 5 programming language system itself. | ||||
| 200 | |||||
| 201 | =cut | ||||
| 202 | |||||
| 203 | |||||
| 204 | __END__ |