← Index
NYTProf Performance Profile   « block view • line view • sub view »
For -e
  Run on Wed Nov 17 21:45:08 2010
Reported on Wed Nov 17 22:10:06 2010

Filename/home/doy/perl5/perlbrew/perls/perl-5.10.1/lib/site_perl/5.10.1/x86_64-linux/Moose/Meta/TypeConstraint/Union.pm
StatementsExecuted 23 statements in 3.77ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1111.60ms5.48msMoose::Meta::TypeConstraint::Union::::BEGIN@8Moose::Meta::TypeConstraint::Union::BEGIN@8
11171µs90µsMoose::Meta::TypeConstraint::Union::::BEGIN@4Moose::Meta::TypeConstraint::Union::BEGIN@4
11143µs201µsMoose::Meta::TypeConstraint::Union::::BEGIN@10Moose::Meta::TypeConstraint::Union::BEGIN@10
11138µs273µsMoose::Meta::TypeConstraint::Union::::BEGIN@16Moose::Meta::TypeConstraint::Union::BEGIN@16
11138µs105µsMoose::Meta::TypeConstraint::Union::::BEGIN@5Moose::Meta::TypeConstraint::Union::BEGIN@5
11135µs3.03msMoose::Meta::TypeConstraint::Union::::BEGIN@6Moose::Meta::TypeConstraint::Union::BEGIN@6
0000s0sMoose::Meta::TypeConstraint::Union::::__ANON__[:123]Moose::Meta::TypeConstraint::Union::__ANON__[:123]
0000s0sMoose::Meta::TypeConstraint::Union::::__ANON__[:20]Moose::Meta::TypeConstraint::Union::__ANON__[:20]
0000s0sMoose::Meta::TypeConstraint::Union::::__ANON__[:34]Moose::Meta::TypeConstraint::Union::__ANON__[:34]
0000s0sMoose::Meta::TypeConstraint::Union::::__ANON__[:74]Moose::Meta::TypeConstraint::Union::__ANON__[:74]
0000s0sMoose::Meta::TypeConstraint::Union::::_actually_compile_type_constraintMoose::Meta::TypeConstraint::Union::_actually_compile_type_constraint
0000s0sMoose::Meta::TypeConstraint::Union::::coercionMoose::Meta::TypeConstraint::Union::coercion
0000s0sMoose::Meta::TypeConstraint::Union::::create_child_typeMoose::Meta::TypeConstraint::Union::create_child_type
0000s0sMoose::Meta::TypeConstraint::Union::::equalsMoose::Meta::TypeConstraint::Union::equals
0000s0sMoose::Meta::TypeConstraint::Union::::find_type_forMoose::Meta::TypeConstraint::Union::find_type_for
0000s0sMoose::Meta::TypeConstraint::Union::::has_coercionMoose::Meta::TypeConstraint::Union::has_coercion
0000s0sMoose::Meta::TypeConstraint::Union::::is_a_type_ofMoose::Meta::TypeConstraint::Union::is_a_type_of
0000s0sMoose::Meta::TypeConstraint::Union::::is_subtype_ofMoose::Meta::TypeConstraint::Union::is_subtype_of
0000s0sMoose::Meta::TypeConstraint::Union::::newMoose::Meta::TypeConstraint::Union::new
0000s0sMoose::Meta::TypeConstraint::Union::::parentsMoose::Meta::TypeConstraint::Union::parents
0000s0sMoose::Meta::TypeConstraint::Union::::validateMoose::Meta::TypeConstraint::Union::validate
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1
2package Moose::Meta::TypeConstraint::Union;
3
43108µs2109µs
# spent 90µs (71+19) within Moose::Meta::TypeConstraint::Union::BEGIN@4 which was called: # once (71µs+19µs) by Moose::Util::TypeConstraints::BEGIN@30 at line 4
use strict;
# spent 90µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@4 # spent 19µs making 1 call to strict::import
5389µs2172µs
# spent 105µs (38+67) within Moose::Meta::TypeConstraint::Union::BEGIN@5 which was called: # once (38µs+67µs) by Moose::Util::TypeConstraints::BEGIN@30 at line 5
use warnings;
# spent 105µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@5 # spent 67µs making 1 call to warnings::import
63138µs26.03ms
# spent 3.03ms (35µs+3.00) within Moose::Meta::TypeConstraint::Union::BEGIN@6 which was called: # once (35µs+3.00ms) by Moose::Util::TypeConstraints::BEGIN@30 at line 6
use metaclass;
# spent 3.03ms making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@6 # spent 3.00ms making 1 call to metaclass::import
7
83480µs15.48ms
# spent 5.48ms (1.60+3.88) within Moose::Meta::TypeConstraint::Union::BEGIN@8 which was called: # once (1.60ms+3.88ms) by Moose::Util::TypeConstraints::BEGIN@30 at line 8
use Moose::Meta::TypeCoercion::Union;
# spent 5.48ms making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@8
9
103210µs2359µs
# spent 201µs (43+158) within Moose::Meta::TypeConstraint::Union::BEGIN@10 which was called: # once (43µs+158µs) by Moose::Util::TypeConstraints::BEGIN@30 at line 10
use List::Util qw(first);
# spent 201µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@10 # spent 158µs making 1 call to Exporter::import
11
1216µsour $VERSION = '1.19';
13163µs$VERSION = eval $VERSION;
# spent 12µs executing statements in string eval
1414µsour $AUTHORITY = 'cpan:STEVAN';
15
1632.60ms2508µs
# spent 273µs (38+235) within Moose::Meta::TypeConstraint::Union::BEGIN@16 which was called: # once (38µs+235µs) by Moose::Util::TypeConstraints::BEGIN@30 at line 16
use base 'Moose::Meta::TypeConstraint';
# spent 273µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@16 # spent 235µs making 1 call to base::import
17
18__PACKAGE__->meta->add_attribute('type_constraints' => (
19 accessor => 'type_constraints',
20 default => sub { [] }
21138µs23.23ms));
# spent 3.11ms making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute # spent 120µs making 1 call to Moose::Meta::TypeConstraint::Union::meta
22
23sub new {
24 my ($class, %options) = @_;
25
26 my $name = join '|' => sort { $a cmp $b }
27 map { $_->name } @{ $options{type_constraints} };
28
29 my $self = $class->SUPER::new(
30 name => $name,
31 %options,
32 );
33
34 $self->_set_constraint(sub { $self->check($_[0]) });
35
36 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.
43sub 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
59sub has_coercion {
60 return defined $_[0]->coercion;
61}
62
63sub _actually_compile_type_constraint {
64 my $self = shift;
65
66 my @constraints = @{ $self->type_constraints };
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 };
75}
76
77
78sub 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
103sub parents {
104 my $self = shift;
105 $self->type_constraints;
106}
107
108sub 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
120sub find_type_for {
121 my ($self, $value) = @_;
122
123 return first { $_->check($value) } @{ $self->type_constraints };
124}
125
126sub 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
134sub 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
142sub 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
165134µs1;
166
167__END__