2 package Mouse::TypeRegistry;
7 use Mouse::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\::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 { \%SUBTYPE }
85 my @SUBTYPE_KEYS = keys %SUBTYPE;
86 sub list_all_builtin_type_constraints { @SUBTYPE_KEYS }
91 my($name, %conf) = @_;
92 if (my $type = $SUBTYPE{$name}) {
93 Carp::croak "The type constraint '$name' has already been created, cannot be created again in $pkg";
96 my $stuff = $conf{where} || optimized_constraints()->{$as};
98 $SUBTYPE{$name} = $stuff;
102 my($name, %conf) = @_;
104 Carp::croak "Cannot find type '$name', perhaps you forgot to load it."
105 unless optimized_constraints()->{$name};
107 my $subtypes = optimized_constraints();
108 unless ($COERCE{$name}) {
110 $COERCE_KEYS{$name} = [];
112 while (my($type, $code) = each %conf) {
113 Carp::croak "A coercion action already exists for '$type'"
114 if $COERCE{$name}->{$type};
116 Carp::croak "Could not find the type constraint ($type) to coerce from"
117 unless $subtypes->{$type};
119 push @{ $COERCE_KEYS{$name} }, $type;
120 $COERCE{$name}->{$type} = $code;
126 my($name, $conf) = @_;
127 my $class = $conf->{class};
129 $name => where => sub {
130 defined $_ && ref($_) eq $class;
136 my($name, $conf) = @_;
137 my $role = $conf->{role};
139 $name => where => sub {
140 return unless defined $_ && ref($_) && $_->isa('Mouse::Object');
141 $_->meta->does_role($role);
146 sub typecast_constraints {
147 my($class, $pkg, $type_constraint, $types, $value) = @_;
149 for my $type (ref($types) eq 'ARRAY' ? @{ $types } : ( $types )) {
150 next unless $COERCE{$type};
152 for my $coerce_type (@{ $COERCE_KEYS{$type}}) {
154 if ($SUBTYPE{$coerce_type}->()) {
156 local $_ = $COERCE{$type}->{$coerce_type}->();
157 return $_ if $type_constraint->();
170 Mouse::TypeRegistry - simple type constraints
174 =head2 optimized_constraints -> HashRef[CODE]
176 Returns the simple type constraints that Mouse understands.