if ($self->should_coerce) {
$value = $type_constraint->coerce($value);
}
- defined($type_constraint->_compiled_type_constraint->($value))
+ $type_constraint->_compiled_type_constraint->($value)
|| confess "Attribute ($attr_name) does not pass the type constraint ("
. $type_constraint->name
. ") with "
my $thing = shift;
foreach my $coercion (@coercions) {
my ($constraint, $converter) = @$coercion;
- if (defined $constraint->($thing)) {
+ if ($constraint->($thing)) {
local $_ = $thing;
return $converter->($thing);
}
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
-=cut
\ No newline at end of file
+=cut
}
sub coerce { ((shift)->coercion || confess "Cannot coerce without a type coercion")->coerce(@_) }
-sub check { $_[0]->_compiled_type_constraint->($_[1]) }
+sub check { $_[0]->_compiled_type_constraint->($_[1]) ? 1 : undef }
sub validate {
my ($self, $value) = @_;
if ($self->_compiled_type_constraint->($value)) {
my $type_constraint = $self->hand_optimized_type_constraint;
- return sub {
- confess unless ref $type_constraint;
- return undef unless $type_constraint->($_[0]);
- return 1;
- };
+ confess unless ref $type_constraint;
+
+ return $type_constraint;
}
sub _compile_subtype {
my $pkg = caller();
no strict 'refs';
foreach my $constraint (keys %{$REGISTRY->type_constraints}) {
- *{"${pkg}::${constraint}"} = $REGISTRY->get_type_constraint($constraint)
- ->_compiled_type_constraint;
+ my $tc = $REGISTRY->get_type_constraint($constraint)->_compiled_type_constraint;
+ *{"${pkg}::${constraint}"} = sub { $tc->($_[0]) ? 1 : undef };
}
}
sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
-sub ScalarRef { ref($_[0]) eq 'SCALAR' }
-sub ArrayRef { ref($_[0]) eq 'ARRAY' }
-sub HashRef { ref($_[0]) eq 'HASH' }
-sub CodeRef { ref($_[0]) eq 'CODE' }
-sub RegexpRef { ref($_[0]) eq 'Regexp' }
-sub GlobRef { ref($_[0]) eq 'GLOB' }
+{
+ no warnings 'uninitialized';
+ sub ScalarRef { ref($_[0]) eq 'SCALAR' }
+ sub ArrayRef { ref($_[0]) eq 'ARRAY' }
+ sub HashRef { ref($_[0]) eq 'HASH' }
+ sub CodeRef { ref($_[0]) eq 'CODE' }
+ sub RegexpRef { ref($_[0]) eq 'Regexp' }
+ sub GlobRef { ref($_[0]) eq 'GLOB' }
+}
sub FileHandle { ref($_[0]) eq 'GLOB' && Scalar::Util::openhandle($_[0]) }