2 package Moose::Util::TypeConstraints;
8 use Scalar::Util 'blessed', 'reftype';
11 our $VERSION = '0.21';
12 our $AUTHORITY = 'cpan:STEVAN';
14 ## --------------------------------------------------------
15 # Prototyped subs must be predeclared because we have a
16 # circular dependency with Moose::Meta::Attribute et. al.
17 # so in case of us being use'd first the predeclaration
18 # ensures the prototypes are in scope when consumers are
21 # creation and location
22 sub find_type_constraint ($);
23 sub register_type_constraint ($);
24 sub find_or_create_type_constraint ($;$);
25 sub create_type_constraint_union (@);
26 sub create_parameterized_type_constraint ($);
27 sub create_class_type_constraint ($);
43 sub _create_type_constraint ($$$;$$);
44 sub _install_type_coercions ($$);
46 ## --------------------------------------------------------
48 use Moose::Meta::TypeConstraint;
49 use Moose::Meta::TypeConstraint::Union;
50 use Moose::Meta::TypeConstraint::Parameterized;
51 use Moose::Meta::TypeConstraint::Parameterizable;
52 use Moose::Meta::TypeCoercion;
53 use Moose::Meta::TypeCoercion::Union;
54 use Moose::Meta::TypeConstraint::Registry;
55 use Moose::Util::TypeConstraints::OptimizedConstraints;
58 type subtype class_type as where message optimize_as
62 register_type_constraint
65 Sub::Exporter::setup_exporter({
67 groups => { default => [':all'] }
73 # loop through the exports ...
74 foreach my $name (@exports) {
76 if (defined &{$class . '::' . $name}) {
77 my $keyword = \&{$class . '::' . $name};
79 # make sure it is from Moose
80 my ($pkg_name) = Class::MOP::get_code_info($keyword);
82 next if $pkg_name ne 'Moose::Util::TypeConstraints';
84 # and if it is from Moose then undef the slot
85 delete ${$class . '::'}{$name};
90 ## --------------------------------------------------------
91 ## type registry and some useful functions for it
92 ## --------------------------------------------------------
94 my $REGISTRY = Moose::Meta::TypeConstraint::Registry->new;
96 sub get_type_constraint_registry { $REGISTRY }
97 sub list_all_type_constraints { keys %{$REGISTRY->type_constraints} }
98 sub export_type_constraints_as_functions {
101 foreach my $constraint (keys %{$REGISTRY->type_constraints}) {
102 my $tc = $REGISTRY->get_type_constraint($constraint)->_compiled_type_constraint;
103 *{"${pkg}::${constraint}"} = sub { $tc->($_[0]) ? 1 : undef };
107 sub create_type_constraint_union (@) {
108 my @type_constraint_names;
110 if (scalar @_ == 1 && _detect_type_constraint_union($_[0])) {
111 @type_constraint_names = _parse_type_constraint_union($_[0]);
114 @type_constraint_names = @_;
117 (scalar @type_constraint_names >= 2)
118 || confess "You must pass in at least 2 type names to make a union";
120 ($REGISTRY->has_type_constraint($_))
121 || confess "Could not locate type constraint ($_) for the union"
122 foreach @type_constraint_names;
124 return Moose::Meta::TypeConstraint::Union->new(
125 type_constraints => [
127 $REGISTRY->get_type_constraint($_)
128 } @type_constraint_names
133 sub create_parameterized_type_constraint ($) {
134 my $type_constraint_name = shift;
136 my ($base_type, $type_parameter) = _parse_parameterized_type_constraint($type_constraint_name);
138 (defined $base_type && defined $type_parameter)
139 || confess "Could not parse type name ($type_constraint_name) correctly";
141 ($REGISTRY->has_type_constraint($base_type))
142 || confess "Could not locate the base type ($base_type)";
144 return Moose::Meta::TypeConstraint::Parameterized->new(
145 name => $type_constraint_name,
146 parent => $REGISTRY->get_type_constraint($base_type),
147 type_parameter => find_or_create_type_constraint(
149 parent => $REGISTRY->get_type_constraint('Object'),
150 constraint => sub { $_[0]->isa($type_parameter) }
156 sub create_class_type_constraint ($) {
159 # too early for this check
160 #find_type_constraint("ClassName")->check($class)
161 # || confess "Can't create a class type constraint because '$class' is not a class name";
163 Moose::Meta::TypeConstraint::Class->new( name => $class );
166 sub find_or_create_type_constraint ($;$) {
167 my ($type_constraint_name, $options_for_anon_type) = @_;
169 return $REGISTRY->get_type_constraint($type_constraint_name)
170 if $REGISTRY->has_type_constraint($type_constraint_name);
174 if (_detect_type_constraint_union($type_constraint_name)) {
175 $constraint = create_type_constraint_union($type_constraint_name);
177 elsif (_detect_parameterized_type_constraint($type_constraint_name)) {
178 $constraint = create_parameterized_type_constraint($type_constraint_name);
182 # if there is no $options_for_anon_type
183 # specified, then we assume they don't
184 # want to create one, and return nothing.
185 return unless defined $options_for_anon_type;
188 # otherwise assume that we should create
189 # an ANON type with the $options_for_anon_type
190 # options which can be passed in. It should
191 # be noted that these don't get registered
192 # so we need to return it.
194 return Moose::Meta::TypeConstraint->new(
196 %{$options_for_anon_type}
200 $REGISTRY->add_type_constraint($constraint);
204 ## --------------------------------------------------------
205 ## exported functions ...
206 ## --------------------------------------------------------
208 sub find_type_constraint ($) { $REGISTRY->get_type_constraint(@_) }
210 sub register_type_constraint ($) {
211 my $constraint = shift;
212 confess "can't register an unnamed type constraint" unless defined $constraint->name;
213 $REGISTRY->add_type_constraint($constraint);
219 splice(@_, 1, 0, undef);
220 goto &_create_type_constraint;
223 sub subtype ($$;$$$) {
225 # this adds an undef for the name
226 # if this is an anon-subtype:
227 # subtype(Num => where { $_ % 2 == 0 }) # anon 'even' subtype
228 # but if the last arg is not a code
229 # ref then it is a subtype alias:
230 # subtype(MyNumbers => as Num); # now MyNumbers is the same as Num
231 # ... yeah I know it's ugly code
233 unshift @_ => undef if scalar @_ <= 2 && (reftype($_[1]) || '') eq 'CODE';
234 goto &_create_type_constraint;
238 register_type_constraint( create_class_type_constraint(shift) );
242 my ($type_name, @coercion_map) = @_;
243 _install_type_coercions($type_name, \@coercion_map);
247 sub from ($) { $_[0] }
248 sub where (&) { $_[0] }
249 sub via (&) { $_[0] }
251 sub message (&) { +{ message => $_[0] } }
252 sub optimize_as (&) { +{ optimized => $_[0] } }
255 my ($type_name, @values) = @_;
256 (scalar @values >= 2)
257 || confess "You must have at least two values to enumerate through";
258 my %valid = map { $_ => 1 } @values;
259 _create_type_constraint(
266 ## --------------------------------------------------------
267 ## desugaring functions ...
268 ## --------------------------------------------------------
270 sub _create_type_constraint ($$$;$$) {
275 my ($message, $optimized);
277 $message = $_->{message} if exists $_->{message};
278 $optimized = $_->{optimized} if exists $_->{optimized};
281 my $pkg_defined_in = scalar(caller(0));
284 my $type = $REGISTRY->get_type_constraint($name);
286 ($type->_package_defined_in eq $pkg_defined_in)
287 || confess ("The type constraint '$name' has already been created in "
288 . $type->_package_defined_in . " and cannot be created again in "
293 $parent = find_or_create_type_constraint($parent) if defined $parent;
295 my $constraint = Moose::Meta::TypeConstraint->new(
296 name => $name || '__ANON__',
297 package_defined_in => $pkg_defined_in,
299 ($parent ? (parent => $parent ) : ()),
300 ($check ? (constraint => $check) : ()),
301 ($message ? (message => $message) : ()),
302 ($optimized ? (optimized => $optimized) : ()),
306 # if we have a type constraint union, and no
307 # type check, this means we are just aliasing
308 # the union constraint, which means we need to
309 # handle this differently.
311 if (not(defined $check)
312 && $parent->isa('Moose::Meta::TypeConstraint::Union')
313 && $parent->has_coercion
315 $constraint->coercion(Moose::Meta::TypeCoercion::Union->new(
316 type_constraint => $parent
320 $REGISTRY->add_type_constraint($constraint)
326 sub _install_type_coercions ($$) {
327 my ($type_name, $coercion_map) = @_;
328 my $type = $REGISTRY->get_type_constraint($type_name);
330 || confess "Cannot find type '$type_name', perhaps you forgot to load it.";
331 if ($type->has_coercion) {
332 $type->coercion->add_type_coercions(@$coercion_map);
335 my $type_coercion = Moose::Meta::TypeCoercion->new(
336 type_coercion_map => $coercion_map,
337 type_constraint => $type
339 $type->coercion($type_coercion);
343 ## --------------------------------------------------------
344 ## type notation parsing ...
345 ## --------------------------------------------------------
348 # All I have to say is mugwump++ cause I know
349 # do not even have enough regexp-fu to be able
350 # to have written this (I can only barely
351 # understand it as it is)
356 my $valid_chars = qr{[\w:]};
357 my $type_atom = qr{ $valid_chars+ };
359 my $type = qr{ $valid_chars+ (?: \[ (??{$any}) \] )? }x;
360 my $type_capture_parts = qr{ ($valid_chars+) (?: \[ ((??{$any})) \] )? }x;
361 my $type_with_parameter = qr{ $valid_chars+ \[ (??{$any}) \] }x;
363 my $op_union = qr{ \s* \| \s* }x;
364 my $union = qr{ $type (?: $op_union $type )+ }x;
366 our $any = qr{ $type | $union }x;
368 sub _parse_parameterized_type_constraint {
369 $_[0] =~ m{ $type_capture_parts }x;
373 sub _detect_parameterized_type_constraint {
374 $_[0] =~ m{ ^ $type_with_parameter $ }x;
377 sub _parse_type_constraint_union {
380 while ( $given =~ m{ \G (?: $op_union )? ($type) }gcx ) {
383 (pos($given) eq length($given))
384 || confess "'$given' didn't parse (parse-pos="
392 sub _detect_type_constraint_union {
393 $_[0] =~ m{^ $type $op_union $type ( $op_union .* )? $}x;
397 ## --------------------------------------------------------
398 # define some basic built-in types
399 ## --------------------------------------------------------
401 type 'Any' => where { 1 }; # meta-type including all
402 type 'Item' => where { 1 }; # base-type
404 subtype 'Undef' => as 'Item' => where { !defined($_) };
405 subtype 'Defined' => as 'Item' => where { defined($_) };
409 => where { !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' };
413 => where { !ref($_) }
414 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Value;
419 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Ref;
424 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Str;
428 => where { Scalar::Util::looks_like_number($_) }
429 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Num;
433 => where { "$_" =~ /^-?[0-9]+$/ }
434 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Int;
436 subtype 'ScalarRef' => as 'Ref' => where { ref($_) eq 'SCALAR' } => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::ScalarRef;
437 subtype 'CodeRef' => as 'Ref' => where { ref($_) eq 'CODE' } => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::CodeRef;
438 subtype 'RegexpRef' => as 'Ref' => where { ref($_) eq 'Regexp' } => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::RegexpRef;
439 subtype 'GlobRef' => as 'Ref' => where { ref($_) eq 'GLOB' } => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::GlobRef;
442 # scalar filehandles are GLOB refs,
443 # but a GLOB ref is not always a filehandle
446 => where { Scalar::Util::openhandle($_) || ( blessed($_) && $_->isa("IO::Handle") ) }
447 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::FileHandle;
450 # blessed(qr/.../) returns true,.. how odd
453 => where { blessed($_) && blessed($_) ne 'Regexp' }
454 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Object;
458 => where { $_->can('does') }
459 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Role;
461 my $_class_name_checker = sub {
462 return if ref($_[0]);
463 return unless defined($_[0]) && length($_[0]);
465 # walk the symbol table tree to avoid autovififying
466 # \*{${main::}{"Foo::"}} == \*main::Foo::
469 foreach my $part (split('::', $_[0])) {
470 return unless exists ${$$pack}{"${part}::"};
471 $pack = \*{${$$pack}{"${part}::"}};
474 # check for $VERSION or @ISA
475 return 1 if exists ${$$pack}{VERSION}
476 && defined *{${$$pack}{VERSION}}{SCALAR};
477 return 1 if exists ${$$pack}{ISA}
478 && defined *{${$$pack}{ISA}}{ARRAY};
480 # check for any method
481 foreach ( keys %{$$pack} ) {
482 next if substr($_, -2, 2) eq '::';
483 return 1 if defined *{${$$pack}{$_}}{CODE};
492 => $_class_name_checker # where ...
493 => { optimize => $_class_name_checker };
495 ## --------------------------------------------------------
496 # parameterizable types ...
498 $REGISTRY->add_type_constraint(
499 Moose::Meta::TypeConstraint::Parameterizable->new(
501 package_defined_in => __PACKAGE__,
502 parent => find_type_constraint('Ref'),
503 constraint => sub { ref($_) eq 'ARRAY' },
504 optimized => \&Moose::Util::TypeConstraints::OptimizedConstraints::ArrayRef,
505 constraint_generator => sub {
506 my $type_parameter = shift;
508 foreach my $x (@$_) {
509 ($type_parameter->check($x)) || return
516 $REGISTRY->add_type_constraint(
517 Moose::Meta::TypeConstraint::Parameterizable->new(
519 package_defined_in => __PACKAGE__,
520 parent => find_type_constraint('Ref'),
521 constraint => sub { ref($_) eq 'HASH' },
522 optimized => \&Moose::Util::TypeConstraints::OptimizedConstraints::HashRef,
523 constraint_generator => sub {
524 my $type_parameter = shift;
526 foreach my $x (values %$_) {
527 ($type_parameter->check($x)) || return
534 $REGISTRY->add_type_constraint(
535 Moose::Meta::TypeConstraint::Parameterizable->new(
537 package_defined_in => __PACKAGE__,
538 parent => find_type_constraint('Item'),
539 constraint => sub { 1 },
540 constraint_generator => sub {
541 my $type_parameter = shift;
543 return 1 if not(defined($_)) || $type_parameter->check($_);
550 my @PARAMETERIZABLE_TYPES = map {
551 $REGISTRY->get_type_constraint($_)
552 } qw[ArrayRef HashRef Maybe];
554 sub get_all_parameterizable_types { @PARAMETERIZABLE_TYPES }
555 sub add_parameterizable_type {
557 (blessed $type && $type->isa('Moose::Meta::TypeConstraint::Parameterizable'))
558 || confess "Type must be a Moose::Meta::TypeConstraint::Parameterizable not $type";
559 push @PARAMETERIZABLE_TYPES => $type;
562 ## --------------------------------------------------------
563 # end of built-in types ...
564 ## --------------------------------------------------------
567 my @BUILTINS = list_all_type_constraints();
568 sub list_all_builtin_type_constraints { @BUILTINS }
579 Moose::Util::TypeConstraints - Type constraint system for Moose
583 use Moose::Util::TypeConstraints;
585 type 'Num' => where { Scalar::Util::looks_like_number($_) };
591 subtype 'NaturalLessThanTen'
594 => message { "This number ($_) is not less than ten!" };
600 enum 'RGBColors' => qw(red green blue);
604 This module provides Moose with the ability to create custom type
605 contraints to be used in attribute definition.
607 =head2 Important Caveat
609 This is B<NOT> a type system for Perl 5. These are type constraints,
610 and they are not used by Moose unless you tell it to. No type
611 inference is performed, expression are not typed, etc. etc. etc.
613 This is simply a means of creating small constraint functions which
614 can be used to simplify your own type-checking code.
616 =head2 Slightly Less Important Caveat
618 It is almost always a good idea to quote your type and subtype names.
619 This is to prevent perl from trying to execute the call as an indirect
620 object call. This issue only seems to come up when you have a subtype
621 the same name as a valid class, but when the issue does arise it tends
622 to be quite annoying to debug.
624 So for instance, this:
626 subtype DateTime => as Object => where { $_->isa('DateTime') };
628 will I<Just Work>, while this:
631 subtype DateTime => as Object => where { $_->isa('DateTime') };
633 will fail silently and cause many headaches. The simple way to solve
634 this, as well as future proof your subtypes from classes which have
635 yet to have been created yet, is to simply do this:
638 subtype 'DateTime' => as 'Object' => where { $_->isa('DateTime') };
640 =head2 Default Type Constraints
642 This module also provides a simple hierarchy for Perl 5 types, this
643 could probably use some work, but it works for me at the moment.
667 Suggestions for improvement are welcome.
669 B<NOTE:> Any type followed by a type parameter C<[`a]> can be
670 parameterized, this means you can say:
672 ArrayRef[Int] # an array of intergers
673 HashRef[CodeRef] # a hash of str to CODE ref mappings
674 Maybe[Str] # value may be a string, may be undefined
676 B<NOTE:> The C<Undef> type constraint for the most part works
677 correctly now, but edge cases may still exist, please use it
680 B<NOTE:> The C<ClassName> type constraint does a complex package
681 existence check. This means that your class B<must> be loaded for
682 this type constraint to pass. I know this is not ideal for all,
683 but it is a saner restriction than most others.
685 =head2 Use with Other Constraint Modules
687 This module should play fairly nicely with other constraint
688 modules with only some slight tweaking. The C<where> clause
689 in types is expected to be a C<CODE> reference which checks
690 it's first argument and returns a bool. Since most constraint
691 modules work in a similar way, it should be simple to adapt
692 them to work with Moose.
694 For instance, this is how you could use it with
695 L<Declare::Constraints::Simple> to declare a completely new type.
697 type 'HashOfArrayOfObjects'
700 -values => IsArrayRef( IsObject ));
702 For more examples see the F<t/204_example_w_DCS.t> test file.
704 Here is an example of using L<Test::Deep> and it's non-test
705 related C<eq_deeply> function.
707 type 'ArrayOfHashOfBarsAndRandomNumbers'
710 array_each(subhashof({
712 random_number => ignore()
716 For a complete example see the F<t/205_example_w_TestDeep.t>
721 =head2 Type Constraint Construction & Locating
725 =item B<create_type_constraint_union ($pipe_seperated_types | @type_constraint_names)>
727 Given string with C<$pipe_seperated_types> or a list of C<@type_constraint_names>,
728 this will return a L<Moose::Meta::TypeConstraint::Union> instance.
730 =item B<create_parameterized_type_constraint ($type_name)>
732 Given a C<$type_name> in the form of:
734 BaseType[ContainerType]
736 this will extract the base type and container type and build an instance of
737 L<Moose::Meta::TypeConstraint::Parameterized> for it.
739 =item B<create_class_type_constraint ($class)>
741 Given a class name it will create a new L<Moose::Meta::TypeConstraint::Class>
742 object for that class name.
744 =item B<find_or_create_type_constraint ($type_name, ?$options_for_anon_type)>
746 This will attempt to find or create a type constraint given the a C<$type_name>.
747 If it cannot find it in the registry, it will see if it should be a union or
748 container type an create one if appropriate, and lastly if nothing can be
749 found or created that way, it will create an anon-type using the
750 C<$options_for_anon_type> HASH ref to populate it. If the C<$options_for_anon_type>
751 is not specified (it is C<undef>), then it will not create anything and simply
754 =item B<find_type_constraint ($type_name)>
756 This function can be used to locate a specific type constraint
757 meta-object, of the class L<Moose::Meta::TypeConstraint> or a
758 derivative. What you do with it from there is up to you :)
760 =item B<register_type_constraint ($type_object)>
762 This function will register a named type constraint with the type registry.
764 =item B<get_type_constraint_registry>
766 Fetch the L<Moose::Meta::TypeConstraint::Registry> object which
767 keeps track of all type constraints.
769 =item B<list_all_type_constraints>
771 This will return a list of type constraint names, you can then
772 fetch them using C<find_type_constraint ($type_name)> if you
775 =item B<list_all_builtin_type_constraints>
777 This will return a list of builtin type constraints, meaning,
778 those which are defined in this module. See the section
779 labeled L<Default Type Constraints> for a complete list.
781 =item B<export_type_constraints_as_functions>
783 This will export all the current type constraints as functions
784 into the caller's namespace. Right now, this is mostly used for
785 testing, but it might prove useful to others.
787 =item B<get_all_parameterizable_types>
789 This returns all the parameterizable types that have been registered.
791 =item B<add_parameterizable_type ($type)>
793 Adds C<$type> to the list of parameterizable types
797 =head2 Type Constraint Constructors
799 The following functions are used to create type constraints.
800 They will then register the type constraints in a global store
801 where Moose can get to them if it needs to.
803 See the L<SYNOPSIS> for an example of how to use these.
807 =item B<type ($name, $where_clause)>
809 This creates a base type, which has no parent.
811 =item B<subtype ($name, $parent, $where_clause, ?$message)>
813 This creates a named subtype.
815 =item B<subtype ($parent, $where_clause, ?$message)>
817 This creates an unnamed subtype and will return the type
818 constraint meta-object, which will be an instance of
819 L<Moose::Meta::TypeConstraint>.
821 =item B<class_type ($class)>
823 Creates a type constraint with the name C<$class> and the metaclass
824 L<Moose::Meta::TypeConstraint::Class>.
826 =item B<enum ($name, @values)>
828 This will create a basic subtype for a given set of strings.
829 The resulting constraint will be a subtype of C<Str> and
830 will match any of the items in C<@values>. It is case sensitive.
831 See the L<SYNOPSIS> for a simple example.
833 B<NOTE:> This is not a true proper enum type, it is simple
834 a convient constraint builder.
838 This is just sugar for the type constraint construction syntax.
842 This is just sugar for the type constraint construction syntax.
846 This is just sugar for the type constraint construction syntax.
850 This can be used to define a "hand optimized" version of your
851 type constraint which can be used to avoid traversing a subtype
852 constraint heirarchy.
854 B<NOTE:> You should only use this if you know what you are doing,
855 all the built in types use this, so your subtypes (assuming they
856 are shallow) will not likely need to use this.
860 =head2 Type Coercion Constructors
862 Type constraints can also contain type coercions as well. If you
863 ask your accessor to coerce, then Moose will run the type-coercion
864 code first, followed by the type constraint check. This feature
865 should be used carefully as it is very powerful and could easily
866 take off a limb if you are not careful.
868 See the L<SYNOPSIS> for an example of how to use these.
876 This is just sugar for the type coercion construction syntax.
880 This is just sugar for the type coercion construction syntax.
884 =head2 Namespace Management
890 This will remove all the type constraint keywords from the
891 calling class namespace.
897 All complex software has bugs lurking in it, and this module is no
898 exception. If you find a bug please either email me, or add the bug
903 Stevan Little E<lt>stevan@iinteractive.comE<gt>
905 =head1 COPYRIGHT AND LICENSE
907 Copyright 2006-2008 by Infinity Interactive, Inc.
909 L<http://www.iinteractive.com>
911 This library is free software; you can redistribute it and/or modify
912 it under the same terms as Perl itself.