2 package Moose::Util::TypeConstraints;
8 use Scalar::Util 'blessed';
10 our $VERSION = '0.07';
12 use Moose::Meta::TypeConstraint;
13 use Moose::Meta::TypeCoercion;
17 exports => qw[type subtype as where message coerce from via find_type_constraint enum],
26 sub find_type_constraint ($) {
27 return $TYPES{$_[0]}->[1]
28 if exists $TYPES{$_[0]};
32 sub _dump_type_constraints {
34 Data::Dumper::Dumper(\%TYPES);
37 sub _create_type_constraint ($$$;$) {
38 my ($name, $parent, $check, $message) = @_;
39 my $pkg_defined_in = scalar(caller(1));
40 ($TYPES{$name}->[0] eq $pkg_defined_in)
41 || confess "The type constraint '$name' has already been created "
42 if defined $name && exists $TYPES{$name};
43 $parent = find_type_constraint($parent) if defined $parent;
44 my $constraint = Moose::Meta::TypeConstraint->new(
45 name => $name || '__ANON__',
50 $TYPES{$name} = [ $pkg_defined_in, $constraint ] if defined $name;
54 sub _install_type_coercions ($$) {
55 my ($type_name, $coercion_map) = @_;
56 my $type = find_type_constraint($type_name);
57 (!$type->has_coercion)
58 || confess "The type coercion for '$type_name' has already been registered";
59 my $type_coercion = Moose::Meta::TypeCoercion->new(
60 type_coercion_map => $coercion_map,
61 type_constraint => $type
63 $type->coercion($type_coercion);
66 sub create_type_constraint_union (@) {
67 my (@type_constraint_names) = @_;
68 return Moose::Meta::TypeConstraint->union(
70 find_type_constraint($_)
71 } @type_constraint_names
75 sub export_type_contstraints_as_functions {
78 foreach my $constraint (keys %TYPES) {
79 *{"${pkg}::${constraint}"} = find_type_constraint($constraint)->_compiled_type_constraint;
87 my ($name, $check) = @_;
88 _create_type_constraint($name, undef, $check);
92 unshift @_ => undef if scalar @_ <= 2;
93 goto &_create_type_constraint;
97 my ($type_name, @coercion_map) = @_;
98 _install_type_coercions($type_name, \@coercion_map);
102 sub from ($) { $_[0] }
103 sub where (&) { $_[0] }
104 sub via (&) { $_[0] }
105 sub message (&) { $_[0] }
108 my ($type_name, @values) = @_;
109 (scalar @values >= 2)
110 || confess "You must have at least two values to enumerate through";
111 my $regexp = join '|' => @values;
112 _create_type_constraint(
115 sub { qr/^$regexp$/i }
119 # define some basic types
121 type 'Any' => where { 1 }; # meta-type including all
122 type 'Item' => where { 1 }; # base-type
124 subtype 'Undef' => as 'Item' => where { !defined($_) };
125 subtype 'Defined' => as 'Item' => where { defined($_) };
127 subtype 'Bool' => as 'Item' => where { !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' };
129 subtype 'Value' => as 'Defined' => where { !ref($_) };
130 subtype 'Ref' => as 'Defined' => where { ref($_) };
132 subtype 'Str' => as 'Value' => where { 1 };
134 subtype 'Num' => as 'Value' => where { Scalar::Util::looks_like_number($_) };
135 subtype 'Int' => as 'Num' => where { "$_" =~ /^-?[0-9]+$/ };
137 subtype 'ScalarRef' => as 'Ref' => where { ref($_) eq 'SCALAR' };
138 subtype 'ArrayRef' => as 'Ref' => where { ref($_) eq 'ARRAY' };
139 subtype 'HashRef' => as 'Ref' => where { ref($_) eq 'HASH' };
140 subtype 'CodeRef' => as 'Ref' => where { ref($_) eq 'CODE' };
141 subtype 'RegexpRef' => as 'Ref' => where { ref($_) eq 'Regexp' };
144 # blessed(qr/.../) returns true,.. how odd
145 subtype 'Object' => as 'Ref' => where { blessed($_) && blessed($_) ne 'Regexp' };
147 subtype 'Role' => as 'Object' => where { $_->can('does') };
157 Moose::Util::TypeConstraints - Type constraint system for Moose
161 use Moose::Util::TypeConstraints;
163 type 'Num' => where { Scalar::Util::looks_like_number($_) };
169 subtype 'NaturalLessThanTen'
172 => message { "This number ($_) is not less than ten!" };
178 enum 'RGBColors' => qw(red green blue);
182 This module provides Moose with the ability to create type contraints
183 to be are used in both attribute definitions and for method argument
186 =head2 Important Caveat
188 This is B<NOT> a type system for Perl 5. These are type constraints,
189 and they are not used by Moose unless you tell it to. No type
190 inference is performed, expression are not typed, etc. etc. etc.
192 This is simply a means of creating small constraint functions which
193 can be used to simplify your own type-checking code.
195 =head2 Slightly Less Important Caveat
197 It is almost always a good idea to quote your type and subtype names.
198 This is to prevent perl from trying to create the call as an indirect
199 object call. This issue only seems to come up when you have a subtype
200 the same name as a valid class, but when the issue does arise it tends
201 to be quite annoying to debug.
203 So for instance, this:
205 subtype DateTime => as Object => where { $_->isa('DateTime') };
207 will I<Just Work>, while this:
210 subtype DateTime => as Object => where { $_->isa('DateTime') };
212 will fail silently and cause many headaches. The simple way to solve
213 this, as well as future proof your subtypes from classes which have
214 yet to have been created yet, is to simply do this:
217 subtype 'DateTime' => as Object => where { $_->isa('DateTime') };
219 =head2 Default Type Constraints
221 This module also provides a simple hierarchy for Perl 5 types, this
222 could probably use some work, but it works for me at the moment.
242 Suggestions for improvement are welcome.
244 B<NOTE:> The C<Undef> type constraint does not work correctly
245 in every occasion, please use it sparringly.
249 =head2 Type Constraint Registry
253 =item B<find_type_constraint ($type_name)>
255 This function can be used to locate a specific type constraint
256 meta-object. What you do with it from there is up to you :)
258 =item B<create_type_constraint_union (@type_constraint_names)>
260 Given a list of C<@type_constraint_names>, this will return a
261 B<Moose::Meta::TypeConstraint::Union> instance.
263 =item B<export_type_contstraints_as_functions>
265 This will export all the current type constraints as functions
266 into the caller's namespace. Right now, this is mostly used for
267 testing, but it might prove useful to others.
271 =head2 Type Constraint Constructors
273 The following functions are used to create type constraints.
274 They will then register the type constraints in a global store
275 where Moose can get to them if it needs to.
277 See the L<SYNOPOSIS> for an example of how to use these.
281 =item B<type ($name, $where_clause)>
283 This creates a base type, which has no parent.
285 =item B<subtype ($name, $parent, $where_clause, ?$message)>
287 This creates a named subtype.
289 =item B<subtype ($parent, $where_clause, ?$message)>
291 This creates an unnamed subtype and will return the type
292 constraint meta-object, which will be an instance of
293 L<Moose::Meta::TypeConstraint>.
295 =item B<enum ($name, @values)>
297 This will create a basic subtype for a given set of strings.
298 The resulting constraint will be a subtype of C<Str> and
299 will match any of the items in C<@values>. See the L<SYNOPSIS>
300 for a simple example.
302 B<NOTE:> This is not a true proper enum type, it is simple
303 a convient constraint builder.
307 This is just sugar for the type constraint construction syntax.
311 This is just sugar for the type constraint construction syntax.
315 This is just sugar for the type constraint construction syntax.
319 =head2 Type Coercion Constructors
321 Type constraints can also contain type coercions as well. In most
322 cases Moose will run the type-coercion code first, followed by the
323 type constraint check. This feature should be used carefully as it
324 is very powerful and could easily take off a limb if you are not
327 See the L<SYNOPOSIS> for an example of how to use these.
335 This is just sugar for the type coercion construction syntax.
339 This is just sugar for the type coercion construction syntax.
345 All complex software has bugs lurking in it, and this module is no
346 exception. If you find a bug please either email me, or add the bug
351 Stevan Little E<lt>stevan@iinteractive.comE<gt>
353 =head1 COPYRIGHT AND LICENSE
355 Copyright 2006 by Infinity Interactive, Inc.
357 L<http://www.iinteractive.com>
359 This library is free software; you can redistribute it and/or modify
360 it under the same terms as Perl itself.