2 package Mouse::TypeRegistry;
7 use Mouse::Util qw/blessed looks_like_number openhandle/;
15 my $caller = caller(0);
17 if (defined $args{'-export'} && ref($args{'-export'}) eq 'ARRAY') {
19 *{"$caller\::import"} = sub { _import(@_) };
23 *{"$caller\::as"} = \&_as;
24 *{"$caller\::where"} = \&_where;
25 *{"$caller\::message"} = \&_message;
26 *{"$caller\::from"} = \&_from;
27 *{"$caller\::via"} = \&_via;
28 *{"$caller\::subtype"} = \&_subtype;
29 *{"$caller\::coerce"} = \&_coerce;
30 *{"$caller\::class_type"} = \&_class_type;
31 *{"$caller\::role_type"} = \&_role_type;
51 my($class, @types) = @_;
52 return unless exists $SUBTYPE->{$class} && exists $COERCE->{$class};
55 copy_types($class, $pkg, @types);
60 my($name, %conf) = @_;
61 if (my $type = $SUBTYPE->{$name}) {
62 Carp::croak "The type constraint '$name' has already been created, cannot be created again in $pkg";
65 my $stuff = $conf{where} || optimized_constraints()->{$as};
67 $SUBTYPE->{$name} = $stuff;
71 my($name, %conf) = @_;
73 Carp::croak "Cannot find type '$name', perhaps you forgot to load it."
74 unless optimized_constraints()->{$name};
76 my $subtypes = optimized_constraints();
77 $COERCE->{$name} ||= {};
78 while (my($type, $code) = each %conf) {
79 Carp::croak "A coercion action already exists for '$type'"
80 if $COERCE->{$name}->{$type};
82 Carp::croak "Could not find the type constraint ($type) to coerce from"
83 unless $subtypes->{$type};
85 $COERCE->{$name}->{$type} = $code;
91 my($name, $conf) = @_;
92 my $class = $conf->{class};
94 $name => where => sub {
95 defined $_ && ref($_) eq $class;
101 my($name, $conf) = @_;
102 my $role = $conf->{role};
104 $name => where => sub {
105 return unless defined $_ && ref($_) && $_->isa('Mouse::Object');
106 $_->meta->does_role($role);
111 sub typecast_constraints {
112 my($class, $pkg, $type, $value) = @_;
113 return $value unless $COERCE->{$type};
115 my $optimized_constraints = optimized_constraints();
116 for my $coerce_type (keys %{ $COERCE->{$type} }) {
118 if ($optimized_constraints->{$coerce_type}->()) {
120 return $COERCE->{$type}->{$coerce_type}->();
128 no warnings 'uninitialized';
129 my $optimized_constraints = {
133 !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0'
135 Undef => sub { !defined($_) },
136 Defined => sub { defined($_) },
137 Value => sub { defined($_) && !ref($_) },
138 Num => sub { !ref($_) && looks_like_number($_) },
139 Int => sub { defined($_) && !ref($_) && /^-?[0-9]+$/ },
140 Str => sub { defined($_) && !ref($_) },
141 ClassName => sub { Mouse::is_class_loaded($_) },
142 Ref => sub { ref($_) },
144 ScalarRef => sub { ref($_) eq 'SCALAR' },
145 ArrayRef => sub { ref($_) eq 'ARRAY' },
146 HashRef => sub { ref($_) eq 'HASH' },
147 CodeRef => sub { ref($_) eq 'CODE' },
148 RegexpRef => sub { ref($_) eq 'Regexp' },
149 GlobRef => sub { ref($_) eq 'GLOB' },
156 && $_->isa("IO::Handle")
159 Object => sub { blessed($_) && blessed($_) ne 'Regexp' },
161 sub optimized_constraints {
162 return { %{ $SUBTYPE }, %{ $optimized_constraints } };
172 Mouse::TypeRegistry - simple type constraints
176 =head2 optimized_constraints -> HashRef[CODE]
178 Returns the simple type constraints that Mouse understands.