2 package Moose::Util::TypeConstraints;
8 use Scalar::Util 'blessed';
10 our $VERSION = '0.05';
12 use Moose::Meta::TypeConstraint;
13 use Moose::Meta::TypeCoercion;
16 require Sub::Exporter;
18 my @exports = qw[type subtype as where message coerce from via find_type_constraint enum];
20 Sub::Exporter->import(
32 sub find_type_constraint {
33 return $TYPES{$_[0]}->[1]
34 if exists $TYPES{$_[0]};
38 sub _dump_type_constraints {
40 Data::Dumper::Dumper(\%TYPES);
43 sub _create_type_constraint {
44 my ($name, $parent, $check, $message) = @_;
45 my $pkg_defined_in = scalar(caller(1));
46 ($TYPES{$name}->[0] eq $pkg_defined_in)
47 || confess "The type constraint '$name' has already been created "
48 if defined $name && exists $TYPES{$name};
49 $parent = find_type_constraint($parent) if defined $parent;
50 my $constraint = Moose::Meta::TypeConstraint->new(
51 name => $name || '__ANON__',
56 $TYPES{$name} = [ $pkg_defined_in, $constraint ] if defined $name;
60 sub _install_type_coercions {
61 my ($type_name, $coercion_map) = @_;
62 my $type = find_type_constraint($type_name);
63 (!$type->has_coercion)
64 || confess "The type coercion for '$type_name' has already been registered";
65 my $type_coercion = Moose::Meta::TypeCoercion->new(
66 type_coercion_map => $coercion_map,
67 type_constraint => $type
69 $type->coercion($type_coercion);
72 sub create_type_constraint_union {
73 my (@type_constraint_names) = @_;
74 return Moose::Meta::TypeConstraint->union(
76 find_type_constraint($_)
77 } @type_constraint_names
81 sub export_type_contstraints_as_functions {
84 foreach my $constraint (keys %TYPES) {
85 *{"${pkg}::${constraint}"} = find_type_constraint($constraint)->_compiled_type_constraint;
93 my ($name, $check) = @_;
94 _create_type_constraint($name, undef, $check);
98 unshift @_ => undef if scalar @_ <= 2;
99 _create_type_constraint(@_);
103 my ($type_name, @coercion_map) = @_;
104 _install_type_coercions($type_name, \@coercion_map);
108 sub from ($) { $_[0] }
109 sub where (&) { $_[0] }
110 sub via (&) { $_[0] }
111 sub message (&) { $_[0] }
114 my ($type_name, @values) = @_;
115 my $regexp = join '|' => @values;
116 _create_type_constraint(
119 sub { qr/^$regexp$/i }
123 # define some basic types
125 type 'Any' => where { 1 }; # meta-type including all
126 type 'Item' => where { 1 }; # base-type
128 subtype 'Undef' => as 'Item' => where { !defined($_) };
129 subtype 'Defined' => as 'Item' => where { defined($_) };
131 subtype 'Bool' => as 'Item' => where { !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' };
133 subtype 'Value' => as 'Defined' => where { !ref($_) };
134 subtype 'Ref' => as 'Defined' => where { ref($_) };
136 subtype 'Str' => as 'Value' => where { 1 };
138 subtype 'Num' => as 'Value' => where { Scalar::Util::looks_like_number($_) };
139 subtype 'Int' => as 'Num' => where { "$_" =~ /^-?[0-9]+$/ };
141 subtype 'ScalarRef' => as 'Ref' => where { ref($_) eq 'SCALAR' };
142 subtype 'ArrayRef' => as 'Ref' => where { ref($_) eq 'ARRAY' };
143 subtype 'HashRef' => as 'Ref' => where { ref($_) eq 'HASH' };
144 subtype 'CodeRef' => as 'Ref' => where { ref($_) eq 'CODE' };
145 subtype 'RegexpRef' => as 'Ref' => where { ref($_) eq 'Regexp' };
148 # blessed(qr/.../) returns true,.. how odd
149 subtype 'Object' => as 'Ref' => where { blessed($_) && blessed($_) ne 'Regexp' };
151 subtype 'Role' => as 'Object' => where { $_->can('does') };
161 Moose::Util::TypeConstraints - Type constraint system for Moose
165 use Moose::Util::TypeConstraints;
167 type Num => where { Scalar::Util::looks_like_number($_) };
173 subtype NaturalLessThanTen
176 => message { "This number ($_) is not less than ten!" };
184 This module provides Moose with the ability to create type contraints
185 to be are used in both attribute definitions and for method argument
188 =head2 Important Caveat
190 This is B<NOT> a type system for Perl 5. These are type constraints,
191 and they are not used by Moose unless you tell it to. No type
192 inference is performed, expression are not typed, etc. etc. etc.
194 This is simply a means of creating small constraint functions which
195 can be used to simplify your own type-checking code.
197 =head2 Default Type Constraints
199 This module also provides a simple hierarchy for Perl 5 types, this
200 could probably use some work, but it works for me at the moment.
220 Suggestions for improvement are welcome.
224 =head2 Type Constraint Registry
228 =item B<find_type_constraint ($type_name)>
230 This function can be used to locate a specific type constraint
231 meta-object. What you do with it from there is up to you :)
233 =item B<create_type_constraint_union (@type_constraint_names)>
235 Given a list of C<@type_constraint_names>, this will return a
236 B<Moose::Meta::TypeConstraint::Union> instance.
238 =item B<export_type_contstraints_as_functions>
240 This will export all the current type constraints as functions
241 into the caller's namespace. Right now, this is mostly used for
242 testing, but it might prove useful to others.
246 =head2 Type Constraint Constructors
248 The following functions are used to create type constraints.
249 They will then register the type constraints in a global store
250 where Moose can get to them if it needs to.
252 See the L<SYNOPOSIS> for an example of how to use these.
256 =item B<type ($name, $where_clause)>
258 This creates a base type, which has no parent.
260 =item B<subtype ($name, $parent, $where_clause, ?$message)>
262 This creates a named subtype.
264 =item B<subtype ($parent, $where_clause, ?$message)>
266 This creates an unnamed subtype and will return the type
267 constraint meta-object, which will be an instance of
268 L<Moose::Meta::TypeConstraint>.
270 =item B<enum ($name, @values)>
274 This is just sugar for the type constraint construction syntax.
278 This is just sugar for the type constraint construction syntax.
282 This is just sugar for the type constraint construction syntax.
286 =head2 Type Coercion Constructors
288 Type constraints can also contain type coercions as well. In most
289 cases Moose will run the type-coercion code first, followed by the
290 type constraint check. This feature should be used carefully as it
291 is very powerful and could easily take off a limb if you are not
294 See the L<SYNOPOSIS> for an example of how to use these.
302 This is just sugar for the type coercion construction syntax.
306 This is just sugar for the type coercion construction syntax.
312 All complex software has bugs lurking in it, and this module is no
313 exception. If you find a bug please either email me, or add the bug
318 Stevan Little E<lt>stevan@iinteractive.comE<gt>
320 =head1 COPYRIGHT AND LICENSE
322 Copyright 2006 by Infinity Interactive, Inc.
324 L<http://www.iinteractive.com>
326 This library is free software; you can redistribute it and/or modify
327 it under the same terms as Perl itself.