1 package Moose::Util::TypeConstraints::Builtins;
6 use List::MoreUtils ();
7 use Scalar::Util qw( blessed looks_like_number reftype );
9 sub type { goto &Moose::Util::TypeConstraints::type }
10 sub subtype { goto &Moose::Util::TypeConstraints::subtype }
11 sub as { goto &Moose::Util::TypeConstraints::as }
12 sub where (&) { goto &Moose::Util::TypeConstraints::where }
13 sub optimize_as (&) { goto &Moose::Util::TypeConstraints::optimize_as }
14 sub inline_as (&) { goto &Moose::Util::TypeConstraints::inline_as }
19 type 'Any' # meta-type including all
23 subtype 'Item' # base-type
29 => where { !defined($_) }
30 => inline_as { "! defined $_[1]" };
34 => where { defined($_) }
35 => inline_as { "defined $_[1]" };
39 => where { !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' }
40 => inline_as { qq{!defined($_[1]) || $_[1] eq "" || "$_[1]" eq '1' || "$_[1]" eq '0'} };
45 => inline_as { "defined $_[1] && ! ref $_[1]" };
50 => inline_as { "ref $_[1]" };
54 => where { ref(\$_) eq 'SCALAR' }
56 return ( qq{defined $_[1]}
57 . qq{&& ( ref(\\ $_[1] ) eq 'SCALAR'}
58 . qq{ || ref(\\(my \$str_value = $_[1])) eq 'SCALAR')} );
63 => where { Scalar::Util::looks_like_number($_) }
64 => inline_as { "!ref $_[1] && Scalar::Util::looks_like_number($_[1])" };
68 => where { "$_" =~ /\A-?[0-9]+\z/ }
70 return ( qq{defined $_[1]}
72 . qq{&& ( my \$int_value = $_[1] ) =~ /\\A-?[0-9]+\\z/} );
77 => where { ref($_) eq 'CODE' }
78 => inline_as { qq{ref $_[1] eq 'CODE'} };
82 => where( \&_RegexpRef )
83 => inline_as { "Moose::Util::TypeConstraints::Builtins::_RegexpRef( $_[1] )" };
87 => where { ref($_) eq 'GLOB' }
88 => inline_as { qq{ref $_[1] eq 'GLOB'} };
90 # NOTE: scalar filehandles are GLOB refs, but a GLOB ref is not always a
95 Scalar::Util::openhandle($_) || ( blessed($_) && $_->isa("IO::Handle") );
98 return ( qq{ref $_[1] eq 'GLOB'}
99 . qq{&& Scalar::Util::openhandle( $_[1] )}
100 . qq{or Scalar::Util::blessed( $_[1] ) && $_[1]->isa("IO::Handle")} );
105 => where { blessed($_) }
106 => inline_as { "Scalar::Util::blessed( $_[1] )" };
108 # This type is deprecated.
111 => where { $_->can('does') };
115 => where { Class::MOP::is_class_loaded($_) }
116 => inline_as { "Class::MOP::is_class_loaded( $_[1] )" };
121 (Class::MOP::class_of($_) || return)->isa('Moose::Meta::Role');
124 return ( qq{Class::MOP::is_class_loaded( $_[1] )}
125 . qq{&& ( Class::MOP::class_of( $_[1] ) || return )}
126 . qq{ ->isa('Moose::Meta::Role')} );
129 $registry->add_type_constraint(
130 Moose::Meta::TypeConstraint::Parameterizable->new(
132 package_defined_in => __PACKAGE__,
134 Moose::Util::TypeConstraints::find_type_constraint('Ref'),
135 constraint => sub { ref($_) eq 'SCALAR' || ref($_) eq 'REF' },
136 constraint_generator => sub {
137 my $type_parameter = shift;
138 my $check = $type_parameter->_compiled_type_constraint;
140 return $check->( ${$_} );
143 inlined => sub {qq{ref $_[1] eq 'SCALAR' || ref $_[1] eq 'REF'}},
144 inline_generator => sub {
146 my $type_parameter = shift;
148 return qq{(ref $val eq 'SCALAR' || ref $val eq 'REF') && }
149 . $type_parameter->_inline_check( '${ (' . $val . ') }' );
154 $registry->add_type_constraint(
155 Moose::Meta::TypeConstraint::Parameterizable->new(
157 package_defined_in => __PACKAGE__,
159 Moose::Util::TypeConstraints::find_type_constraint('Ref'),
160 constraint => sub { ref($_) eq 'ARRAY' },
161 constraint_generator => sub {
162 my $type_parameter = shift;
163 my $check = $type_parameter->_compiled_type_constraint;
165 foreach my $x (@$_) {
166 ( $check->($x) ) || return;
171 inlined => sub {qq{ref $_[1] eq 'ARRAY'}},
172 inline_generator => sub {
174 my $type_parameter = shift;
177 qq{ref $val eq 'ARRAY' && }
178 . '&List::MoreUtils::all( sub { '
179 . $type_parameter->_inline_check('$_')
185 $registry->add_type_constraint(
186 Moose::Meta::TypeConstraint::Parameterizable->new(
188 package_defined_in => __PACKAGE__,
190 Moose::Util::TypeConstraints::find_type_constraint('Ref'),
191 constraint => sub { ref($_) eq 'HASH' },
192 constraint_generator => sub {
193 my $type_parameter = shift;
194 my $check = $type_parameter->_compiled_type_constraint;
196 foreach my $x ( values %$_ ) {
197 ( $check->($x) ) || return;
202 inlined => sub {qq{ref $_[1] eq 'HASH'}},
203 inline_generator => sub {
205 my $type_parameter = shift;
208 qq{ref $val eq 'HASH' && }
209 . '&List::MoreUtils::all( sub { '
210 . $type_parameter->_inline_check('$_')
211 . " }, values \%{$val} )";
216 $registry->add_type_constraint(
217 Moose::Meta::TypeConstraint::Parameterizable->new(
219 package_defined_in => __PACKAGE__,
221 Moose::Util::TypeConstraints::find_type_constraint('Item'),
222 constraint => sub {1},
223 constraint_generator => sub {
224 my $type_parameter = shift;
225 my $check = $type_parameter->_compiled_type_constraint;
227 return 1 if not( defined($_) ) || $check->($_);
231 inlined => sub {'1'},
232 inline_generator => sub {
234 my $type_parameter = shift;
237 "(! defined $val) || ("
238 . $type_parameter->_inline_check($val) . ')';