| Filename | /home/doy/perl5/perlbrew/perls/perl-5.10.1/lib/site_perl/5.10.1/x86_64-linux/Moose/Meta/TypeConstraint/Union.pm |
| Statements | Executed 86 statements in 4.99ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 1.56ms | 5.47ms | Moose::Meta::TypeConstraint::Union::BEGIN@8 |
| 6 | 1 | 1 | 949µs | 3.42ms | Moose::Meta::TypeConstraint::Union::new |
| 6 | 1 | 1 | 248µs | 319µs | Moose::Meta::TypeConstraint::Union::_actually_compile_type_constraint |
| 1 | 1 | 1 | 70µs | 88µs | Moose::Meta::TypeConstraint::Union::BEGIN@4 |
| 6 | 1 | 1 | 55µs | 55µs | Moose::Meta::TypeConstraint::Union::CORE:sort (opcode) |
| 1 | 1 | 1 | 43µs | 200µs | Moose::Meta::TypeConstraint::Union::BEGIN@10 |
| 1 | 1 | 1 | 40µs | 279µs | Moose::Meta::TypeConstraint::Union::BEGIN@16 |
| 1 | 1 | 1 | 37µs | 105µs | Moose::Meta::TypeConstraint::Union::BEGIN@5 |
| 1 | 1 | 1 | 35µs | 3.08ms | Moose::Meta::TypeConstraint::Union::BEGIN@6 |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::__ANON__[:123] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::__ANON__[:20] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::__ANON__[:34] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::__ANON__[:74] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::coercion |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::create_child_type |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::equals |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::find_type_for |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::has_coercion |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::is_a_type_of |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::is_subtype_of |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::parents |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::validate |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | |||||
| 2 | package Moose::Meta::TypeConstraint::Union; | ||||
| 3 | |||||
| 4 | 3 | 104µs | 2 | 105µs | # spent 88µs (70+17) within Moose::Meta::TypeConstraint::Union::BEGIN@4 which was called:
# once (70µs+17µs) by Moose::Util::TypeConstraints::BEGIN@30 at line 4 # spent 88µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@4
# spent 17µs making 1 call to strict::import |
| 5 | 3 | 128µs | 2 | 173µs | # spent 105µs (37+68) within Moose::Meta::TypeConstraint::Union::BEGIN@5 which was called:
# once (37µs+68µs) by Moose::Util::TypeConstraints::BEGIN@30 at line 5 # spent 105µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@5
# spent 68µs making 1 call to warnings::import |
| 6 | 3 | 139µs | 2 | 6.13ms | # spent 3.08ms (35µs+3.05) within Moose::Meta::TypeConstraint::Union::BEGIN@6 which was called:
# once (35µs+3.05ms) by Moose::Util::TypeConstraints::BEGIN@30 at line 6 # spent 3.08ms making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@6
# spent 3.05ms making 1 call to metaclass::import |
| 7 | |||||
| 8 | 3 | 470µs | 1 | 5.47ms | # spent 5.47ms (1.56+3.91) within Moose::Meta::TypeConstraint::Union::BEGIN@8 which was called:
# once (1.56ms+3.91ms) by Moose::Util::TypeConstraints::BEGIN@30 at line 8 # spent 5.47ms making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@8 |
| 9 | |||||
| 10 | 3 | 211µs | 2 | 357µs | # spent 200µs (43+157) within Moose::Meta::TypeConstraint::Union::BEGIN@10 which was called:
# once (43µs+157µs) by Moose::Util::TypeConstraints::BEGIN@30 at line 10 # spent 200µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@10
# spent 157µs making 1 call to Exporter::import |
| 11 | |||||
| 12 | 1 | 5µs | our $VERSION = '1.19'; | ||
| 13 | 1 | 68µs | $VERSION = eval $VERSION; # spent 11µs executing statements in string eval | ||
| 14 | 1 | 4µs | our $AUTHORITY = 'cpan:STEVAN'; | ||
| 15 | |||||
| 16 | 3 | 2.62ms | 2 | 519µs | # spent 279µs (40+239) within Moose::Meta::TypeConstraint::Union::BEGIN@16 which was called:
# once (40µs+239µs) by Moose::Util::TypeConstraints::BEGIN@30 at line 16 # spent 279µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@16
# spent 240µs making 1 call to base::import |
| 17 | |||||
| 18 | __PACKAGE__->meta->add_attribute('type_constraints' => ( | ||||
| 19 | accessor => 'type_constraints', | ||||
| 20 | default => sub { [] } | ||||
| 21 | 1 | 34µs | 2 | 2.23ms | )); # spent 2.10ms making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 126µs making 1 call to Moose::Meta::TypeConstraint::Union::meta |
| 22 | |||||
| 23 | # spent 3.42ms (949µs+2.47) within Moose::Meta::TypeConstraint::Union::new which was called 6 times, avg 570µs/call:
# 6 times (949µs+2.47ms) by Moose::Util::TypeConstraints::create_type_constraint_union at line 95 of Moose/Util/TypeConstraints.pm, avg 570µs/call | ||||
| 24 | 6 | 45µs | my ($class, %options) = @_; | ||
| 25 | |||||
| 26 | 15 | 175µs | 15 | 206µs | my $name = join '|' => sort { $a cmp $b } # spent 206µs making 15 calls to Moose::Meta::TypeConstraint::name, avg 14µs/call |
| 27 | 6 | 212µs | 6 | 55µs | map { $_->name } @{ $options{type_constraints} }; # spent 55µs making 6 calls to Moose::Meta::TypeConstraint::Union::CORE:sort, avg 9µs/call |
| 28 | |||||
| 29 | 6 | 279µs | 6 | 2.14ms | my $self = $class->SUPER::new( # spent 2.14ms making 6 calls to Moose::Meta::TypeConstraint::new, avg 357µs/call |
| 30 | name => $name, | ||||
| 31 | %options, | ||||
| 32 | ); | ||||
| 33 | |||||
| 34 | 6 | 135µs | 6 | 69µs | $self->_set_constraint(sub { $self->check($_[0]) }); # spent 69µs making 6 calls to Moose::Meta::TypeConstraint::_set_constraint, avg 12µs/call |
| 35 | |||||
| 36 | 6 | 82µs | return $self; | ||
| 37 | } | ||||
| 38 | |||||
| 39 | # XXX - this is a rather gross implementation of laziness for the benefit of | ||||
| 40 | # MX::Types. If we try to call ->has_coercion on the objects during object | ||||
| 41 | # construction, this does not work when defining a recursive constraint with | ||||
| 42 | # MX::Types. | ||||
| 43 | sub coercion { | ||||
| 44 | my $self = shift; | ||||
| 45 | |||||
| 46 | return $self->{coercion} if exists $self->{coercion}; | ||||
| 47 | |||||
| 48 | # Using any instead of grep here causes a weird error with some corner | ||||
| 49 | # cases when MX::Types is in use. See RT #61001. | ||||
| 50 | if ( grep { $_->has_coercion } @{ $self->type_constraints } ) { | ||||
| 51 | return $self->{coercion} = Moose::Meta::TypeCoercion::Union->new( | ||||
| 52 | type_constraint => $self ); | ||||
| 53 | } | ||||
| 54 | else { | ||||
| 55 | return $self->{coercion} = undef; | ||||
| 56 | } | ||||
| 57 | } | ||||
| 58 | |||||
| 59 | sub has_coercion { | ||||
| 60 | return defined $_[0]->coercion; | ||||
| 61 | } | ||||
| 62 | |||||
| 63 | # spent 319µs (248+71) within Moose::Meta::TypeConstraint::Union::_actually_compile_type_constraint which was called 6 times, avg 53µs/call:
# 6 times (248µs+71µs) by Moose::Meta::TypeConstraint::compile_type_constraint at line 202 of Moose/Meta/TypeConstraint.pm, avg 53µs/call | ||||
| 64 | 6 | 17µs | my $self = shift; | ||
| 65 | |||||
| 66 | 6 | 85µs | 6 | 71µs | my @constraints = @{ $self->type_constraints }; # spent 71µs making 6 calls to Moose::Meta::TypeConstraint::Union::type_constraints, avg 12µs/call |
| 67 | |||||
| 68 | return sub { | ||||
| 69 | my $value = shift; | ||||
| 70 | foreach my $type (@constraints) { | ||||
| 71 | return 1 if $type->check($value); | ||||
| 72 | } | ||||
| 73 | return undef; | ||||
| 74 | 6 | 146µs | }; | ||
| 75 | } | ||||
| 76 | |||||
| 77 | |||||
| 78 | sub equals { | ||||
| 79 | my ( $self, $type_or_name ) = @_; | ||||
| 80 | |||||
| 81 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); | ||||
| 82 | |||||
| 83 | return unless $other->isa(__PACKAGE__); | ||||
| 84 | |||||
| 85 | my @self_constraints = @{ $self->type_constraints }; | ||||
| 86 | my @other_constraints = @{ $other->type_constraints }; | ||||
| 87 | |||||
| 88 | return unless @self_constraints == @other_constraints; | ||||
| 89 | |||||
| 90 | # FIXME presort type constraints for efficiency? | ||||
| 91 | constraint: foreach my $constraint ( @self_constraints ) { | ||||
| 92 | for ( my $i = 0; $i < @other_constraints; $i++ ) { | ||||
| 93 | if ( $constraint->equals($other_constraints[$i]) ) { | ||||
| 94 | splice @other_constraints, $i, 1; | ||||
| 95 | next constraint; | ||||
| 96 | } | ||||
| 97 | } | ||||
| 98 | } | ||||
| 99 | |||||
| 100 | return @other_constraints == 0; | ||||
| 101 | } | ||||
| 102 | |||||
| 103 | sub parents { | ||||
| 104 | my $self = shift; | ||||
| 105 | $self->type_constraints; | ||||
| 106 | } | ||||
| 107 | |||||
| 108 | sub validate { | ||||
| 109 | my ($self, $value) = @_; | ||||
| 110 | my $message; | ||||
| 111 | foreach my $type (@{$self->type_constraints}) { | ||||
| 112 | my $err = $type->validate($value); | ||||
| 113 | return unless defined $err; | ||||
| 114 | $message .= ($message ? ' and ' : '') . $err | ||||
| 115 | if defined $err; | ||||
| 116 | } | ||||
| 117 | return ($message . ' in (' . $self->name . ')') ; | ||||
| 118 | } | ||||
| 119 | |||||
| 120 | sub find_type_for { | ||||
| 121 | my ($self, $value) = @_; | ||||
| 122 | |||||
| 123 | return first { $_->check($value) } @{ $self->type_constraints }; | ||||
| 124 | } | ||||
| 125 | |||||
| 126 | sub is_a_type_of { | ||||
| 127 | my ($self, $type_name) = @_; | ||||
| 128 | foreach my $type (@{$self->type_constraints}) { | ||||
| 129 | return 1 if $type->is_a_type_of($type_name); | ||||
| 130 | } | ||||
| 131 | return 0; | ||||
| 132 | } | ||||
| 133 | |||||
| 134 | sub is_subtype_of { | ||||
| 135 | my ($self, $type_name) = @_; | ||||
| 136 | foreach my $type (@{$self->type_constraints}) { | ||||
| 137 | return 1 if $type->is_subtype_of($type_name); | ||||
| 138 | } | ||||
| 139 | return 0; | ||||
| 140 | } | ||||
| 141 | |||||
| 142 | sub create_child_type { | ||||
| 143 | my ( $self, %opts ) = @_; | ||||
| 144 | |||||
| 145 | my $constraint | ||||
| 146 | = Moose::Meta::TypeConstraint->new( %opts, parent => $self ); | ||||
| 147 | |||||
| 148 | # if we have a type constraint union, and no | ||||
| 149 | # type check, this means we are just aliasing | ||||
| 150 | # the union constraint, which means we need to | ||||
| 151 | # handle this differently. | ||||
| 152 | # - SL | ||||
| 153 | if ( not( defined $opts{constraint} ) | ||||
| 154 | && $self->has_coercion ) { | ||||
| 155 | $constraint->coercion( | ||||
| 156 | Moose::Meta::TypeCoercion::Union->new( | ||||
| 157 | type_constraint => $self, | ||||
| 158 | ) | ||||
| 159 | ); | ||||
| 160 | } | ||||
| 161 | |||||
| 162 | return $constraint; | ||||
| 163 | } | ||||
| 164 | |||||
| 165 | 1 | 26µs | 1; | ||
| 166 | |||||
| 167 | __END__ | ||||
# spent 55µs within Moose::Meta::TypeConstraint::Union::CORE:sort which was called 6 times, avg 9µs/call:
# 6 times (55µs+0s) by Moose::Meta::TypeConstraint::Union::new at line 27, avg 9µs/call |