2 package Mouse::TypeRegistry;
7 use Mouse::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\::subtype"} = \&_subtype;
25 *{"$caller\::coerce"} = \&_coerce;
26 *{"$caller\::class_type"} = \&_class_type;
27 *{"$caller\::role_type"} = \&_role_type;
48 my($name, %conf) = @_;
49 if (my $type = $SUBTYPE->{$name}) {
50 Carp::croak "The type constraint '$name' has already been created, cannot be created again in $pkg";
53 my $stuff = $conf{where} || optimized_constraints()->{$as};
55 $SUBTYPE->{$name} = $stuff;
59 my($name, %conf) = @_;
61 Carp::croak "Cannot find type '$name', perhaps you forgot to load it."
62 unless optimized_constraints()->{$name};
64 my $subtypes = optimized_constraints();
65 $COERCE->{$name} ||= {};
66 while (my($type, $code) = each %conf) {
67 Carp::croak "A coercion action already exists for '$type'"
68 if $COERCE->{$name}->{$type};
70 Carp::croak "Could not find the type constraint ($type) to coerce from"
71 unless $subtypes->{$type};
73 $COERCE->{$name}->{$type} = $code;
79 my($name, $conf) = @_;
80 my $class = $conf->{class};
82 $name => where => sub {
83 defined $_ && ref($_) eq $class;
89 my($name, $conf) = @_;
90 my $role = $conf->{role};
92 $name => where => sub {
93 return unless defined $_ && ref($_) && $_->isa('Mouse::Object');
94 $_->meta->does_role($role);
99 sub typecast_constraints {
100 my($class, $pkg, $type, $value) = @_;
101 return $value unless $COERCE->{$type};
103 my $optimized_constraints = optimized_constraints();
104 for my $coerce_type (keys %{ $COERCE->{$type} }) {
106 if ($optimized_constraints->{$coerce_type}->()) {
108 return $COERCE->{$type}->{$coerce_type}->();
116 no warnings 'uninitialized';
117 my $optimized_constraints = {
121 !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0'
123 Undef => sub { !defined($_) },
124 Defined => sub { defined($_) },
125 Value => sub { defined($_) && !ref($_) },
126 Num => sub { !ref($_) && looks_like_number($_) },
127 Int => sub { defined($_) && !ref($_) && /^-?[0-9]+$/ },
128 Str => sub { defined($_) && !ref($_) },
129 ClassName => sub { Mouse::is_class_loaded($_) },
130 Ref => sub { ref($_) },
132 ScalarRef => sub { ref($_) eq 'SCALAR' },
133 ArrayRef => sub { ref($_) eq 'ARRAY' },
134 HashRef => sub { ref($_) eq 'HASH' },
135 CodeRef => sub { ref($_) eq 'CODE' },
136 RegexpRef => sub { ref($_) eq 'Regexp' },
137 GlobRef => sub { ref($_) eq 'GLOB' },
144 && $_->isa("IO::Handle")
147 Object => sub { blessed($_) && blessed($_) ne 'Regexp' },
149 sub list_all_builtin_type_constraints { keys %{ $optimized_constraints } }
150 sub optimized_constraints {
151 return { %{ $SUBTYPE }, %{ $optimized_constraints } };
161 Mouse::TypeRegistry - simple type constraints
165 =head2 optimized_constraints -> HashRef[CODE]
167 Returns the simple type constraints that Mouse understands.