2 package Moose::Util::TypeConstraints;
8 use Scalar::Util 'blessed';
10 our $VERSION = '0.02';
12 use Moose::Meta::TypeConstraint;
13 use Moose::Meta::TypeCoercion;
17 my $pkg = shift || caller();
18 return if $pkg eq '-no-export';
20 foreach my $export (qw(type subtype as where coerce from via find_type_constraint)) {
21 *{"${pkg}::${export}"} = \&{"${export}"};
27 sub find_type_constraint { $TYPES{$_[0]} }
29 sub _create_type_constraint {
30 my ($name, $parent, $check) = @_;
31 (!exists $TYPES{$name})
32 || confess "The type constraint '$name' has already been created"
34 $parent = $TYPES{$parent} if defined $parent;
35 my $constraint = Moose::Meta::TypeConstraint->new(
36 name => $name || '__ANON__',
40 $TYPES{$name} = $constraint if defined $name;
44 sub _install_type_coercions {
45 my ($type_name, $coercion_map) = @_;
46 my $type = $TYPES{$type_name};
47 (!$type->has_coercion)
48 || confess "The type coercion for '$type_name' has already been registered";
49 my $type_coercion = Moose::Meta::TypeCoercion->new(
50 type_coercion_map => $coercion_map,
51 type_constraint => $type
53 $type->coercion($type_coercion);
56 sub export_type_contstraints_as_functions {
59 foreach my $constraint (keys %TYPES) {
60 *{"${pkg}::${constraint}"} = $TYPES{$constraint}->_compiled_type_constraint;
68 my ($name, $check) = @_;
69 _create_type_constraint($name, undef, $check);
73 unshift @_ => undef if scalar @_ == 2;
74 _create_type_constraint(@_);
78 my ($type_name, @coercion_map) = @_;
79 _install_type_coercions($type_name, \@coercion_map);
83 sub from ($) { $_[0] }
84 sub where (&) { $_[0] }
87 # define some basic types
89 type Any => where { 1 };
91 type Value => where { !ref($_) };
92 type Ref => where { ref($_) };
94 subtype Int => as Value => where { Scalar::Util::looks_like_number($_) };
95 subtype Str => as Value => where { !Scalar::Util::looks_like_number($_) };
97 subtype ScalarRef => as Ref => where { ref($_) eq 'SCALAR' };
98 subtype ArrayRef => as Ref => where { ref($_) eq 'ARRAY' };
99 subtype HashRef => as Ref => where { ref($_) eq 'HASH' };
100 subtype CodeRef => as Ref => where { ref($_) eq 'CODE' };
101 subtype RegexpRef => as Ref => where { ref($_) eq 'Regexp' };
104 # blessed(qr/.../) returns true,.. how odd
105 subtype Object => as Ref => where { blessed($_) && blessed($_) ne 'Regexp' };
115 Moose::Util::TypeConstraints - Type constraint system for Moose
119 use Moose::Util::TypeConstraints;
121 type Num => where { Scalar::Util::looks_like_number($_) };
127 subtype NaturalLessThanTen
129 => where { $_ < 10 };
137 This module provides Moose with the ability to create type contraints
138 to be are used in both attribute definitions and for method argument
141 =head2 Important Caveat
143 This is B<NOT> a type system for Perl 5. These are type constraints,
144 and they are not used by Moose unless you tell it to. No type
145 inference is performed, expression are not typed, etc. etc. etc.
147 This is simply a means of creating small constraint functions which
148 can be used to simply your own type-checking code.
150 =head2 Default Type Constraints
152 This module also provides a simple hierarchy for Perl 5 types, this
153 could probably use some work, but it works for me at the moment.
167 Suggestions for improvement are welcome.
171 =head2 Type Constraint Registry
175 =item B<find_type_constraint ($type_name)>
177 This function can be used to locate a specific type constraint
178 meta-object. What you do with it from there is up to you :)
180 =item B<export_type_contstraints_as_functions>
182 This will export all the current type constraints as functions
183 into the caller's namespace. Right now, this is mostly used for
184 testing, but it might prove useful to others.
188 =head2 Type Constraint Constructors
190 The following functions are used to create type constraints.
191 They will then register the type constraints in a global store
192 where Moose can get to them if it needs to.
194 See the L<SYNOPOSIS> for an example of how to use these.
198 =item B<type ($name, $where_clause)>
200 This creates a base type, which has no parent.
202 =item B<subtype ($name, $parent, $where_clause)>
204 This creates a named subtype.
206 =item B<subtype ($parent, $where_clause)>
208 This creates an unnamed subtype and will return the type
209 constraint meta-object, which will be an instance of
210 L<Moose::Meta::TypeConstraint>.
214 This is just sugar for the type constraint construction syntax.
218 This is just sugar for the type constraint construction syntax.
222 =head2 Type Coercion Constructors
224 Type constraints can also contain type coercions as well. In most
225 cases Moose will run the type-coercion code first, followed by the
226 type constraint check. This feature should be used carefully as it
227 is very powerful and could easily take off a limb if you are not
230 See the L<SYNOPOSIS> for an example of how to use these.
238 This is just sugar for the type coercion construction syntax.
242 This is just sugar for the type coercion construction syntax.
248 All complex software has bugs lurking in it, and this module is no
249 exception. If you find a bug please either email me, or add the bug
254 Stevan Little E<lt>stevan@iinteractive.comE<gt>
256 =head1 COPYRIGHT AND LICENSE
258 Copyright 2006 by Infinity Interactive, Inc.
260 L<http://www.iinteractive.com>
262 This library is free software; you can redistribute it and/or modify
263 it under the same terms as Perl itself.