1 package Mouse::Util::TypeConstraints;
7 use Scalar::Util qw/blessed looks_like_number openhandle/;
10 as where message from via type subtype coerce class_type role_type enum
34 no warnings 'uninitialized';
39 !defined($_[0]) || $_[0] eq "" || "$_[0]" eq '1' || "$_[0]" eq '0'
41 Undef => sub { !defined($_[0]) },
42 Defined => sub { defined($_[0]) },
43 Value => sub { defined($_[0]) && !ref($_[0]) },
44 Num => sub { !ref($_[0]) && looks_like_number($_[0]) },
45 Int => sub { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ },
46 Str => sub { defined($_[0]) && !ref($_[0]) },
47 ClassName => sub { Mouse::is_class_loaded($_[0]) },
48 Ref => sub { ref($_[0]) },
50 ScalarRef => sub { ref($_[0]) eq 'SCALAR' },
51 ArrayRef => sub { ref($_[0]) eq 'ARRAY' },
52 HashRef => sub { ref($_[0]) eq 'HASH' },
53 CodeRef => sub { ref($_[0]) eq 'CODE' },
54 RegexpRef => sub { ref($_[0]) eq 'Regexp' },
55 GlobRef => sub { ref($_[0]) eq 'GLOB' },
58 ref($_[0]) eq 'GLOB' && openhandle($_[0])
60 blessed($_[0]) && $_[0]->isa("IO::Handle")
63 Object => sub { blessed($_[0]) && blessed($_[0]) ne 'Regexp' },
66 sub optimized_constraints { \%TYPE }
67 my @TYPE_KEYS = keys %TYPE;
68 sub list_all_builtin_type_constraints { @TYPE_KEYS }
70 @TYPE_SOURCE{@TYPE_KEYS} = (__PACKAGE__) x @TYPE_KEYS;
75 my($name, %conf) = @_;
76 if ($TYPE{$name} && $TYPE_SOURCE{$name} ne $pkg) {
77 Carp::croak "The type constraint '$name' has already been created in $TYPE_SOURCE{$name} and cannot be created again in $pkg";
79 my $constraint = $conf{where} || do {
80 my $as = delete $conf{as} || 'Any';
81 if (! exists $TYPE{$as}) {
82 $TYPE{$as} = _build_type_constraint($as);
87 $TYPE_SOURCE{$name} = $pkg;
88 $TYPE{$name} = sub { local $_ = $_[0]; $constraint->($_[0]) };
93 my($name, %conf) = @_;
94 if ($TYPE{$name} && $TYPE_SOURCE{$name} ne $pkg) {
95 Carp::croak "The type constraint '$name' has already been created in $TYPE_SOURCE{$name} and cannot be created again in $pkg";
97 my $constraint = $conf{where};
98 my $as_constraint = find_or_create_isa_type_constraint($conf{as} || 'Any');
100 $TYPE_SOURCE{$name} = $pkg;
101 $TYPE{$name} = $constraint ?
104 $as_constraint->($_[0]) && $constraint->($_[0])
108 $as_constraint->($_[0]);
116 my($name, %conf) = @_;
118 Carp::croak "Cannot find type '$name', perhaps you forgot to load it."
121 unless ($COERCE{$name}) {
123 $COERCE_KEYS{$name} = [];
125 while (my($type, $code) = each %conf) {
126 Carp::croak "A coercion action already exists for '$type'"
127 if $COERCE{$name}->{$type};
129 if (! $TYPE{$type}) {
130 # looks parameterized
131 if ($type =~ /^[^\[]+\[.+\]$/) {
132 _build_type_constraint($type);
134 Carp::croak "Could not find the type constraint ($type) to coerce from"
138 push @{ $COERCE_KEYS{$name} }, $type;
139 $COERCE{$name}->{$type} = $code;
144 my($name, $conf) = @_;
145 if ($conf && $conf->{class}) {
146 # No, you're using this wrong
147 warn "class_type() should be class_type(ClassName). Perhaps you're looking for subtype $name => as '$conf->{class}'?";
148 subtype($name, as => $conf->{class});
151 $name => where => sub { $_->isa($name) }
157 my($name, $conf) = @_;
158 my $role = $conf->{role};
160 $name => where => sub {
161 return unless defined $_ && ref($_) && $_->isa('Mouse::Object');
162 $_->meta->does_role($role);
167 sub typecast_constraints {
168 my($class, $pkg, $type_constraint, $types, $value) = @_;
171 for my $type (ref($types) eq 'ARRAY' ? @{ $types } : ( $types )) {
172 next unless $COERCE{$type};
173 for my $coerce_type (@{ $COERCE_KEYS{$type}}) {
175 next unless $TYPE{$coerce_type}->($value);
177 $_ = $COERCE{$type}->{$coerce_type}->($value);
178 return $_ if $type_constraint->($_);
186 # enum ['small', 'medium', 'large']
187 if (ref($_[0]) eq 'ARRAY') {
188 my @elements = @{ shift @_ };
190 my $name = 'Mouse::Util::TypeConstaints::Enum::Serial::'
192 enum($name, @elements);
196 # enum size => 'small', 'medium', 'large'
198 my %is_valid = map { $_ => 1 } @_;
201 $name => where => sub { $is_valid{$_} }
205 sub _build_type_constraint {
209 if ($spec =~ /^([^\[]+)\[(.+)\]$/) {
214 if ($constraint eq 'Maybe') {
215 $parent = _build_type_constraint('Undef');
217 $parent = _build_type_constraint($constraint);
219 my $child = _build_type_constraint($param);
220 if ($constraint eq 'ArrayRef') {
222 "#line " . __LINE__ . ' "' . __FILE__ . "\"\n" .
224 " if (\$parent->(\$_[0])) {\n" .
225 " foreach my \$e (\@{\$_[0]}) {\n" .
226 " return () unless \$child->(\$e);\n" .
233 $code = eval $code_str or Carp::confess("Failed to generate inline type constraint: $@");
234 } elsif ($constraint eq 'HashRef') {
236 "#line " . __LINE__ . ' "' . __FILE__ . "\"\n" .
238 " if (\$parent->(\$_[0])) {\n" .
239 " foreach my \$e (values \%{\$_[0]}) {\n" .
240 " return () unless \$child->(\$e);\n" .
247 $code = eval $code_str or Carp::confess($@);
248 } elsif ($constraint eq 'Maybe') {
250 "#line " . __LINE__ . ' "' . __FILE__ . "\"\n" .
252 " return \$child->(\$_[0]) || \$parent->(\$_[0]);\n" .
255 $code = eval $code_str or Carp::confess($@);
257 Carp::confess("Support for parameterized types other than ArrayRef or HashRef is not implemented yet");
259 $TYPE{$spec} = $code;
261 $code = $TYPE{ $spec };
264 "#line " . __LINE__ . ' "' . __FILE__ . "\"\n" .
266 " Scalar::Util::blessed(\$_[0]) && \$_[0]->isa('$spec');\n" .
269 $code = eval $code_str or Carp::confess($@);
270 $TYPE{$spec} = $code;
276 sub find_type_constraint {
277 my $type_constraint = shift;
278 return $TYPE{$type_constraint};
281 sub find_or_create_isa_type_constraint {
282 my $type_constraint = shift;
286 $type_constraint =~ s/\s+//g;
287 my @type_constraints = split /\|/, $type_constraint;
288 if (@type_constraints == 1) {
289 $code = $TYPE{$type_constraints[0]} ||
290 _build_type_constraint($type_constraints[0]);
292 my @code_list = map {
293 $TYPE{$_} || _build_type_constraint($_)
297 for my $code (@code_list) {
298 return 1 if $code->($_[0]);
312 Mouse::Util::TypeConstraints - simple type constraints
316 =head2 optimized_constraints -> HashRef[CODE]
318 Returns the simple type constraints that Mouse understands.
324 =item B<subtype 'Name' => as 'Parent' => where { } ...>
326 =item B<subtype as 'Parent' => where { } ...>
328 =item B<class_type ($class, ?$options)>
330 =item B<role_type ($role, ?$options)>
332 =item B<enum (\@values)>