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 $_[0]" };
34 => where { defined($_) }
35 => inline_as { "defined $_[0]" };
39 => where { !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' }
40 => inline_as { qq{!defined($_[0]) || $_[0] eq "" || "$_[0]" eq '1' || "$_[0]" eq '0'} };
45 => optimize_as( \&_Value )
46 => inline_as { "defined $_[0] && ! ref $_[0]" };
51 => optimize_as( \&_Ref )
52 => inline_as { "ref $_[0]" };
56 => where { ref(\$_) eq 'SCALAR' }
57 => optimize_as( \&_Str )
59 return ( qq{defined $_[0]}
60 . qq{&& ( ref(\\ $_[0] ) eq 'SCALAR'}
61 . qq{ || ref(\\(my \$value = $_[0])) eq 'SCALAR')} );
66 => where { Scalar::Util::looks_like_number($_) }
67 => optimize_as( \&_Num )
68 => inline_as { "!ref $_[0] && Scalar::Util::looks_like_number($_[0])" };
72 => where { "$_" =~ /^-?[0-9]+$/ }
73 => optimize_as( \&_Int )
75 return ( qq{defined $_[0]}
77 . qq{&& ( my \$value = $_[0] ) =~ /\\A-?[0-9]+\\z/} );
82 => where { ref($_) eq 'CODE' }
83 => optimize_as( \&_CodeRef )
84 => inline_as { qq{ref $_[0] eq 'CODE'} };
88 => where( \&_RegexpRef )
89 => optimize_as( \&_RegexpRef )
90 => inline_as { "Moose::Util::TypeConstraints::Builtins::_RegexpRef( $_[0] )" };
94 => where { ref($_) eq 'GLOB' }
95 => optimize_as( \&_GlobRef )
96 => inline_as { qq{ref $_[0] eq 'GLOB'} };
98 # NOTE: scalar filehandles are GLOB refs, but a GLOB ref is not always a
103 Scalar::Util::openhandle($_) || ( blessed($_) && $_->isa("IO::Handle") );
105 => optimize_as( \&_FileHandle )
107 return ( qq{ref $_[0] eq 'GLOB'}
108 . qq{&& Scalar::Util::openhandle( $_[0] )}
109 . qq{or Scalar::Util::blessed( $_[0] ) && $_[0]->isa("IO::Handle")} );
114 => where { blessed($_) }
115 => optimize_as( \&_Object )
116 => inline_as { "Scalar::Util::blessed( $_[0] )" };
118 # This type is deprecated.
121 => where { $_->can('does') }
122 => optimize_as( \&_Role );
126 => where { Class::MOP::is_class_loaded($_) }
127 => optimize_as( \&_ClassName )
128 => inline_as { "Class::MOP::is_class_loaded( $_[0] )" };
133 (Class::MOP::class_of($_) || return)->isa('Moose::Meta::Role');
135 => optimize_as( \&_RoleName )
137 return ( qq{Class::MOP::is_class_loaded( $_[0] )}
138 . qq{&& ( Class::MOP::class_of( $_[0] ) || return )}
139 . qq{ ->isa('Moose::Meta::Role')} );
142 $registry->add_type_constraint(
143 Moose::Meta::TypeConstraint::Parameterizable->new(
145 package_defined_in => __PACKAGE__,
147 Moose::Util::TypeConstraints::find_type_constraint('Ref'),
148 constraint => sub { ref($_) eq 'SCALAR' || ref($_) eq 'REF' },
149 optimized => \&_ScalarRef,
150 constraint_generator => sub {
151 my $type_parameter = shift;
152 my $check = $type_parameter->_compiled_type_constraint;
154 return $check->( ${$_} );
157 inlined => sub {qq{ref $_[0] eq 'SCALAR' || ref $_[0] eq 'REF'}},
158 inline_generator => sub {
159 my $type_parameter = shift;
161 return $type_parameter->_inline_check(
162 '${ (' . $val . ') }' );
167 $registry->add_type_constraint(
168 Moose::Meta::TypeConstraint::Parameterizable->new(
170 package_defined_in => __PACKAGE__,
172 Moose::Util::TypeConstraints::find_type_constraint('Ref'),
173 constraint => sub { ref($_) eq 'ARRAY' },
174 optimized => \&_ArrayRef,
175 constraint_generator => sub {
176 my $type_parameter = shift;
177 my $check = $type_parameter->_compiled_type_constraint;
179 foreach my $x (@$_) {
180 ( $check->($x) ) || return;
185 inlined => sub {qq{ref $_[0] eq 'ARRAY'}},
186 inline_generator => sub {
187 my $type_parameter = shift;
190 '&List::MoreUtils::all( sub { '
191 . $type_parameter->_inline_check('$_')
197 $registry->add_type_constraint(
198 Moose::Meta::TypeConstraint::Parameterizable->new(
200 package_defined_in => __PACKAGE__,
202 Moose::Util::TypeConstraints::find_type_constraint('Ref'),
203 constraint => sub { ref($_) eq 'HASH' },
204 optimized => \&_HashRef,
205 constraint_generator => sub {
206 my $type_parameter = shift;
207 my $check = $type_parameter->_compiled_type_constraint;
209 foreach my $x ( values %$_ ) {
210 ( $check->($x) ) || return;
215 inlined => sub {qq{ref $_[0] eq 'HASH'}},
216 inline_generator => sub {
217 my $type_parameter = shift;
220 '&List::MoreUtils::all( sub { '
221 . $type_parameter->_inline_check('$_')
222 . " }, values \%{$val} )";
227 $registry->add_type_constraint(
228 Moose::Meta::TypeConstraint::Parameterizable->new(
230 package_defined_in => __PACKAGE__,
232 Moose::Util::TypeConstraints::find_type_constraint('Item'),
233 constraint => sub {1},
234 constraint_generator => sub {
235 my $type_parameter = shift;
236 my $check = $type_parameter->_compiled_type_constraint;
238 return 1 if not( defined($_) ) || $check->($_);
242 inlined => sub {'1'},
243 inline_generator => sub {
244 my $type_parameter = shift;
247 "(! defined $val) || ("
248 . $type_parameter->_inline_check($val) . ')';
254 sub _Value { defined($_[0]) && !ref($_[0]) }
256 sub _Ref { ref($_[0]) }
258 # We might need to use a temporary here to flatten LVALUEs, for instance as in
259 # Str(substr($_,0,255)).
262 && ( ref(\ $_[0] ) eq 'SCALAR'
263 || ref(\(my $value = $_[0])) eq 'SCALAR')
266 sub _Num { !ref($_[0]) && looks_like_number($_[0]) }
268 # using a temporary here because regex matching promotes an IV to a PV,
269 # and that confuses some things (like JSON.pm)
272 defined($value) && !ref($value) && $value =~ /\A-?[0-9]+\z/
275 sub _ScalarRef { ref($_[0]) eq 'SCALAR' || ref($_[0]) eq 'REF' }
276 sub _ArrayRef { ref($_[0]) eq 'ARRAY' }
277 sub _HashRef { ref($_[0]) eq 'HASH' }
278 sub _CodeRef { ref($_[0]) eq 'CODE' }
279 sub _GlobRef { ref($_[0]) eq 'GLOB' }
281 # RegexpRef is implemented in Moose.xs
284 ref( $_[0] ) eq 'GLOB' && Scalar::Util::openhandle( $_[0] )
285 or blessed( $_[0] ) && $_[0]->isa("IO::Handle");
288 sub _Object { blessed($_[0]) }
291 Moose::Deprecated::deprecated(
292 feature => 'Role type',
294 'The Role type has been deprecated. Maybe you meant to create a RoleName type? This type be will be removed in Moose 2.0200.'
296 blessed( $_[0] ) && $_[0]->can('does');
300 return Class::MOP::is_class_loaded( $_[0] );
305 && ( Class::MOP::class_of( $_[0] ) || return )
306 ->isa('Moose::Meta::Role');