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;
17 my $pkg = shift || caller();
18 return if $pkg eq '-no-export';
20 foreach my $export (qw(type subtype as where message coerce from via find_type_constraint)) {
21 *{"${pkg}::${export}"} = \&{"${export}"};
27 sub find_type_constraint {
28 return $TYPES{$_[0]}->[1]
29 if exists $TYPES{$_[0]};
33 sub _dump_type_constraints {
35 Data::Dumper::Dumper(\%TYPES);
38 sub _create_type_constraint {
39 my ($name, $parent, $check, $message) = @_;
40 my $pkg_defined_in = scalar(caller(1));
41 ($TYPES{$name}->[0] eq $pkg_defined_in)
42 || confess "The type constraint '$name' has already been created "
43 if defined $name && exists $TYPES{$name};
44 $parent = find_type_constraint($parent) if defined $parent;
45 my $constraint = Moose::Meta::TypeConstraint->new(
46 name => $name || '__ANON__',
51 $TYPES{$name} = [ $pkg_defined_in, $constraint ] if defined $name;
55 sub _install_type_coercions {
56 my ($type_name, $coercion_map) = @_;
57 my $type = find_type_constraint($type_name);
58 (!$type->has_coercion)
59 || confess "The type coercion for '$type_name' has already been registered";
60 my $type_coercion = Moose::Meta::TypeCoercion->new(
61 type_coercion_map => $coercion_map,
62 type_constraint => $type
64 $type->coercion($type_coercion);
67 sub export_type_contstraints_as_functions {
70 foreach my $constraint (keys %TYPES) {
71 *{"${pkg}::${constraint}"} = find_type_constraint($constraint)->_compiled_type_constraint;
79 my ($name, $check) = @_;
80 _create_type_constraint($name, undef, $check);
84 unshift @_ => undef if scalar @_ <= 2;
85 _create_type_constraint(@_);
89 my ($type_name, @coercion_map) = @_;
90 _install_type_coercions($type_name, \@coercion_map);
94 sub from ($) { $_[0] }
95 sub where (&) { $_[0] }
97 sub message (&) { $_[0] }
99 # define some basic types
101 type 'Any' => where { 1 };
103 subtype 'Value' => as 'Any' => where { !ref($_) };
104 subtype 'Ref' => as 'Any' => where { ref($_) };
106 subtype 'Bool' => as 'Any' => where { "$_" eq '1' || "$_" eq '0' };
108 subtype 'Int' => as 'Value' => where { Scalar::Util::looks_like_number($_) };
109 subtype 'Str' => as 'Value' => where { !Scalar::Util::looks_like_number($_) };
111 subtype 'ScalarRef' => as 'Ref' => where { ref($_) eq 'SCALAR' };
113 subtype 'CollectionRef' => as 'Ref' => where { ref($_) eq 'ARRAY' || ref($_) eq 'HASH' };
115 subtype 'ArrayRef' => as 'CollectionRef' => where { ref($_) eq 'ARRAY' };
116 subtype 'HashRef' => as 'CollectionRef' => where { ref($_) eq 'HASH' };
118 subtype 'CodeRef' => as 'Ref' => where { ref($_) eq 'CODE' };
119 subtype 'RegexpRef' => as 'Ref' => where { ref($_) eq 'Regexp' };
122 # blessed(qr/.../) returns true,.. how odd
123 subtype 'Object' => as 'Ref' => where { blessed($_) && blessed($_) ne 'Regexp' };
125 subtype 'Role' => as 'Object' => where { $_->can('does') };
135 Moose::Util::TypeConstraints - Type constraint system for Moose
139 use Moose::Util::TypeConstraints;
141 type Num => where { Scalar::Util::looks_like_number($_) };
147 subtype NaturalLessThanTen
150 => message { "This number ($_) is not less than ten!" };
158 This module provides Moose with the ability to create type contraints
159 to be are used in both attribute definitions and for method argument
162 =head2 Important Caveat
164 This is B<NOT> a type system for Perl 5. These are type constraints,
165 and they are not used by Moose unless you tell it to. No type
166 inference is performed, expression are not typed, etc. etc. etc.
168 This is simply a means of creating small constraint functions which
169 can be used to simplify your own type-checking code.
171 =head2 Default Type Constraints
173 This module also provides a simple hierarchy for Perl 5 types, this
174 could probably use some work, but it works for me at the moment.
191 Suggestions for improvement are welcome.
195 =head2 Type Constraint Registry
199 =item B<find_type_constraint ($type_name)>
201 This function can be used to locate a specific type constraint
202 meta-object. What you do with it from there is up to you :)
204 =item B<export_type_contstraints_as_functions>
206 This will export all the current type constraints as functions
207 into the caller's namespace. Right now, this is mostly used for
208 testing, but it might prove useful to others.
212 =head2 Type Constraint Constructors
214 The following functions are used to create type constraints.
215 They will then register the type constraints in a global store
216 where Moose can get to them if it needs to.
218 See the L<SYNOPOSIS> for an example of how to use these.
222 =item B<type ($name, $where_clause)>
224 This creates a base type, which has no parent.
226 =item B<subtype ($name, $parent, $where_clause, ?$message)>
228 This creates a named subtype.
230 =item B<subtype ($parent, $where_clause, ?$message)>
232 This creates an unnamed subtype and will return the type
233 constraint meta-object, which will be an instance of
234 L<Moose::Meta::TypeConstraint>.
238 This is just sugar for the type constraint construction syntax.
242 This is just sugar for the type constraint construction syntax.
246 This is just sugar for the type constraint construction syntax.
250 =head2 Type Coercion Constructors
252 Type constraints can also contain type coercions as well. In most
253 cases Moose will run the type-coercion code first, followed by the
254 type constraint check. This feature should be used carefully as it
255 is very powerful and could easily take off a limb if you are not
258 See the L<SYNOPOSIS> for an example of how to use these.
266 This is just sugar for the type coercion construction syntax.
270 This is just sugar for the type coercion construction syntax.
276 All complex software has bugs lurking in it, and this module is no
277 exception. If you find a bug please either email me, or add the bug
282 Stevan Little E<lt>stevan@iinteractive.comE<gt>
284 =head1 COPYRIGHT AND LICENSE
286 Copyright 2006 by Infinity Interactive, Inc.
288 L<http://www.iinteractive.com>
290 This library is free software; you can redistribute it and/or modify
291 it under the same terms as Perl itself.