1 package Mouse::Util::TypeConstraints;
6 use Scalar::Util qw/blessed looks_like_number openhandle/;
12 #find_type_constraint register_type_constraint
16 my $caller = $args{callee} || caller(0);
19 *{"$caller\::as"} = \&_as;
20 *{"$caller\::where"} = \&_where;
21 *{"$caller\::message"} = \&_message;
22 *{"$caller\::from"} = \&_from;
23 *{"$caller\::via"} = \&_via;
24 *{"$caller\::type"} = \&_type;
25 *{"$caller\::subtype"} = \&_subtype;
26 *{"$caller\::coerce"} = \&_coerce;
27 *{"$caller\::class_type"} = \&_class_type;
28 *{"$caller\::role_type"} = \&_role_type;
47 my $optimized_constraints;
48 my $optimized_constraints_base;
50 no warnings 'uninitialized';
55 !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0'
57 Undef => sub { !defined($_) },
58 Defined => sub { defined($_) },
59 Value => sub { defined($_) && !ref($_) },
60 Num => sub { !ref($_) && looks_like_number($_) },
61 Int => sub { defined($_) && !ref($_) && /^-?[0-9]+$/ },
62 Str => sub { defined($_) && !ref($_) },
63 ClassName => sub { Mouse::is_class_loaded($_) },
64 Ref => sub { ref($_) },
66 ScalarRef => sub { ref($_) eq 'SCALAR' },
67 ArrayRef => sub { ref($_) eq 'ARRAY' },
68 HashRef => sub { ref($_) eq 'HASH' },
69 CodeRef => sub { ref($_) eq 'CODE' },
70 RegexpRef => sub { ref($_) eq 'Regexp' },
71 GlobRef => sub { ref($_) eq 'GLOB' },
78 && $_->isa("IO::Handle")
81 Object => sub { blessed($_) && blessed($_) ne 'Regexp' },
84 sub optimized_constraints { \%TYPE }
85 my @TYPE_KEYS = keys %TYPE;
86 sub list_all_builtin_type_constraints { @TYPE_KEYS }
91 my($name, %conf) = @_;
92 if (my $type = $TYPE{$name}) {
93 Carp::croak "The type constraint '$name' has already been created, cannot be created again in $pkg";
95 my $stuff = $conf{where} || do { $TYPE{delete $conf{as} || 'Any' } };
96 $TYPE{$name} = $stuff;
101 my($name, %conf) = @_;
102 if (my $type = $TYPE{$name}) {
103 Carp::croak "The type constraint '$name' has already been created, cannot be created again in $pkg";
105 my $stuff = $conf{where} || do { $TYPE{delete $conf{as} || 'Any' } };
106 my $as = $conf{as} || '';
107 if ($as = $TYPE{$as}) {
108 $TYPE{$name} = sub { $as->($_) && $stuff->($_) };
110 $TYPE{$name} = $stuff;
115 my($name, %conf) = @_;
117 Carp::croak "Cannot find type '$name', perhaps you forgot to load it."
120 unless ($COERCE{$name}) {
122 $COERCE_KEYS{$name} = [];
124 while (my($type, $code) = each %conf) {
125 Carp::croak "A coercion action already exists for '$type'"
126 if $COERCE{$name}->{$type};
128 Carp::croak "Could not find the type constraint ($type) to coerce from"
131 push @{ $COERCE_KEYS{$name} }, $type;
132 $COERCE{$name}->{$type} = $code;
138 my($name, $conf) = @_;
139 my $class = $conf->{class};
140 Mouse::load_class($class);
142 $name => where => sub { $_->isa($class) }
147 my($name, $conf) = @_;
148 my $role = $conf->{role};
150 $name => where => sub {
151 return unless defined $_ && ref($_) && $_->isa('Mouse::Object');
152 $_->meta->does_role($role);
157 sub typecast_constraints {
158 my($class, $pkg, $type_constraint, $types, $value) = @_;
161 for my $type (ref($types) eq 'ARRAY' ? @{ $types } : ( $types )) {
162 next unless $COERCE{$type};
163 for my $coerce_type (@{ $COERCE_KEYS{$type}}) {
165 next unless $TYPE{$coerce_type}->();
167 $_ = $COERCE{$type}->{$coerce_type}->();
168 return $_ if $type_constraint->();
180 Mouse::Util::TypeConstraints - simple type constraints
184 =head2 optimized_constraints -> HashRef[CODE]
186 Returns the simple type constraints that Mouse understands.