2 package Moose::Util::TypeConstraints;
8 use Scalar::Util 'blessed';
12 our $VERSION = '0.10';
14 use Moose::Meta::TypeConstraint;
15 use Moose::Meta::TypeCoercion;
18 type subtype as where message optimize_as
24 Sub::Exporter::setup_exporter({
26 groups => { default => [':all'] }
32 # loop through the exports ...
33 foreach my $name (@exports) {
35 if (defined &{$class . '::' . $name}) {
36 my $keyword = \&{$class . '::' . $name};
38 # make sure it is from Moose
39 my $pkg_name = eval { svref_2object($keyword)->GV->STASH->NAME };
41 next if $pkg_name ne 'Moose::Util::TypeConstraints';
43 # and if it is from Moose then undef the slot
44 delete ${$class . '::'}{$name};
51 sub find_type_constraint ($) {
52 return $TYPES{$_[0]}->[1]
53 if exists $TYPES{$_[0]};
57 sub _dump_type_constraints {
59 Data::Dumper::Dumper(\%TYPES);
62 sub _create_type_constraint ($$$;$$) {
67 my ($message, $optimized);
69 $message = $_->{message} if exists $_->{message};
70 $optimized = $_->{optimized} if exists $_->{optimized};
73 my $pkg_defined_in = scalar(caller(1));
74 ($TYPES{$name}->[0] eq $pkg_defined_in)
75 || confess "The type constraint '$name' has already been created "
76 if defined $name && exists $TYPES{$name};
77 $parent = find_type_constraint($parent) if defined $parent;
78 my $constraint = Moose::Meta::TypeConstraint->new(
79 name => $name || '__ANON__',
83 optimized => $optimized,
85 $TYPES{$name} = [ $pkg_defined_in, $constraint ] if defined $name;
89 sub _install_type_coercions ($$) {
90 my ($type_name, $coercion_map) = @_;
91 my $type = find_type_constraint($type_name);
92 (!$type->has_coercion)
93 || confess "The type coercion for '$type_name' has already been registered";
94 my $type_coercion = Moose::Meta::TypeCoercion->new(
95 type_coercion_map => $coercion_map,
96 type_constraint => $type
98 $type->coercion($type_coercion);
101 sub create_type_constraint_union (@) {
102 my (@type_constraint_names) = @_;
103 return Moose::Meta::TypeConstraint->union(
105 find_type_constraint($_)
106 } @type_constraint_names
110 sub export_type_contstraints_as_functions {
113 foreach my $constraint (keys %TYPES) {
114 *{"${pkg}::${constraint}"} = find_type_constraint($constraint)->_compiled_type_constraint;
122 my ($name, $check) = @_;
123 _create_type_constraint($name, undef, $check);
126 sub subtype ($$;$$$) {
127 unshift @_ => undef if scalar @_ <= 2;
128 goto &_create_type_constraint;
132 my ($type_name, @coercion_map) = @_;
133 _install_type_coercions($type_name, \@coercion_map);
137 sub from ($) { $_[0] }
138 sub where (&) { $_[0] }
139 sub via (&) { $_[0] }
141 sub message (&) { +{ message => $_[0] } }
142 sub optimize_as (&) { +{ optimized => $_[0] } }
145 my ($type_name, @values) = @_;
146 (scalar @values >= 2)
147 || confess "You must have at least two values to enumerate through";
148 my $regexp = join '|' => @values;
149 _create_type_constraint(
152 sub { qr/^$regexp$/i }
156 # define some basic types
158 type 'Any' => where { 1 }; # meta-type including all
159 type 'Item' => where { 1 }; # base-type
161 subtype 'Undef' => as 'Item' => where { !defined($_) };
162 subtype 'Defined' => as 'Item' => where { defined($_) };
166 => where { !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' };
170 => where { !ref($_) }
171 => optimize_as { defined($_[0]) && !ref($_[0]) };
176 => optimize_as { ref($_[0]) };
181 => optimize_as { defined($_[0]) && !ref($_[0]) };
185 => where { Scalar::Util::looks_like_number($_) }
186 => optimize_as { !ref($_[0]) && Scalar::Util::looks_like_number($_[0]) };
190 => where { "$_" =~ /^-?[0-9]+$/ }
191 => optimize_as { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ };
193 subtype 'ScalarRef' => as 'Ref' => where { ref($_) eq 'SCALAR' } => optimize_as { ref($_[0]) eq 'SCALAR' };
194 subtype 'ArrayRef' => as 'Ref' => where { ref($_) eq 'ARRAY' } => optimize_as { ref($_[0]) eq 'ARRAY' };
195 subtype 'HashRef' => as 'Ref' => where { ref($_) eq 'HASH' } => optimize_as { ref($_[0]) eq 'HASH' };
196 subtype 'CodeRef' => as 'Ref' => where { ref($_) eq 'CODE' } => optimize_as { ref($_[0]) eq 'CODE' };
197 subtype 'RegexpRef' => as 'Ref' => where { ref($_) eq 'Regexp' } => optimize_as { ref($_[0]) eq 'Regexp' };
198 subtype 'GlobRef' => as 'Ref' => where { ref($_) eq 'GLOB' } => optimize_as { ref($_[0]) eq 'GLOB' };
201 # scalar filehandles are GLOB refs,
202 # but a GLOB ref is not always a filehandle
205 => where { Scalar::Util::openhandle($_) }
206 => optimize_as { ref($_[0]) eq 'GLOB' && Scalar::Util::openhandle($_[0]) };
209 # blessed(qr/.../) returns true,.. how odd
212 => where { blessed($_) && blessed($_) ne 'Regexp' }
213 => optimize_as { blessed($_[0]) && blessed($_[0]) ne 'Regexp' };
217 => where { $_->can('does') }
218 => optimize_as { blessed($_[0]) && $_[0]->can('does') };
228 Moose::Util::TypeConstraints - Type constraint system for Moose
232 use Moose::Util::TypeConstraints;
234 type 'Num' => where { Scalar::Util::looks_like_number($_) };
240 subtype 'NaturalLessThanTen'
243 => message { "This number ($_) is not less than ten!" };
249 enum 'RGBColors' => qw(red green blue);
253 This module provides Moose with the ability to create type contraints
254 to be are used in both attribute definitions and for method argument
257 =head2 Important Caveat
259 This is B<NOT> a type system for Perl 5. These are type constraints,
260 and they are not used by Moose unless you tell it to. No type
261 inference is performed, expression are not typed, etc. etc. etc.
263 This is simply a means of creating small constraint functions which
264 can be used to simplify your own type-checking code.
266 =head2 Slightly Less Important Caveat
268 It is almost always a good idea to quote your type and subtype names.
269 This is to prevent perl from trying to execute the call as an indirect
270 object call. This issue only seems to come up when you have a subtype
271 the same name as a valid class, but when the issue does arise it tends
272 to be quite annoying to debug.
274 So for instance, this:
276 subtype DateTime => as Object => where { $_->isa('DateTime') };
278 will I<Just Work>, while this:
281 subtype DateTime => as Object => where { $_->isa('DateTime') };
283 will fail silently and cause many headaches. The simple way to solve
284 this, as well as future proof your subtypes from classes which have
285 yet to have been created yet, is to simply do this:
288 subtype 'DateTime' => as Object => where { $_->isa('DateTime') };
290 =head2 Default Type Constraints
292 This module also provides a simple hierarchy for Perl 5 types, this
293 could probably use some work, but it works for me at the moment.
315 Suggestions for improvement are welcome.
317 B<NOTE:> The C<Undef> type constraint does not work correctly
318 in every occasion, please use it sparringly.
320 =head2 Use with Other Constraint Modules
322 This module should play fairly nicely with other constraint
323 modules with only some slight tweaking. The C<where> clause
324 in types is expected to be a C<CODE> reference which checks
325 it's first argument and returns a bool. Since most constraint
326 modules work in a similar way, it should be simple to adapt
327 them to work with Moose.
329 For instance, this is how you could use it with
330 L<Declare::Constraint::Simple> to declare a completely new type.
332 type 'HashOfArrayOfObjects'
335 -values => IsArrayRef( IsObject ));
337 For more examples see the F<t/204_example_w_DCS.t> test file.
339 Here is an example of using L<Test::Deep> and it's non-test
340 related C<eq_deeply> function.
342 type 'ArrayOfHashOfBarsAndRandomNumbers'
345 array_each(subhashof({
347 random_number => ignore()
351 For a complete example see the F<t/205_example_w_TestDeep.t>
356 =head2 Type Constraint Registry
360 =item B<find_type_constraint ($type_name)>
362 This function can be used to locate a specific type constraint
363 meta-object. What you do with it from there is up to you :)
365 =item B<create_type_constraint_union (@type_constraint_names)>
367 Given a list of C<@type_constraint_names>, this will return a
368 B<Moose::Meta::TypeConstraint::Union> instance.
370 =item B<export_type_contstraints_as_functions>
372 This will export all the current type constraints as functions
373 into the caller's namespace. Right now, this is mostly used for
374 testing, but it might prove useful to others.
378 =head2 Type Constraint Constructors
380 The following functions are used to create type constraints.
381 They will then register the type constraints in a global store
382 where Moose can get to them if it needs to.
384 See the L<SYNOPOSIS> for an example of how to use these.
388 =item B<type ($name, $where_clause)>
390 This creates a base type, which has no parent.
392 =item B<subtype ($name, $parent, $where_clause, ?$message)>
394 This creates a named subtype.
396 =item B<subtype ($parent, $where_clause, ?$message)>
398 This creates an unnamed subtype and will return the type
399 constraint meta-object, which will be an instance of
400 L<Moose::Meta::TypeConstraint>.
402 =item B<enum ($name, @values)>
404 This will create a basic subtype for a given set of strings.
405 The resulting constraint will be a subtype of C<Str> and
406 will match any of the items in C<@values>. See the L<SYNOPSIS>
407 for a simple example.
409 B<NOTE:> This is not a true proper enum type, it is simple
410 a convient constraint builder.
414 This is just sugar for the type constraint construction syntax.
418 This is just sugar for the type constraint construction syntax.
422 This is just sugar for the type constraint construction syntax.
428 =head2 Type Coercion Constructors
430 Type constraints can also contain type coercions as well. If you
431 ask your accessor too coerce, the Moose will run the type-coercion
432 code first, followed by the type constraint check. This feature
433 should be used carefully as it is very powerful and could easily
434 take off a limb if you are not careful.
436 See the L<SYNOPOSIS> for an example of how to use these.
444 This is just sugar for the type coercion construction syntax.
448 This is just sugar for the type coercion construction syntax.
452 =head2 Namespace Management
458 This will remove all the type constraint keywords from the
459 calling class namespace.
465 All complex software has bugs lurking in it, and this module is no
466 exception. If you find a bug please either email me, or add the bug
471 Stevan Little E<lt>stevan@iinteractive.comE<gt>
473 =head1 COPYRIGHT AND LICENSE
475 Copyright 2006, 2007 by Infinity Interactive, Inc.
477 L<http://www.iinteractive.com>
479 This library is free software; you can redistribute it and/or modify
480 it under the same terms as Perl itself.