1 package Mouse::Util::TypeConstraints;
6 use Scalar::Util qw/blessed looks_like_number openhandle/;
13 #find_type_constraint register_type_constraint
17 my $caller = $args{callee} || caller(0);
20 *{"$caller\::as"} = \&_as;
21 *{"$caller\::where"} = \&_where;
22 *{"$caller\::message"} = \&_message;
23 *{"$caller\::from"} = \&_from;
24 *{"$caller\::via"} = \&_via;
25 *{"$caller\::type"} = \&_type;
26 *{"$caller\::subtype"} = \&_subtype;
27 *{"$caller\::coerce"} = \&_coerce;
28 *{"$caller\::class_type"} = \&_class_type;
29 *{"$caller\::role_type"} = \&_role_type;
48 my $optimized_constraints;
49 my $optimized_constraints_base;
51 no warnings 'uninitialized';
56 !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0'
58 Undef => sub { !defined($_) },
59 Defined => sub { defined($_) },
60 Value => sub { defined($_) && !ref($_) },
61 Num => sub { !ref($_) && looks_like_number($_) },
62 Int => sub { defined($_) && !ref($_) && /^-?[0-9]+$/ },
63 Str => sub { defined($_) && !ref($_) },
64 ClassName => sub { Mouse::is_class_loaded($_) },
65 Ref => sub { ref($_) },
67 ScalarRef => sub { ref($_) eq 'SCALAR' },
68 ArrayRef => sub { ref($_) eq 'ARRAY' },
69 HashRef => sub { ref($_) eq 'HASH' },
70 CodeRef => sub { ref($_) eq 'CODE' },
71 RegexpRef => sub { ref($_) eq 'Regexp' },
72 GlobRef => sub { ref($_) eq 'GLOB' },
75 ref($_) eq 'GLOB' && openhandle($_)
77 blessed($_) && $_->isa("IO::Handle")
80 Object => sub { blessed($_) && blessed($_) ne 'Regexp' },
83 sub optimized_constraints { \%TYPE }
84 my @TYPE_KEYS = keys %TYPE;
85 sub list_all_builtin_type_constraints { @TYPE_KEYS }
87 @TYPE_SOURCE{@TYPE_KEYS} = (__PACKAGE__) x @TYPE_KEYS;
92 my($name, %conf) = @_;
93 if (my $type = $TYPE{$name}) {
94 Carp::croak "The type constraint '$name' has already been created in $TYPE_SOURCE{$name} and cannot be created again in $pkg";
96 my $constraint = $conf{where} || do { $TYPE{delete $conf{as} || 'Any' } };
98 $TYPE_SOURCE{$name} = $pkg;
99 $TYPE{$name} = $constraint;
104 my($name, %conf) = @_;
105 if (my $type = $TYPE{$name}) {
106 Carp::croak "The type constraint '$name' has already been created in $TYPE_SOURCE{$name} and cannot be created again in $pkg";
108 my $constraint = $conf{where} || do { $TYPE{delete $conf{as} || 'Any' } };
109 my $as = $conf{as} || '';
111 $TYPE_SOURCE{$name} = $pkg;
113 if ($as = $TYPE{$as}) {
114 $TYPE{$name} = sub { $as->($_) && $constraint->($_) };
116 $TYPE{$name} = $constraint;
121 my($name, %conf) = @_;
123 Carp::croak "Cannot find type '$name', perhaps you forgot to load it."
126 unless ($COERCE{$name}) {
128 $COERCE_KEYS{$name} = [];
130 while (my($type, $code) = each %conf) {
131 Carp::croak "A coercion action already exists for '$type'"
132 if $COERCE{$name}->{$type};
134 Carp::croak "Could not find the type constraint ($type) to coerce from"
137 push @{ $COERCE_KEYS{$name} }, $type;
138 $COERCE{$name}->{$type} = $code;
144 my($name, $conf) = @_;
145 my $class = $conf->{class};
146 Mouse::load_class($class);
148 $name => where => sub { $_->isa($class) }
153 my($name, $conf) = @_;
154 my $role = $conf->{role};
156 $name => where => sub {
157 return unless defined $_ && ref($_) && $_->isa('Mouse::Object');
158 $_->meta->does_role($role);
163 sub typecast_constraints {
164 my($class, $pkg, $type_constraint, $types, $value) = @_;
167 for my $type (ref($types) eq 'ARRAY' ? @{ $types } : ( $types )) {
168 next unless $COERCE{$type};
169 for my $coerce_type (@{ $COERCE_KEYS{$type}}) {
171 next unless $TYPE{$coerce_type}->();
173 $_ = $COERCE{$type}->{$coerce_type}->();
174 return $_ if $type_constraint->();
186 Mouse::Util::TypeConstraints - simple type constraints
190 =head2 optimized_constraints -> HashRef[CODE]
192 Returns the simple type constraints that Mouse understands.