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;
46 my $optimized_constraints;
47 my $optimized_constraints_base;
49 no warnings 'uninitialized';
50 $optimized_constraints = $optimized_constraints_base = {
54 !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0'
56 Undef => sub { !defined($_) },
57 Defined => sub { defined($_) },
58 Value => sub { defined($_) && !ref($_) },
59 Num => sub { !ref($_) && looks_like_number($_) },
60 Int => sub { defined($_) && !ref($_) && /^-?[0-9]+$/ },
61 Str => sub { defined($_) && !ref($_) },
62 ClassName => sub { Mouse::is_class_loaded($_) },
63 Ref => sub { ref($_) },
65 ScalarRef => sub { ref($_) eq 'SCALAR' },
66 ArrayRef => sub { ref($_) eq 'ARRAY' },
67 HashRef => sub { ref($_) eq 'HASH' },
68 CodeRef => sub { ref($_) eq 'CODE' },
69 RegexpRef => sub { ref($_) eq 'Regexp' },
70 GlobRef => sub { ref($_) eq 'GLOB' },
77 && $_->isa("IO::Handle")
80 Object => sub { blessed($_) && blessed($_) ne 'Regexp' },
85 my($name, %conf) = @_;
86 if (my $type = $SUBTYPE->{$name}) {
87 Carp::croak "The type constraint '$name' has already been created, cannot be created again in $pkg";
90 my $stuff = $conf{where} || optimized_constraints()->{$as};
92 $SUBTYPE->{$name} = $stuff;
93 $optimized_constraints = +{ %{ $SUBTYPE }, %{ $optimized_constraints_base } };
97 my($name, %conf) = @_;
99 Carp::croak "Cannot find type '$name', perhaps you forgot to load it."
100 unless optimized_constraints()->{$name};
102 my $subtypes = optimized_constraints();
103 $COERCE->{$name} ||= {};
104 while (my($type, $code) = each %conf) {
105 Carp::croak "A coercion action already exists for '$type'"
106 if $COERCE->{$name}->{$type};
108 Carp::croak "Could not find the type constraint ($type) to coerce from"
109 unless $subtypes->{$type};
111 $COERCE->{$name}->{$type} = $code;
117 my($name, $conf) = @_;
118 my $class = $conf->{class};
120 $name => where => sub {
121 defined $_ && ref($_) eq $class;
127 my($name, $conf) = @_;
128 my $role = $conf->{role};
130 $name => where => sub {
131 return unless defined $_ && ref($_) && $_->isa('Mouse::Object');
132 $_->meta->does_role($role);
137 sub typecast_constraints {
138 my($class, $pkg, $type, $value) = @_;
139 return $value unless $COERCE->{$type};
141 my $optimized_constraints = optimized_constraints();
142 for my $coerce_type (keys %{ $COERCE->{$type} }) {
144 if ($optimized_constraints->{$coerce_type}->()) {
146 return $COERCE->{$type}->{$coerce_type}->();
153 sub optimized_constraints { $optimized_constraints }
155 my @optimized_constraints_keys = keys %{ $optimized_constraints };
156 sub list_all_builtin_type_constraints { @optimized_constraints_keys }
165 Mouse::TypeRegistry - simple type constraints
169 =head2 optimized_constraints -> HashRef[CODE]
171 Returns the simple type constraints that Mouse understands.