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' || ref(\(my $val = $_)) 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 { (my $val = $_) =~ /\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 (ref($_) eq "GLOB" && Scalar::Util::openhandle($_))
106 || (blessed($_) && $_->isa("IO::Handle"));
109 '(ref(' . $_[1] . ') eq "GLOB" '
110 . '&& Scalar::Util::openhandle(' . $_[1] . ')) '
111 . '|| (Scalar::Util::blessed(' . $_[1] . ') '
112 . '&& ' . $_[1] . '->isa("IO::Handle"))'
117 => where { blessed($_) }
118 => inline_as { 'Scalar::Util::blessed(' . $_[1] . ')' };
120 # This type is deprecated.
123 => where { $_->can('does') };
127 => where { Class::MOP::is_class_loaded($_) }
128 => inline_as { 'Class::MOP::is_class_loaded(' . $_[1] . ')' };
133 (Class::MOP::class_of($_) || return)->isa('Moose::Meta::Role');
136 'Class::MOP::is_class_loaded(' . $_[1] . ') '
138 . 'my $meta = Class::MOP::class_of(' . $_[1] . ');'
139 . '$meta && $meta->isa("Moose::Meta::Role");'
143 $registry->add_type_constraint(
144 Moose::Meta::TypeConstraint::Parameterizable->new(
146 package_defined_in => __PACKAGE__,
148 Moose::Util::TypeConstraints::find_type_constraint('Ref'),
149 constraint => sub { ref($_) eq 'SCALAR' || ref($_) eq 'REF' },
150 constraint_generator => sub {
151 my $type_parameter = shift;
152 my $check = $type_parameter->_compiled_type_constraint;
154 return $check->( ${$_} );
158 'ref(' . $_[1] . ') eq "SCALAR" '
159 . '|| ref(' . $_[1] . ') eq "REF"'
161 inline_generator => sub {
163 my $type_parameter = shift;
165 '(ref(' . $val . ') eq "SCALAR" || ref(' . $val . ') eq "REF") '
166 . '&& ' . $type_parameter->_inline_check('${(' . $val . ')}')
171 $registry->add_type_constraint(
172 Moose::Meta::TypeConstraint::Parameterizable->new(
174 package_defined_in => __PACKAGE__,
176 Moose::Util::TypeConstraints::find_type_constraint('Ref'),
177 constraint => sub { ref($_) eq 'ARRAY' },
178 constraint_generator => sub {
179 my $type_parameter = shift;
180 my $check = $type_parameter->_compiled_type_constraint;
182 foreach my $x (@$_) {
183 ( $check->($x) ) || return;
188 inlined => sub { 'ref(' . $_[1] . ') eq "ARRAY"' },
189 inline_generator => sub {
191 my $type_parameter = shift;
195 . 'my $check = ' . $val . ';'
196 . 'ref($check) eq "ARRAY" '
197 . '&& &List::MoreUtils::all('
198 . 'sub { ' . $type_parameter->_inline_check('$_') . ' }, '
206 $registry->add_type_constraint(
207 Moose::Meta::TypeConstraint::Parameterizable->new(
209 package_defined_in => __PACKAGE__,
211 Moose::Util::TypeConstraints::find_type_constraint('Ref'),
212 constraint => sub { ref($_) eq 'HASH' },
213 constraint_generator => sub {
214 my $type_parameter = shift;
215 my $check = $type_parameter->_compiled_type_constraint;
217 foreach my $x ( values %$_ ) {
218 ( $check->($x) ) || return;
223 inlined => sub { 'ref(' . $_[1] . ') eq "HASH"' },
224 inline_generator => sub {
226 my $type_parameter = shift;
230 . 'my $check = ' . $val . ';'
231 . 'ref($check) eq "HASH" '
232 . '&& &List::MoreUtils::all('
233 . 'sub { ' . $type_parameter->_inline_check('$_') . ' }, '
241 $registry->add_type_constraint(
242 Moose::Meta::TypeConstraint::Parameterizable->new(
244 package_defined_in => __PACKAGE__,
246 Moose::Util::TypeConstraints::find_type_constraint('Item'),
247 constraint => sub {1},
248 constraint_generator => sub {
249 my $type_parameter = shift;
250 my $check = $type_parameter->_compiled_type_constraint;
252 return 1 if not( defined($_) ) || $check->($_);
256 inlined => sub {'1'},
257 inline_generator => sub {
259 my $type_parameter = shift;
261 '!defined(' . $val . ') '
262 . '|| (' . $type_parameter->_inline_check($val) . ')'