| File | /usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Moose/Meta/TypeConstraint.pm |
| Statements Executed | 603 |
| Statement Execution Time | 2.41ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 12.2ms | 12.5ms | Moose::Meta::TypeConstraint::BEGIN@6 |
| 22 | 6 | 2 | 362µs | 1.44ms | Moose::Meta::TypeConstraint::new |
| 74 | 4 | 2 | 218µs | 312µs | Moose::Meta::TypeConstraint::__ANON__[:8] |
| 23 | 1 | 1 | 168µs | 560µs | Moose::Meta::TypeConstraint::_actually_compile_type_constraint |
| 5 | 1 | 1 | 133µs | 238µs | Moose::Meta::TypeConstraint::_compile_subtype |
| 23 | 2 | 2 | 123µs | 730µs | Moose::Meta::TypeConstraint::compile_type_constraint |
| 17 | 1 | 1 | 105µs | 1.09ms | Moose::Meta::TypeConstraint::create_child_type |
| 17 | 1 | 1 | 68µs | 91µs | Moose::Meta::TypeConstraint::_compile_hand_optimized_type_constraint |
| 5 | 1 | 1 | 49µs | 66µs | Moose::Meta::TypeConstraint::_collect_all_parents |
| 1 | 1 | 1 | 15µs | 19µs | Moose::Meta::TypeConstraint::BEGIN@4 |
| 1 | 1 | 1 | 13µs | 17µs | Moose::Meta::TypeConstraint::_compile_type |
| 1 | 1 | 1 | 11µs | 52µs | Moose::Meta::TypeConstraint::BEGIN@8 |
| 1 | 1 | 1 | 8µs | 19µs | Moose::Meta::TypeConstraint::BEGIN@5 |
| 1 | 1 | 1 | 8µs | 43µs | Moose::Meta::TypeConstraint::BEGIN@11 |
| 1 | 1 | 1 | 7µs | 61µs | Moose::Meta::TypeConstraint::BEGIN@14 |
| 1 | 1 | 1 | 7µs | 32µs | Moose::Meta::TypeConstraint::BEGIN@12 |
| 2 | 2 | 2 | 5µs | 5µs | Moose::Meta::TypeConstraint::__ANON__[:30] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::__ANON__[:253] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::__ANON__[:266] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::__ANON__[:26] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::__ANON__[:279] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::assert_valid |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::check |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::coerce |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::equals |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::get_message |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::is_a_type_of |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::is_subtype_of |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::parents |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::validate |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | |||||
| 2 | package Moose::Meta::TypeConstraint; | ||||
| 3 | |||||
| 4 | 3 | 22µs | 2 | 22µs | # spent 19µs (15+4) within Moose::Meta::TypeConstraint::BEGIN@4 which was called
# once (15µs+4µs) by Moose::BEGIN@19 at line 4 # spent 19µs making 1 call to Moose::Meta::TypeConstraint::BEGIN@4
# spent 4µs making 1 call to strict::import |
| 5 | 3 | 20µs | 2 | 30µs | # spent 19µs (8+11) within Moose::Meta::TypeConstraint::BEGIN@5 which was called
# once (8µs+11µs) by Moose::BEGIN@19 at line 5 # spent 19µs making 1 call to Moose::Meta::TypeConstraint::BEGIN@5
# spent 11µs making 1 call to warnings::import |
| 6 | 3 | 139µs | 2 | 12.6ms | # spent 12.5ms (12.2+249µs) within Moose::Meta::TypeConstraint::BEGIN@6 which was called
# once (12.2ms+249µs) by Moose::BEGIN@19 at line 6 # spent 12.5ms making 1 call to Moose::Meta::TypeConstraint::BEGIN@6
# spent 141µs making 1 call to metaclass::import |
| 7 | |||||
| 8 | 74 | 186µs | 74 | 95µs | # spent 52µs (11+41) within Moose::Meta::TypeConstraint::BEGIN@8 which was called
# once (11µs+41µs) by Moose::BEGIN@19 at line 9
# spent 312µs (218+95) within Moose::Meta::TypeConstraint::__ANON__[/usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Moose/Meta/TypeConstraint.pm:8] which was called 74 times, avg 4µs/call:
# 23 times (74µs+31µs) by Moose::Meta::TypeConstraint::Registry::add_type_constraint at line 47 of Moose/Meta/TypeConstraint/Registry.pm, avg 5µs/call
# 17 times (55µs+24µs) by Moose::Util::TypeConstraints::find_or_parse_type_constraint at line 217 of Moose/Util/TypeConstraints.pm, avg 5µs/call
# 17 times (45µs+20µs) by Moose::Util::TypeConstraints::find_or_create_isa_type_constraint at line 203 of Moose/Util/TypeConstraints.pm, avg 4µs/call
# 17 times (44µs+19µs) by Moose::Util::TypeConstraints::_create_type_constraint at line 523 of Moose/Util/TypeConstraints.pm, avg 4µs/call # spent 95µs making 74 calls to Moose::Meta::TypeConstraint::name, avg 1µs/call |
| 9 | 3 | 27µs | 2 | 93µs | fallback => 1; # spent 52µs making 1 call to Moose::Meta::TypeConstraint::BEGIN@8
# spent 41µs making 1 call to overload::import |
| 10 | |||||
| 11 | 3 | 26µs | 2 | 78µs | # spent 43µs (8+35) within Moose::Meta::TypeConstraint::BEGIN@11 which was called
# once (8µs+35µs) by Moose::BEGIN@19 at line 11 # spent 43µs making 1 call to Moose::Meta::TypeConstraint::BEGIN@11
# spent 35µs making 1 call to Exporter::import |
| 12 | 3 | 25µs | 2 | 56µs | # spent 32µs (7+24) within Moose::Meta::TypeConstraint::BEGIN@12 which was called
# once (7µs+24µs) by Moose::BEGIN@19 at line 12 # spent 32µs making 1 call to Moose::Meta::TypeConstraint::BEGIN@12
# spent 24µs making 1 call to Exporter::import |
| 13 | |||||
| 14 | 3 | 1.08ms | 2 | 114µs | # spent 61µs (7+53) within Moose::Meta::TypeConstraint::BEGIN@14 which was called
# once (7µs+53µs) by Moose::BEGIN@19 at line 14 # spent 61µs making 1 call to Moose::Meta::TypeConstraint::BEGIN@14
# spent 53µs making 1 call to base::import |
| 15 | |||||
| 16 | 1 | 700ns | our $VERSION = '0.98'; | ||
| 17 | 1 | 14µs | $VERSION = eval $VERSION; | ||
| 18 | 1 | 300ns | our $AUTHORITY = 'cpan:STEVAN'; | ||
| 19 | |||||
| 20 | 1 | 4µs | 2 | 439µs | __PACKAGE__->meta->add_attribute('name' => (reader => 'name')); # spent 422µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 18µs making 1 call to Moose::Meta::TypeConstraint::meta |
| 21 | 1 | 2µs | 2 | 402µs | __PACKAGE__->meta->add_attribute('parent' => ( # spent 391µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 11µs making 1 call to Moose::Meta::TypeConstraint::meta |
| 22 | reader => 'parent', | ||||
| 23 | predicate => 'has_parent', | ||||
| 24 | )); | ||||
| 25 | |||||
| 26 | 1 | 1µs | my $null_constraint = sub { 1 }; | ||
| 27 | __PACKAGE__->meta->add_attribute('constraint' => ( | ||||
| 28 | reader => 'constraint', | ||||
| 29 | writer => '_set_constraint', | ||||
| 30 | 2 | 9µs | # spent 5µs within Moose::Meta::TypeConstraint::__ANON__[/usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Moose/Meta/TypeConstraint.pm:30] which was called 2 times, avg 2µs/call:
# once (3µs+0s) by Moose::Meta::TypeConstraint::Class::_new at line 35 of (eval 0)[Class/MOP/Method/Generated.pm:54] at line 54 of Class/MOP/Method/Generated.pm
# once (2µs+0s) by Moose::Meta::TypeConstraint::_new at line 26 of (eval 0)[Class/MOP/Method/Generated.pm:54] at line 54 of Class/MOP/Method/Generated.pm | ||
| 31 | 1 | 4µs | 2 | 421µs | )); # spent 410µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 11µs making 1 call to Moose::Meta::TypeConstraint::meta |
| 32 | 1 | 2µs | 2 | 407µs | __PACKAGE__->meta->add_attribute('message' => ( # spent 396µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 11µs making 1 call to Moose::Meta::TypeConstraint::meta |
| 33 | accessor => 'message', | ||||
| 34 | predicate => 'has_message' | ||||
| 35 | )); | ||||
| 36 | 1 | 2µs | 2 | 388µs | __PACKAGE__->meta->add_attribute('coercion' => ( # spent 378µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 11µs making 1 call to Moose::Meta::TypeConstraint::meta |
| 37 | accessor => 'coercion', | ||||
| 38 | predicate => 'has_coercion' | ||||
| 39 | )); | ||||
| 40 | |||||
| 41 | 1 | 2µs | 2 | 422µs | __PACKAGE__->meta->add_attribute('hand_optimized_type_constraint' => ( # spent 412µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 11µs making 1 call to Moose::Meta::TypeConstraint::meta |
| 42 | init_arg => 'optimized', | ||||
| 43 | accessor => 'hand_optimized_type_constraint', | ||||
| 44 | predicate => 'has_hand_optimized_type_constraint', | ||||
| 45 | )); | ||||
| 46 | |||||
| 47 | sub parents { | ||||
| 48 | my $self; | ||||
| 49 | $self->parent; | ||||
| 50 | } | ||||
| 51 | |||||
| 52 | # private accessors | ||||
| 53 | |||||
| 54 | 1 | 2µs | 2 | 388µs | __PACKAGE__->meta->add_attribute('compiled_type_constraint' => ( # spent 377µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 11µs making 1 call to Moose::Meta::TypeConstraint::meta |
| 55 | accessor => '_compiled_type_constraint', | ||||
| 56 | predicate => '_has_compiled_type_constraint' | ||||
| 57 | )); | ||||
| 58 | 1 | 2µs | 2 | 280µs | __PACKAGE__->meta->add_attribute('package_defined_in' => ( # spent 269µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 11µs making 1 call to Moose::Meta::TypeConstraint::meta |
| 59 | accessor => '_package_defined_in' | ||||
| 60 | )); | ||||
| 61 | |||||
| 62 | # spent 1.44ms (362µs+1.08) within Moose::Meta::TypeConstraint::new which was called 22 times, avg 66µs/call:
# 17 times (257µs+728µs) by Moose::Meta::TypeConstraint::create_child_type at line 298, avg 58µs/call
# once (29µs+116µs) by Moose::Util::TypeConstraints::_create_type_constraint at line 533 of Moose/Util/TypeConstraints.pm
# once (26µs+106µs) by Moose::Meta::Attribute::BEGIN@18 at line 731 of Moose/Util/TypeConstraints.pm
# once (15µs+78µs) by Moose::Meta::Attribute::BEGIN@18 at line 790 of Moose/Util/TypeConstraints.pm
# once (21µs+28µs) by Moose::Meta::Attribute::BEGIN@18 at line 752 of Moose/Util/TypeConstraints.pm
# once (14µs+26µs) by Moose::Meta::Attribute::BEGIN@18 at line 773 of Moose/Util/TypeConstraints.pm | ||||
| 63 | 154 | 293µs | my $class = shift; | ||
| 64 | my ($first, @rest) = @_; | ||||
| 65 | my %args = ref $first ? %$first : $first ? ($first, @rest) : (); | ||||
| 66 | $args{name} = $args{name} ? "$args{name}" : "__ANON__"; | ||||
| 67 | |||||
| 68 | my $self = $class->_new(%args); # spent 250µs making 18 calls to Moose::Meta::TypeConstraint::_new, avg 14µs/call
# spent 107µs making 4 calls to Moose::Meta::TypeConstraint::Parameterizable::_new, avg 27µs/call | ||||
| 69 | $self->compile_type_constraint() # spent 690µs making 22 calls to Moose::Meta::TypeConstraint::compile_type_constraint, avg 31µs/call
# spent 36µs making 22 calls to Moose::Meta::TypeConstraint::_has_compiled_type_constraint, avg 2µs/call | ||||
| 70 | unless $self->_has_compiled_type_constraint; | ||||
| 71 | return $self; | ||||
| 72 | } | ||||
| 73 | |||||
| 74 | |||||
| 75 | |||||
| 76 | sub coerce { | ||||
| 77 | my $self = shift; | ||||
| 78 | |||||
| 79 | my $coercion = $self->coercion; | ||||
| 80 | |||||
| 81 | unless ($coercion) { | ||||
| 82 | require Moose; | ||||
| 83 | Moose->throw_error("Cannot coerce without a type coercion"); | ||||
| 84 | } | ||||
| 85 | |||||
| 86 | return $_[0] if $self->check($_[0]); | ||||
| 87 | |||||
| 88 | return $coercion->coerce(@_); | ||||
| 89 | } | ||||
| 90 | |||||
| 91 | sub check { | ||||
| 92 | my ($self, @args) = @_; | ||||
| 93 | my $constraint_subref = $self->_compiled_type_constraint; | ||||
| 94 | return $constraint_subref->(@args) ? 1 : undef; | ||||
| 95 | } | ||||
| 96 | |||||
| 97 | sub validate { | ||||
| 98 | my ($self, $value) = @_; | ||||
| 99 | if ($self->_compiled_type_constraint->($value)) { | ||||
| 100 | return undef; | ||||
| 101 | } | ||||
| 102 | else { | ||||
| 103 | $self->get_message($value); | ||||
| 104 | } | ||||
| 105 | } | ||||
| 106 | |||||
| 107 | sub assert_valid { | ||||
| 108 | my ($self, $value) = @_; | ||||
| 109 | |||||
| 110 | my $error = $self->validate($value); | ||||
| 111 | return 1 if ! defined $error; | ||||
| 112 | |||||
| 113 | require Moose; | ||||
| 114 | Moose->throw_error($error); | ||||
| 115 | } | ||||
| 116 | |||||
| 117 | sub get_message { | ||||
| 118 | my ($self, $value) = @_; | ||||
| 119 | if (my $msg = $self->message) { | ||||
| 120 | local $_ = $value; | ||||
| 121 | return $msg->($value); | ||||
| 122 | } | ||||
| 123 | else { | ||||
| 124 | $value = (defined $value ? overload::StrVal($value) : 'undef'); | ||||
| 125 | return "Validation failed for '" . $self->name . "' failed with value $value"; | ||||
| 126 | } | ||||
| 127 | } | ||||
| 128 | |||||
| 129 | ## type predicates ... | ||||
| 130 | |||||
| 131 | sub equals { | ||||
| 132 | my ( $self, $type_or_name ) = @_; | ||||
| 133 | |||||
| 134 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name) or return; | ||||
| 135 | |||||
| 136 | return 1 if refaddr($self) == refaddr($other); | ||||
| 137 | |||||
| 138 | if ( $self->has_hand_optimized_type_constraint and $other->has_hand_optimized_type_constraint ) { | ||||
| 139 | return 1 if $self->hand_optimized_type_constraint == $other->hand_optimized_type_constraint; | ||||
| 140 | } | ||||
| 141 | |||||
| 142 | return unless $self->constraint == $other->constraint; | ||||
| 143 | |||||
| 144 | if ( $self->has_parent ) { | ||||
| 145 | return unless $other->has_parent; | ||||
| 146 | return unless $self->parent->equals( $other->parent ); | ||||
| 147 | } else { | ||||
| 148 | return if $other->has_parent; | ||||
| 149 | } | ||||
| 150 | |||||
| 151 | return 1; | ||||
| 152 | } | ||||
| 153 | |||||
| 154 | sub is_a_type_of { | ||||
| 155 | my ($self, $type_or_name) = @_; | ||||
| 156 | |||||
| 157 | my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name) or return; | ||||
| 158 | |||||
| 159 | ($self->equals($type) || $self->is_subtype_of($type)); | ||||
| 160 | } | ||||
| 161 | |||||
| 162 | sub is_subtype_of { | ||||
| 163 | my ($self, $type_or_name) = @_; | ||||
| 164 | |||||
| 165 | my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name) or return; | ||||
| 166 | |||||
| 167 | my $current = $self; | ||||
| 168 | |||||
| 169 | while (my $parent = $current->parent) { | ||||
| 170 | return 1 if $parent->equals($type); | ||||
| 171 | $current = $parent; | ||||
| 172 | } | ||||
| 173 | |||||
| 174 | return 0; | ||||
| 175 | } | ||||
| 176 | |||||
| 177 | ## compiling the type constraint | ||||
| 178 | |||||
| 179 | # spent 730µs (123+607) within Moose::Meta::TypeConstraint::compile_type_constraint which was called 23 times, avg 32µs/call:
# 22 times (110µs+579µs) by Moose::Meta::TypeConstraint::new at line 69, avg 31µs/call
# once (13µs+27µs) by Moose::Meta::TypeConstraint::Class::new at line 27 of Moose/Meta/TypeConstraint/Class.pm | ||||
| 180 | 46 | 93µs | my $self = shift; | ||
| 181 | $self->_compiled_type_constraint($self->_actually_compile_type_constraint); # spent 560µs making 23 calls to Moose::Meta::TypeConstraint::_actually_compile_type_constraint, avg 24µs/call
# spent 46µs making 23 calls to Moose::Meta::TypeConstraint::_compiled_type_constraint, avg 2µs/call | ||||
| 182 | } | ||||
| 183 | |||||
| 184 | ## type compilers ... | ||||
| 185 | |||||
| 186 | # spent 560µs (168+392) within Moose::Meta::TypeConstraint::_actually_compile_type_constraint which was called 23 times, avg 24µs/call:
# 23 times (168µs+392µs) by Moose::Meta::TypeConstraint::compile_type_constraint at line 181, avg 24µs/call | ||||
| 187 | 65 | 115µs | my $self = shift; | ||
| 188 | |||||
| 189 | return $self->_compile_hand_optimized_type_constraint # spent 91µs making 17 calls to Moose::Meta::TypeConstraint::_compile_hand_optimized_type_constraint, avg 5µs/call
# spent 29µs making 23 calls to Moose::Meta::TypeConstraint::has_hand_optimized_type_constraint, avg 1µs/call | ||||
| 190 | if $self->has_hand_optimized_type_constraint; | ||||
| 191 | |||||
| 192 | my $check = $self->constraint; # spent 9µs making 6 calls to Moose::Meta::TypeConstraint::constraint, avg 1µs/call | ||||
| 193 | unless ( defined $check ) { | ||||
| 194 | require Moose; | ||||
| 195 | Moose->throw_error( "Could not compile type constraint '" | ||||
| 196 | . $self->name | ||||
| 197 | . "' because no constraint check" ); | ||||
| 198 | } | ||||
| 199 | |||||
| 200 | return $self->_compile_subtype($check) # spent 238µs making 5 calls to Moose::Meta::TypeConstraint::_compile_subtype, avg 48µs/call
# spent 8µs making 6 calls to Moose::Meta::TypeConstraint::has_parent, avg 1µs/call | ||||
| 201 | if $self->has_parent; | ||||
| 202 | |||||
| 203 | return $self->_compile_type($check); # spent 17µs making 1 call to Moose::Meta::TypeConstraint::_compile_type | ||||
| 204 | } | ||||
| 205 | |||||
| 206 | # spent 91µs (68+23) within Moose::Meta::TypeConstraint::_compile_hand_optimized_type_constraint which was called 17 times, avg 5µs/call:
# 17 times (68µs+23µs) by Moose::Meta::TypeConstraint::_actually_compile_type_constraint at line 189, avg 5µs/call | ||||
| 207 | 68 | 63µs | my $self = shift; | ||
| 208 | |||||
| 209 | my $type_constraint = $self->hand_optimized_type_constraint; # spent 24µs making 17 calls to Moose::Meta::TypeConstraint::hand_optimized_type_constraint, avg 1µs/call | ||||
| 210 | |||||
| 211 | unless ( ref $type_constraint ) { | ||||
| 212 | require Moose; | ||||
| 213 | Carp::confess ("Hand optimized type constraint for " . $self->name . " is not a code reference"); | ||||
| 214 | Moose->throw_error("Hand optimized type constraint is not a code reference"); | ||||
| 215 | } | ||||
| 216 | |||||
| 217 | return $type_constraint; | ||||
| 218 | } | ||||
| 219 | |||||
| 220 | # spent 238µs (133+105) within Moose::Meta::TypeConstraint::_compile_subtype which was called 5 times, avg 48µs/call:
# 5 times (133µs+105µs) by Moose::Meta::TypeConstraint::_actually_compile_type_constraint at line 200, avg 48µs/call | ||||
| 221 | 30 | 24µs | my ($self, $check) = @_; | ||
| 222 | |||||
| 223 | # gather all the parent constraintss in order | ||||
| 224 | my @parents; | ||||
| 225 | my $optimized_parent; | ||||
| 226 | foreach my $parent ($self->_collect_all_parents) { # spent 66µs making 5 calls to Moose::Meta::TypeConstraint::_collect_all_parents, avg 13µs/call | ||||
| 227 | # if a parent is optimized, the optimized constraint already includes | ||||
| 228 | # all of its parents tcs, so we can break the loop | ||||
| 229 | 18 | 24µs | 9 | 11µs | if ($parent->has_hand_optimized_type_constraint) { # spent 11µs making 9 calls to Moose::Meta::TypeConstraint::has_hand_optimized_type_constraint, avg 1µs/call |
| 230 | push @parents => $optimized_parent = $parent->hand_optimized_type_constraint; | ||||
| 231 | last; | ||||
| 232 | } | ||||
| 233 | else { | ||||
| 234 | push @parents => $parent->constraint; # spent 10µs making 9 calls to Moose::Meta::TypeConstraint::constraint, avg 1µs/call | ||||
| 235 | } | ||||
| 236 | } | ||||
| 237 | |||||
| 238 | @parents = grep { $_ != $null_constraint } reverse @parents; | ||||
| 239 | |||||
| 240 | 15 | 57µs | unless ( @parents ) { | ||
| 241 | return $self->_compile_type($check); | ||||
| 242 | } elsif( $optimized_parent and @parents == 1 ) { | ||||
| 243 | # the case of just one optimized parent is optimized to prevent | ||||
| 244 | # looping and the unnecessary localization | ||||
| 245 | if ( $check == $null_constraint ) { | ||||
| 246 | return $optimized_parent; | ||||
| 247 | } else { | ||||
| 248 | return subname($self->name, sub { | ||||
| 249 | return undef unless $optimized_parent->($_[0]); | ||||
| 250 | my (@args) = @_; | ||||
| 251 | local $_ = $args[0]; | ||||
| 252 | $check->(@args); | ||||
| 253 | }); | ||||
| 254 | } | ||||
| 255 | } else { | ||||
| 256 | # general case, check all the constraints, from the first parent to ourselves | ||||
| 257 | my @checks = @parents; | ||||
| 258 | push @checks, $check if $check != $null_constraint; | ||||
| 259 | return subname($self->name => sub { | ||||
| 260 | my (@args) = @_; | ||||
| 261 | local $_ = $args[0]; | ||||
| 262 | foreach my $check (@checks) { | ||||
| 263 | return undef unless $check->(@args); | ||||
| 264 | } | ||||
| 265 | return 1; | ||||
| 266 | }); # spent 10µs making 5 calls to Sub::Name::subname, avg 2µs/call
# spent 7µs making 5 calls to Moose::Meta::TypeConstraint::name, avg 1µs/call | ||||
| 267 | } | ||||
| 268 | } | ||||
| 269 | |||||
| 270 | # spent 17µs (13+4) within Moose::Meta::TypeConstraint::_compile_type which was called
# once (13µs+4µs) by Moose::Meta::TypeConstraint::_actually_compile_type_constraint at line 203 | ||||
| 271 | 3 | 13µs | my ($self, $check) = @_; | ||
| 272 | |||||
| 273 | return $check if $check == $null_constraint; # Item, Any | ||||
| 274 | |||||
| 275 | return subname($self->name => sub { | ||||
| 276 | my (@args) = @_; | ||||
| 277 | local $_ = $args[0]; | ||||
| 278 | $check->(@args); | ||||
| 279 | }); # spent 2µs making 1 call to Moose::Meta::TypeConstraint::name
# spent 2µs making 1 call to Sub::Name::subname | ||||
| 280 | } | ||||
| 281 | |||||
| 282 | ## other utils ... | ||||
| 283 | |||||
| 284 | # spent 66µs (49+17) within Moose::Meta::TypeConstraint::_collect_all_parents which was called 5 times, avg 13µs/call:
# 5 times (49µs+17µs) by Moose::Meta::TypeConstraint::_compile_subtype at line 226, avg 13µs/call | ||||
| 285 | 25 | 21µs | my $self = shift; | ||
| 286 | my @parents; | ||||
| 287 | my $current = $self->parent; # spent 8µs making 5 calls to Moose::Meta::TypeConstraint::parent, avg 2µs/call | ||||
| 288 | while (defined $current) { | ||||
| 289 | 18 | 12µs | push @parents => $current; | ||
| 290 | $current = $current->parent; # spent 10µs making 9 calls to Moose::Meta::TypeConstraint::parent, avg 1µs/call | ||||
| 291 | } | ||||
| 292 | return @parents; | ||||
| 293 | } | ||||
| 294 | |||||
| 295 | # spent 1.09ms (105µs+985µs) within Moose::Meta::TypeConstraint::create_child_type which was called 17 times, avg 64µs/call:
# 17 times (105µs+985µs) by Moose::Util::TypeConstraints::_create_type_constraint at line 523 of Moose/Util/TypeConstraints.pm, avg 64µs/call | ||||
| 296 | 51 | 98µs | my ($self, %opts) = @_; | ||
| 297 | my $class = ref $self; | ||||
| 298 | return $class->new(%opts, parent => $self); # spent 985µs making 17 calls to Moose::Meta::TypeConstraint::new, avg 58µs/call | ||||
| 299 | } | ||||
| 300 | |||||
| 301 | 1 | 26µs | 1; | ||
| 302 | |||||
| 303 | __END__ | ||||
| 304 | |||||
| 305 | =pod | ||||
| 306 | |||||
| 307 | =head1 NAME | ||||
| 308 | |||||
| 309 | Moose::Meta::TypeConstraint - The Moose Type Constraint metaclass | ||||
| 310 | |||||
| 311 | =head1 DESCRIPTION | ||||
| 312 | |||||
| 313 | This class represents a single type constraint. Moose's built-in type | ||||
| 314 | constraints, as well as constraints you define, are all stored in a | ||||
| 315 | L<Moose::Meta::TypeConstraint::Registry> object as objects of this | ||||
| 316 | class. | ||||
| 317 | |||||
| 318 | =head1 INHERITANCE | ||||
| 319 | |||||
| 320 | C<Moose::Meta::TypeConstraint> is a subclass of L<Class::MOP::Object>. | ||||
| 321 | |||||
| 322 | =head1 METHODS | ||||
| 323 | |||||
| 324 | =over 4 | ||||
| 325 | |||||
| 326 | =item B<< Moose::Meta::TypeConstraint->new(%options) >> | ||||
| 327 | |||||
| 328 | This creates a new type constraint based on the provided C<%options>: | ||||
| 329 | |||||
| 330 | =over 8 | ||||
| 331 | |||||
| 332 | =item * name | ||||
| 333 | |||||
| 334 | The constraint name. If a name is not provided, it will be set to | ||||
| 335 | "__ANON__". | ||||
| 336 | |||||
| 337 | =item * parent | ||||
| 338 | |||||
| 339 | A C<Moose::Meta::TypeConstraint> object which is the parent type for | ||||
| 340 | the type being created. This is optional. | ||||
| 341 | |||||
| 342 | =item * constraint | ||||
| 343 | |||||
| 344 | This is the subroutine reference that implements the actual constraint | ||||
| 345 | check. This defaults to a subroutine which always returns true. | ||||
| 346 | |||||
| 347 | =item * message | ||||
| 348 | |||||
| 349 | A subroutine reference which is used to generate an error message when | ||||
| 350 | the constraint fails. This is optional. | ||||
| 351 | |||||
| 352 | =item * coercion | ||||
| 353 | |||||
| 354 | A L<Moose::Meta::TypeCoercion> object representing the coercions to | ||||
| 355 | the type. This is optional. | ||||
| 356 | |||||
| 357 | =item * optimized | ||||
| 358 | |||||
| 359 | This is a variant of the C<constraint> parameter that is somehow | ||||
| 360 | optimized. Typically, this means incorporating both the type's | ||||
| 361 | constraint and all of its parents' constraints into a single | ||||
| 362 | subroutine reference. | ||||
| 363 | |||||
| 364 | =back | ||||
| 365 | |||||
| 366 | =item B<< $constraint->equals($type_name_or_object) >> | ||||
| 367 | |||||
| 368 | Returns true if the supplied name or type object is the same as the | ||||
| 369 | current type. | ||||
| 370 | |||||
| 371 | =item B<< $constraint->is_subtype_of($type_name_or_object) >> | ||||
| 372 | |||||
| 373 | Returns true if the supplied name or type object is a parent of the | ||||
| 374 | current type. | ||||
| 375 | |||||
| 376 | =item B<< $constraint->is_a_type_of($type_name_or_object) >> | ||||
| 377 | |||||
| 378 | Returns true if the given type is the same as the current type, or is | ||||
| 379 | a parent of the current type. This is a shortcut for checking | ||||
| 380 | C<equals> and C<is_subtype_of>. | ||||
| 381 | |||||
| 382 | =item B<< $constraint->coerce($value) >> | ||||
| 383 | |||||
| 384 | This will attempt to coerce the value to the type. If the type does | ||||
| 385 | have any defined coercions this will throw an error. | ||||
| 386 | |||||
| 387 | =item B<< $constraint->check($value) >> | ||||
| 388 | |||||
| 389 | Returns true if the given value passes the constraint for the type. | ||||
| 390 | |||||
| 391 | =item B<< $constraint->validate($value) >> | ||||
| 392 | |||||
| 393 | This is similar to C<check>. However, if the type I<is valid> then the | ||||
| 394 | method returns an explicit C<undef>. If the type is not valid, we call | ||||
| 395 | C<< $self->get_message($value) >> internally to generate an error | ||||
| 396 | message. | ||||
| 397 | |||||
| 398 | =item B<< $constraint->assert_valid($value) >> | ||||
| 399 | |||||
| 400 | Like C<check> and C<validate>, this method checks whether C<$value> is | ||||
| 401 | valid under the constraint. If it is, it will return true. If it is not, | ||||
| 402 | an exception will be thrown with the results of | ||||
| 403 | C<< $self->get_message($value) >>. | ||||
| 404 | |||||
| 405 | =item B<< $constraint->name >> | ||||
| 406 | |||||
| 407 | Returns the type's name, as provided to the constructor. | ||||
| 408 | |||||
| 409 | =item B<< $constraint->parent >> | ||||
| 410 | |||||
| 411 | Returns the type's parent, as provided to the constructor, if any. | ||||
| 412 | |||||
| 413 | =item B<< $constraint->has_parent >> | ||||
| 414 | |||||
| 415 | Returns true if the type has a parent type. | ||||
| 416 | |||||
| 417 | =item B<< $constraint->parents >> | ||||
| 418 | |||||
| 419 | A synonym for C<parent>. This is useful for polymorphism with types | ||||
| 420 | that can have more than one parent. | ||||
| 421 | |||||
| 422 | =item B<< $constraint->constraint >> | ||||
| 423 | |||||
| 424 | Returns the type's constraint, as provided to the constructor. | ||||
| 425 | |||||
| 426 | =item B<< $constraint->get_message($value) >> | ||||
| 427 | |||||
| 428 | This generates a method for the given value. If the type does not have | ||||
| 429 | an explicit message, we generate a default message. | ||||
| 430 | |||||
| 431 | =item B<< $constraint->has_message >> | ||||
| 432 | |||||
| 433 | Returns true if the type has a message. | ||||
| 434 | |||||
| 435 | =item B<< $constraint->message >> | ||||
| 436 | |||||
| 437 | Returns the type's message as a subroutine reference. | ||||
| 438 | |||||
| 439 | =item B<< $constraint->coercion >> | ||||
| 440 | |||||
| 441 | Returns the type's L<Moose::Meta::TypeCoercion> object, if one | ||||
| 442 | exists. | ||||
| 443 | |||||
| 444 | =item B<< $constraint->has_coercion >> | ||||
| 445 | |||||
| 446 | Returns true if the type has a coercion. | ||||
| 447 | |||||
| 448 | =item B<< $constraint->hand_optimized_type_constraint >> | ||||
| 449 | |||||
| 450 | Returns the type's hand optimized constraint, as provided to the | ||||
| 451 | constructor via the C<optimized> option. | ||||
| 452 | |||||
| 453 | =item B<< $constraint->has_hand_optimized_type_constraint >> | ||||
| 454 | |||||
| 455 | Returns true if the type has an optimized constraint. | ||||
| 456 | |||||
| 457 | =item B<< $constraint->create_child_type(%options) >> | ||||
| 458 | |||||
| 459 | This returns a new type constraint of the same class using the | ||||
| 460 | provided C<%options>. The C<parent> option will be the current type. | ||||
| 461 | |||||
| 462 | This method exists so that subclasses of this class can override this | ||||
| 463 | behavior and change how child types are created. | ||||
| 464 | |||||
| 465 | =back | ||||
| 466 | |||||
| 467 | =head1 BUGS | ||||
| 468 | |||||
| 469 | See L<Moose/BUGS> for details on reporting bugs. | ||||
| 470 | |||||
| 471 | =head1 AUTHOR | ||||
| 472 | |||||
| 473 | Stevan Little E<lt>stevan@iinteractive.comE<gt> | ||||
| 474 | |||||
| 475 | =head1 COPYRIGHT AND LICENSE | ||||
| 476 | |||||
| 477 | Copyright 2006-2010 by Infinity Interactive, Inc. | ||||
| 478 | |||||
| 479 | L<http://www.iinteractive.com> | ||||
| 480 | |||||
| 481 | This library is free software; you can redistribute it and/or modify | ||||
| 482 | it under the same terms as Perl itself. | ||||
| 483 | |||||
| 484 | =cut |