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' }
41 '!defined(' . $_[1] . ') '
42 . '|| ' . $_[1] . ' eq "" '
43 . '|| "' . $_[1] . '" eq "1" '
44 . '|| "' . $_[1] . '" eq "0"'
50 => inline_as { 'defined(' . $_[1] . ') && !ref(' . $_[1] . ')' };
55 => inline_as { 'ref(' . $_[1] . ')' };
59 => where { ref(\$_) eq 'SCALAR' }
61 'defined(' . $_[1] . ') '
62 . '&& (ref(\\' . $_[1] . ') eq "SCALAR"'
63 . '|| ref(\\(my $val = ' . $_[1] . ')) eq "SCALAR")'
68 => where { Scalar::Util::looks_like_number($_) }
70 '!ref(' . $_[1] . ') '
71 . '&& Scalar::Util::looks_like_number(' . $_[1] . ')'
76 => where { "$_" =~ /\A-?[0-9]+\z/ }
78 'defined(' . $_[1] . ') '
79 . '&& !ref(' . $_[1] . ') '
80 . '&& (my $val = ' . $_[1] . ') =~ /\A-?[0-9]+\z/'
85 => where { ref($_) eq 'CODE' }
86 => inline_as { 'ref(' . $_[1] . ') eq "CODE"' };
90 => where( \&_RegexpRef )
92 'Moose::Util::TypeConstraints::Builtins::_RegexpRef(' . $_[1] . ')'
97 => where { ref($_) eq 'GLOB' }
98 => inline_as { 'ref(' . $_[1] . ') eq "GLOB"' };
100 # NOTE: scalar filehandles are GLOB refs, but a GLOB ref is not always a
105 Scalar::Util::openhandle($_) || ( blessed($_) && $_->isa("IO::Handle") );
108 '(ref(' . $_[1] . ') eq "GLOB" '
109 . '&& Scalar::Util::openhandle(' . $_[1] . ')) '
110 . '|| (Scalar::Util::blessed(' . $_[1] . ') '
111 . '&& ' . $_[1] . '->isa("IO::Handle"))'
116 => where { blessed($_) }
117 => inline_as { 'Scalar::Util::blessed(' . $_[1] . ')' };
119 # This type is deprecated.
122 => where { $_->can('does') };
126 => where { Class::MOP::is_class_loaded($_) }
127 => inline_as { 'Class::MOP::is_class_loaded(' . $_[1] . ')' };
132 (Class::MOP::class_of($_) || return)->isa('Moose::Meta::Role');
135 'Class::MOP::is_class_loaded(' . $_[1] . ') '
136 . '&& (Class::MOP::class_of(' . $_[1] . ') || return)->isa('
137 . '"Moose::Meta::Role"'
141 $registry->add_type_constraint(
142 Moose::Meta::TypeConstraint::Parameterizable->new(
144 package_defined_in => __PACKAGE__,
146 Moose::Util::TypeConstraints::find_type_constraint('Ref'),
147 constraint => sub { ref($_) eq 'SCALAR' || ref($_) eq 'REF' },
148 constraint_generator => sub {
149 my $type_parameter = shift;
150 my $check = $type_parameter->_compiled_type_constraint;
152 return $check->( ${$_} );
156 'ref(' . $_[1] . ') eq "SCALAR" '
157 . '|| ref(' . $_[1] . ') eq "REF"'
159 inline_generator => sub {
161 my $type_parameter = shift;
163 '(ref(' . $val . ') eq "SCALAR" || ref(' . $val . ') eq "REF") '
164 . '&& ' . $type_parameter->_inline_check('${(' . $val . ')}')
169 $registry->add_type_constraint(
170 Moose::Meta::TypeConstraint::Parameterizable->new(
172 package_defined_in => __PACKAGE__,
174 Moose::Util::TypeConstraints::find_type_constraint('Ref'),
175 constraint => sub { ref($_) eq 'ARRAY' },
176 constraint_generator => sub {
177 my $type_parameter = shift;
178 my $check = $type_parameter->_compiled_type_constraint;
180 foreach my $x (@$_) {
181 ( $check->($x) ) || return;
186 inlined => sub { 'ref(' . $_[1] . ') eq "ARRAY"' },
187 inline_generator => sub {
189 my $type_parameter = shift;
191 'ref(' . $val . ') eq "ARRAY" '
192 . '&& &List::MoreUtils::all('
193 . 'sub { ' . $type_parameter->_inline_check('$_') . ' }, '
200 $registry->add_type_constraint(
201 Moose::Meta::TypeConstraint::Parameterizable->new(
203 package_defined_in => __PACKAGE__,
205 Moose::Util::TypeConstraints::find_type_constraint('Ref'),
206 constraint => sub { ref($_) eq 'HASH' },
207 constraint_generator => sub {
208 my $type_parameter = shift;
209 my $check = $type_parameter->_compiled_type_constraint;
211 foreach my $x ( values %$_ ) {
212 ( $check->($x) ) || return;
217 inlined => sub { 'ref(' . $_[1] . ') eq "HASH"' },
218 inline_generator => sub {
220 my $type_parameter = shift;
222 'ref(' . $val . ') eq "HASH" '
223 . '&& &List::MoreUtils::all('
224 . 'sub { ' . $type_parameter->_inline_check('$_') . ' }, '
225 . 'values %{' . $val . '}'
231 $registry->add_type_constraint(
232 Moose::Meta::TypeConstraint::Parameterizable->new(
234 package_defined_in => __PACKAGE__,
236 Moose::Util::TypeConstraints::find_type_constraint('Item'),
237 constraint => sub {1},
238 constraint_generator => sub {
239 my $type_parameter = shift;
240 my $check = $type_parameter->_compiled_type_constraint;
242 return 1 if not( defined($_) ) || $check->($_);
246 inlined => sub {'1'},
247 inline_generator => sub {
249 my $type_parameter = shift;
251 '!defined(' . $val . ') '
252 . '|| (' . $type_parameter->_inline_check($val) . ')'