2 package Moose::Util::TypeConstraints;
8 use Scalar::Util 'blessed', 'reftype';
12 our $VERSION = '0.15';
13 our $AUTHORITY = 'cpan:STEVAN';
15 ## --------------------------------------------------------
16 # Prototyped subs must be predeclared because we have a
17 # circular dependency with Moose::Meta::Attribute et. al.
18 # so in case of us being use'd first the predeclaration
19 # ensures the prototypes are in scope when consumers are
22 # creation and location
23 sub find_type_constraint ($);
24 sub find_or_create_type_constraint ($;$);
25 sub create_type_constraint_union (@);
26 sub create_parameterized_type_constraint ($);
41 sub _create_type_constraint ($$$;$$);
42 sub _install_type_coercions ($$);
44 ## --------------------------------------------------------
46 use Moose::Meta::TypeConstraint;
47 use Moose::Meta::TypeConstraint::Union;
48 use Moose::Meta::TypeConstraint::Parameterized;
49 use Moose::Meta::TypeCoercion;
50 use Moose::Meta::TypeCoercion::Union;
51 use Moose::Meta::TypeConstraint::Registry;
54 type subtype as where message optimize_as
60 Sub::Exporter::setup_exporter({
62 groups => { default => [':all'] }
68 # loop through the exports ...
69 foreach my $name (@exports) {
71 if (defined &{$class . '::' . $name}) {
72 my $keyword = \&{$class . '::' . $name};
74 # make sure it is from Moose
75 my $pkg_name = eval { svref_2object($keyword)->GV->STASH->NAME };
77 next if $pkg_name ne 'Moose::Util::TypeConstraints';
79 # and if it is from Moose then undef the slot
80 delete ${$class . '::'}{$name};
85 ## --------------------------------------------------------
86 ## type registry and some useful functions for it
87 ## --------------------------------------------------------
89 my $REGISTRY = Moose::Meta::TypeConstraint::Registry->new;
91 sub get_type_constraint_registry { $REGISTRY }
92 sub list_all_type_constraints { keys %{$REGISTRY->type_constraints} }
93 sub export_type_constraints_as_functions {
96 foreach my $constraint (keys %{$REGISTRY->type_constraints}) {
97 *{"${pkg}::${constraint}"} = $REGISTRY->get_type_constraint($constraint)
98 ->_compiled_type_constraint;
102 sub create_type_constraint_union (@) {
103 my @type_constraint_names;
105 if (scalar @_ == 1 && _detect_type_constraint_union($_[0])) {
106 @type_constraint_names = _parse_type_constraint_union($_[0]);
109 @type_constraint_names = @_;
112 (scalar @type_constraint_names >= 2)
113 || confess "You must pass in at least 2 type names to make a union";
115 ($REGISTRY->has_type_constraint($_))
116 || confess "Could not locate type constraint ($_) for the union"
117 foreach @type_constraint_names;
119 return Moose::Meta::TypeConstraint::Union->new(
120 type_constraints => [
122 $REGISTRY->get_type_constraint($_)
123 } @type_constraint_names
128 sub create_parameterized_type_constraint ($) {
129 my $type_constraint_name = shift;
131 my ($base_type, $type_parameter) = _parse_parameterized_type_constraint($type_constraint_name);
133 (defined $base_type && defined $type_parameter)
134 || confess "Could not parse type name ($type_constraint_name) correctly";
136 ($REGISTRY->has_type_constraint($base_type))
137 || confess "Could not locate the base type ($base_type)";
139 return Moose::Meta::TypeConstraint::Parameterized->new(
140 name => $type_constraint_name,
141 parent => $REGISTRY->get_type_constraint($base_type),
142 type_parameter => find_or_create_type_constraint(
144 parent => $REGISTRY->get_type_constraint('Object'),
145 constraint => sub { $_[0]->isa($type_parameter) }
151 sub find_or_create_type_constraint ($;$) {
152 my ($type_constraint_name, $options_for_anon_type) = @_;
154 return $REGISTRY->get_type_constraint($type_constraint_name)
155 if $REGISTRY->has_type_constraint($type_constraint_name);
159 if (_detect_type_constraint_union($type_constraint_name)) {
160 $constraint = create_type_constraint_union($type_constraint_name);
162 elsif (_detect_parameterized_type_constraint($type_constraint_name)) {
163 $constraint = create_parameterized_type_constraint($type_constraint_name);
167 # otherwise assume that we should create
168 # an ANON type with the $options_for_anon_type
169 # options which can be passed in. It should
170 # be noted that these don't get registered
171 # so we need to return it.
173 return Moose::Meta::TypeConstraint->new(
175 %{$options_for_anon_type}
179 $REGISTRY->add_type_constraint($constraint);
183 ## --------------------------------------------------------
184 ## exported functions ...
185 ## --------------------------------------------------------
187 sub find_type_constraint ($) { $REGISTRY->get_type_constraint(@_) }
192 splice(@_, 1, 0, undef);
193 goto &_create_type_constraint;
196 sub subtype ($$;$$$) {
198 # this adds an undef for the name
199 # if this is an anon-subtype:
200 # subtype(Num => where { $_ % 2 == 0 }) # anon 'even' subtype
201 # but if the last arg is not a code
202 # ref then it is a subtype alias:
203 # subtype(MyNumbers => as Num); # now MyNumbers is the same as Num
204 # ... yeah I know it's ugly code
206 unshift @_ => undef if scalar @_ <= 2 && (reftype($_[1]) || '') eq 'CODE';
207 goto &_create_type_constraint;
211 my ($type_name, @coercion_map) = @_;
212 _install_type_coercions($type_name, \@coercion_map);
216 sub from ($) { $_[0] }
217 sub where (&) { $_[0] }
218 sub via (&) { $_[0] }
220 sub message (&) { +{ message => $_[0] } }
221 sub optimize_as (&) { +{ optimized => $_[0] } }
224 my ($type_name, @values) = @_;
225 (scalar @values >= 2)
226 || confess "You must have at least two values to enumerate through";
227 my $regexp = join '|' => @values;
228 _create_type_constraint(
231 sub { qr/^$regexp$/i }
235 ## --------------------------------------------------------
236 ## desugaring functions ...
237 ## --------------------------------------------------------
239 sub _create_type_constraint ($$$;$$) {
242 my $check = shift || sub { 1 };
244 my ($message, $optimized);
246 $message = $_->{message} if exists $_->{message};
247 $optimized = $_->{optimized} if exists $_->{optimized};
250 my $pkg_defined_in = scalar(caller(0));
253 my $type = $REGISTRY->get_type_constraint($name);
255 ($type->_package_defined_in eq $pkg_defined_in)
256 || confess ("The type constraint '$name' has already been created in "
257 . $type->_package_defined_in . " and cannot be created again in "
262 $parent = $REGISTRY->get_type_constraint($parent) if defined $parent;
264 my $constraint = Moose::Meta::TypeConstraint->new(
265 name => $name || '__ANON__',
267 constraint => $check,
269 optimized => $optimized,
270 package_defined_in => $pkg_defined_in,
273 $REGISTRY->add_type_constraint($constraint)
279 sub _install_type_coercions ($$) {
280 my ($type_name, $coercion_map) = @_;
281 my $type = $REGISTRY->get_type_constraint($type_name);
282 (!$type->has_coercion)
283 || confess "The type coercion for '$type_name' has already been registered";
284 my $type_coercion = Moose::Meta::TypeCoercion->new(
285 type_coercion_map => $coercion_map,
286 type_constraint => $type
288 $type->coercion($type_coercion);
291 ## --------------------------------------------------------
292 ## type notation parsing ...
293 ## --------------------------------------------------------
296 # All I have to say is mugwump++ cause I know
297 # do not even have enough regexp-fu to be able
298 # to have written this (I can only barely
299 # understand it as it is)
304 my $valid_chars = qr{[\w:]};
305 my $type_atom = qr{ $valid_chars+ };
307 my $type = qr{ $valid_chars+ (?: \[ (??{$any}) \] )? }x;
308 my $type_capture_parts = qr{ ($valid_chars+) (?: \[ ((??{$any})) \] )? }x;
309 my $type_with_parameter = qr{ $valid_chars+ \[ (??{$any}) \] }x;
311 my $op_union = qr{ \s* \| \s* }x;
312 my $union = qr{ $type (?: $op_union $type )+ }x;
314 our $any = qr{ $type | $union }x;
316 sub _parse_parameterized_type_constraint {
317 $_[0] =~ m{ $type_capture_parts }x;
321 sub _detect_parameterized_type_constraint {
322 $_[0] =~ m{ ^ $type_with_parameter $ }x;
325 sub _parse_type_constraint_union {
328 while ( $given =~ m{ \G (?: $op_union )? ($type) }gcx ) {
331 (pos($given) eq length($given))
332 || confess "'$given' didn't parse (parse-pos="
340 sub _detect_type_constraint_union {
341 $_[0] =~ m{^ $type $op_union $type ( $op_union .* )? $}x;
345 ## --------------------------------------------------------
346 # define some basic built-in types
347 ## --------------------------------------------------------
349 type 'Any' => where { 1 }; # meta-type including all
350 type 'Item' => where { 1 }; # base-type
352 subtype 'Undef' => as 'Item' => where { !defined($_) };
353 subtype 'Defined' => as 'Item' => where { defined($_) };
357 => where { !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' };
361 => where { !ref($_) }
362 => optimize_as { defined($_[0]) && !ref($_[0]) };
367 => optimize_as { ref($_[0]) };
372 => optimize_as { defined($_[0]) && !ref($_[0]) };
376 => where { Scalar::Util::looks_like_number($_) }
377 => optimize_as { !ref($_[0]) && Scalar::Util::looks_like_number($_[0]) };
381 => where { "$_" =~ /^-?[0-9]+$/ }
382 => optimize_as { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ };
384 subtype 'ScalarRef' => as 'Ref' => where { ref($_) eq 'SCALAR' } => optimize_as { ref($_[0]) eq 'SCALAR' };
385 subtype 'ArrayRef' => as 'Ref' => where { ref($_) eq 'ARRAY' } => optimize_as { ref($_[0]) eq 'ARRAY' };
386 subtype 'HashRef' => as 'Ref' => where { ref($_) eq 'HASH' } => optimize_as { ref($_[0]) eq 'HASH' };
387 subtype 'CodeRef' => as 'Ref' => where { ref($_) eq 'CODE' } => optimize_as { ref($_[0]) eq 'CODE' };
388 subtype 'RegexpRef' => as 'Ref' => where { ref($_) eq 'Regexp' } => optimize_as { ref($_[0]) eq 'Regexp' };
389 subtype 'GlobRef' => as 'Ref' => where { ref($_) eq 'GLOB' } => optimize_as { ref($_[0]) eq 'GLOB' };
392 # scalar filehandles are GLOB refs,
393 # but a GLOB ref is not always a filehandle
396 => where { Scalar::Util::openhandle($_) }
397 => optimize_as { ref($_[0]) eq 'GLOB' && Scalar::Util::openhandle($_[0]) };
400 # blessed(qr/.../) returns true,.. how odd
403 => where { blessed($_) && blessed($_) ne 'Regexp' }
404 => optimize_as { blessed($_[0]) && blessed($_[0]) ne 'Regexp' };
408 => where { $_->can('does') }
409 => optimize_as { blessed($_[0]) && $_[0]->can('does') };
413 => where { eval { $_->isa('UNIVERSAL') } }
414 => optimize_as { !ref($_[0]) && eval { $_[0]->isa('UNIVERSAL') } };
416 ## --------------------------------------------------------
417 # end of built-in types ...
418 ## --------------------------------------------------------
421 my @BUILTINS = list_all_type_constraints();
422 sub list_all_builtin_type_constraints { @BUILTINS }
433 Moose::Util::TypeConstraints - Type constraint system for Moose
437 use Moose::Util::TypeConstraints;
439 type 'Num' => where { Scalar::Util::looks_like_number($_) };
445 subtype 'NaturalLessThanTen'
448 => message { "This number ($_) is not less than ten!" };
454 enum 'RGBColors' => qw(red green blue);
458 This module provides Moose with the ability to create custom type
459 contraints to be used in attribute definition.
461 =head2 Important Caveat
463 This is B<NOT> a type system for Perl 5. These are type constraints,
464 and they are not used by Moose unless you tell it to. No type
465 inference is performed, expression are not typed, etc. etc. etc.
467 This is simply a means of creating small constraint functions which
468 can be used to simplify your own type-checking code.
470 =head2 Slightly Less Important Caveat
472 It is almost always a good idea to quote your type and subtype names.
473 This is to prevent perl from trying to execute the call as an indirect
474 object call. This issue only seems to come up when you have a subtype
475 the same name as a valid class, but when the issue does arise it tends
476 to be quite annoying to debug.
478 So for instance, this:
480 subtype DateTime => as Object => where { $_->isa('DateTime') };
482 will I<Just Work>, while this:
485 subtype DateTime => as Object => where { $_->isa('DateTime') };
487 will fail silently and cause many headaches. The simple way to solve
488 this, as well as future proof your subtypes from classes which have
489 yet to have been created yet, is to simply do this:
492 subtype 'DateTime' => as 'Object' => where { $_->isa('DateTime') };
494 =head2 Default Type Constraints
496 This module also provides a simple hierarchy for Perl 5 types, this
497 could probably use some work, but it works for me at the moment.
520 Suggestions for improvement are welcome.
522 B<NOTE:> The C<Undef> type constraint does not work correctly
523 in every occasion, please use it sparringly.
525 B<NOTE:> The C<ClassName> type constraint is simply a subtype
526 of string which responds true to C<isa('UNIVERSAL')>. This means
527 that your class B<must> be loaded for this type constraint to
528 pass. I know this is not ideal for all, but it is a saner
529 restriction than most others.
531 =head2 Use with Other Constraint Modules
533 This module should play fairly nicely with other constraint
534 modules with only some slight tweaking. The C<where> clause
535 in types is expected to be a C<CODE> reference which checks
536 it's first argument and returns a bool. Since most constraint
537 modules work in a similar way, it should be simple to adapt
538 them to work with Moose.
540 For instance, this is how you could use it with
541 L<Declare::Constraints::Simple> to declare a completely new type.
543 type 'HashOfArrayOfObjects'
546 -values => IsArrayRef( IsObject ));
548 For more examples see the F<t/204_example_w_DCS.t> test file.
550 Here is an example of using L<Test::Deep> and it's non-test
551 related C<eq_deeply> function.
553 type 'ArrayOfHashOfBarsAndRandomNumbers'
556 array_each(subhashof({
558 random_number => ignore()
562 For a complete example see the F<t/205_example_w_TestDeep.t>
567 =head2 Type Constraint Construction & Locating
571 =item B<create_type_constraint_union ($pipe_seperated_types | @type_constraint_names)>
573 Given string with C<$pipe_seperated_types> or a list of C<@type_constraint_names>,
574 this will return a L<Moose::Meta::TypeConstraint::Union> instance.
576 =item B<create_parameterized_type_constraint ($type_name)>
578 Given a C<$type_name> in the form of:
580 BaseType[ContainerType]
582 this will extract the base type and container type and build an instance of
583 L<Moose::Meta::TypeConstraint::Parameterized> for it.
585 =item B<find_or_create_type_constraint ($type_name, ?$options_for_anon_type)>
587 This will attempt to find or create a type constraint given the a C<$type_name>.
588 If it cannot find it in the registry, it will see if it should be a union or
589 container type an create one if appropriate, and lastly if nothing can be
590 found or created that way, it will create an anon-type using the
591 C<$options_for_anon_type> HASH ref to populate it.
593 =item B<find_type_constraint ($type_name)>
595 This function can be used to locate a specific type constraint
596 meta-object, of the class L<Moose::Meta::TypeConstraint> or a
597 derivative. What you do with it from there is up to you :)
599 =item B<get_type_constraint_registry>
601 Fetch the L<Moose::Meta::TypeConstraint::Registry> object which
602 keeps track of all type constraints.
604 =item B<list_all_type_constraints>
606 This will return a list of type constraint names, you can then
607 fetch them using C<find_type_constraint ($type_name)> if you
610 =item B<list_all_builtin_type_constraints>
612 This will return a list of builtin type constraints, meaning,
613 those which are defined in this module. See the section
614 labeled L<Default Type Constraints> for a complete list.
616 =item B<export_type_constraints_as_functions>
618 This will export all the current type constraints as functions
619 into the caller's namespace. Right now, this is mostly used for
620 testing, but it might prove useful to others.
624 =head2 Type Constraint Constructors
626 The following functions are used to create type constraints.
627 They will then register the type constraints in a global store
628 where Moose can get to them if it needs to.
630 See the L<SYNOPSIS> for an example of how to use these.
634 =item B<type ($name, $where_clause)>
636 This creates a base type, which has no parent.
638 =item B<subtype ($name, $parent, $where_clause, ?$message)>
640 This creates a named subtype.
642 =item B<subtype ($parent, $where_clause, ?$message)>
644 This creates an unnamed subtype and will return the type
645 constraint meta-object, which will be an instance of
646 L<Moose::Meta::TypeConstraint>.
648 =item B<enum ($name, @values)>
650 This will create a basic subtype for a given set of strings.
651 The resulting constraint will be a subtype of C<Str> and
652 will match any of the items in C<@values>. See the L<SYNOPSIS>
653 for a simple example.
655 B<NOTE:> This is not a true proper enum type, it is simple
656 a convient constraint builder.
660 This is just sugar for the type constraint construction syntax.
664 This is just sugar for the type constraint construction syntax.
668 This is just sugar for the type constraint construction syntax.
672 This can be used to define a "hand optimized" version of your
673 type constraint which can be used to avoid traversing a subtype
674 constraint heirarchy.
676 B<NOTE:> You should only use this if you know what you are doing,
677 all the built in types use this, so your subtypes (assuming they
678 are shallow) will not likely need to use this.
682 =head2 Type Coercion Constructors
684 Type constraints can also contain type coercions as well. If you
685 ask your accessor to coerce, then Moose will run the type-coercion
686 code first, followed by the type constraint check. This feature
687 should be used carefully as it is very powerful and could easily
688 take off a limb if you are not careful.
690 See the L<SYNOPSIS> for an example of how to use these.
698 This is just sugar for the type coercion construction syntax.
702 This is just sugar for the type coercion construction syntax.
706 =head2 Namespace Management
712 This will remove all the type constraint keywords from the
713 calling class namespace.
719 All complex software has bugs lurking in it, and this module is no
720 exception. If you find a bug please either email me, or add the bug
725 Stevan Little E<lt>stevan@iinteractive.comE<gt>
727 =head1 COPYRIGHT AND LICENSE
729 Copyright 2006, 2007 by Infinity Interactive, Inc.
731 L<http://www.iinteractive.com>
733 This library is free software; you can redistribute it and/or modify
734 it under the same terms as Perl itself.