2 package Moose::Util::TypeConstraints;
8 use List::MoreUtils qw( all );
9 use Scalar::Util 'blessed';
12 our $VERSION = '0.57';
13 $VERSION = eval $VERSION;
14 our $AUTHORITY = 'cpan:STEVAN';
16 ## --------------------------------------------------------
17 # Prototyped subs must be predeclared because we have a
18 # circular dependency with Moose::Meta::Attribute et. al.
19 # so in case of us being use'd first the predeclaration
20 # ensures the prototypes are in scope when consumers are
23 # creation and location
24 sub find_type_constraint ($);
25 sub register_type_constraint ($);
26 sub find_or_create_type_constraint ($;$);
27 sub find_or_parse_type_constraint ($);
28 sub find_or_create_isa_type_constraint ($);
29 sub find_or_create_does_type_constraint ($);
30 sub create_type_constraint_union (@);
31 sub create_parameterized_type_constraint ($);
32 sub create_class_type_constraint ($;$);
33 sub create_role_type_constraint ($;$);
34 sub create_enum_type_constraint ($$);
50 sub _create_type_constraint ($$$;$$);
51 sub _install_type_coercions ($$);
53 ## --------------------------------------------------------
55 use Moose::Meta::TypeConstraint;
56 use Moose::Meta::TypeConstraint::Union;
57 use Moose::Meta::TypeConstraint::Parameterized;
58 use Moose::Meta::TypeConstraint::Parameterizable;
59 use Moose::Meta::TypeConstraint::Class;
60 use Moose::Meta::TypeConstraint::Role;
61 use Moose::Meta::TypeConstraint::Enum;
62 use Moose::Meta::TypeCoercion;
63 use Moose::Meta::TypeCoercion::Union;
64 use Moose::Meta::TypeConstraint::Registry;
65 use Moose::Util::TypeConstraints::OptimizedConstraints;
67 Moose::Exporter->setup_import_methods(
70 type subtype class_type role_type as where message optimize_as
74 register_type_constraint )
79 ## --------------------------------------------------------
80 ## type registry and some useful functions for it
81 ## --------------------------------------------------------
83 my $REGISTRY = Moose::Meta::TypeConstraint::Registry->new;
85 sub get_type_constraint_registry { $REGISTRY }
86 sub list_all_type_constraints { keys %{$REGISTRY->type_constraints} }
87 sub export_type_constraints_as_functions {
90 foreach my $constraint (keys %{$REGISTRY->type_constraints}) {
91 my $tc = $REGISTRY->get_type_constraint($constraint)->_compiled_type_constraint;
92 *{"${pkg}::${constraint}"} = sub { $tc->($_[0]) ? 1 : undef }; # the undef is for compat
96 sub create_type_constraint_union (@) {
97 my @type_constraint_names;
99 if (scalar @_ == 1 && _detect_type_constraint_union($_[0])) {
100 @type_constraint_names = _parse_type_constraint_union($_[0]);
103 @type_constraint_names = @_;
106 (scalar @type_constraint_names >= 2)
107 || Moose->throw_error("You must pass in at least 2 type names to make a union");
109 my @type_constraints = sort {$a->name cmp $b->name} map {
110 find_or_parse_type_constraint($_) ||
111 Moose->throw_error("Could not locate type constraint ($_) for the union");
112 } @type_constraint_names;
114 return Moose::Meta::TypeConstraint::Union->new(
115 type_constraints => \@type_constraints
119 sub create_parameterized_type_constraint ($) {
120 my $type_constraint_name = shift;
121 my ($base_type, $type_parameter) = _parse_parameterized_type_constraint($type_constraint_name);
123 (defined $base_type && defined $type_parameter)
124 || Moose->throw_error("Could not parse type name ($type_constraint_name) correctly");
126 if ($REGISTRY->has_type_constraint($base_type)) {
127 my $base_type_tc = $REGISTRY->get_type_constraint($base_type);
128 return _create_parameterized_type_constraint(
133 Moose->throw_error("Could not locate the base type ($base_type)");
137 sub _create_parameterized_type_constraint {
138 my ( $base_type_tc, $type_parameter ) = @_;
139 if ( $base_type_tc->can('parameterize') ) {
140 return $base_type_tc->parameterize($type_parameter);
143 return Moose::Meta::TypeConstraint::Parameterized->new(
144 name => $base_type_tc->name . '[' . $type_parameter . ']',
145 parent => $base_type_tc,
147 find_or_create_isa_type_constraint($type_parameter),
152 #should we also support optimized checks?
153 sub create_class_type_constraint ($;$) {
154 my ( $class, $options ) = @_;
156 # too early for this check
157 #find_type_constraint("ClassName")->check($class)
158 # || Moose->throw_error("Can't create a class type constraint because '$class' is not a class name");
166 $options{name} ||= "__ANON__";
168 Moose::Meta::TypeConstraint::Class->new( %options );
171 sub create_role_type_constraint ($;$) {
172 my ( $role, $options ) = @_;
174 # too early for this check
175 #find_type_constraint("ClassName")->check($class)
176 # || Moose->throw_error("Can't create a class type constraint because '$class' is not a class name");
184 $options{name} ||= "__ANON__";
186 Moose::Meta::TypeConstraint::Role->new( %options );
190 sub find_or_create_type_constraint ($;$) {
191 my ( $type_constraint_name, $options_for_anon_type ) = @_;
193 if ( my $constraint = find_or_parse_type_constraint($type_constraint_name) ) {
196 elsif ( defined $options_for_anon_type ) {
198 # if there is no $options_for_anon_type
199 # specified, then we assume they don't
200 # want to create one, and return nothing.
202 # otherwise assume that we should create
203 # an ANON type with the $options_for_anon_type
204 # options which can be passed in. It should
205 # be noted that these don't get registered
206 # so we need to return it.
208 return Moose::Meta::TypeConstraint->new(
210 %{$options_for_anon_type}
217 sub find_or_create_isa_type_constraint ($) {
218 my $type_constraint_name = shift;
219 find_or_parse_type_constraint($type_constraint_name) || create_class_type_constraint($type_constraint_name)
222 sub find_or_create_does_type_constraint ($) {
223 my $type_constraint_name = shift;
224 find_or_parse_type_constraint($type_constraint_name) || create_role_type_constraint($type_constraint_name)
227 sub find_or_parse_type_constraint ($) {
228 my $type_constraint_name = normalize_type_constraint_name(shift);
231 if ($constraint = find_type_constraint($type_constraint_name)) {
233 } elsif (_detect_type_constraint_union($type_constraint_name)) {
234 $constraint = create_type_constraint_union($type_constraint_name);
235 } elsif (_detect_parameterized_type_constraint($type_constraint_name)) {
236 $constraint = create_parameterized_type_constraint($type_constraint_name);
241 $REGISTRY->add_type_constraint($constraint);
245 sub normalize_type_constraint_name {
246 my $type_constraint_name = shift @_;
247 $type_constraint_name =~ s/\s//g;
248 return $type_constraint_name;
254 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
255 Carp::confess($error);
258 ## --------------------------------------------------------
259 ## exported functions ...
260 ## --------------------------------------------------------
262 sub find_type_constraint ($) {
265 if ( blessed $type and $type->isa("Moose::Meta::TypeConstraint") ) {
269 return unless $REGISTRY->has_type_constraint($type);
270 return $REGISTRY->get_type_constraint($type);
274 sub register_type_constraint ($) {
275 my $constraint = shift;
276 Moose->throw_error("can't register an unnamed type constraint") unless defined $constraint->name;
277 $REGISTRY->add_type_constraint($constraint);
284 splice(@_, 1, 0, undef);
285 goto &_create_type_constraint;
288 sub subtype ($$;$$$) {
290 # this adds an undef for the name
291 # if this is an anon-subtype:
292 # subtype(Num => where { $_ % 2 == 0 }) # anon 'even' subtype
294 # subtype(Num => where { $_ % 2 == 0 }) message { "$_ must be an even number" }
296 # but if the last arg is not a code ref then it is a subtype
299 # subtype(MyNumbers => as Num); # now MyNumbers is the same as Num
300 # ... yeah I know it's ugly code
302 unshift @_ => undef if scalar @_ == 2 && ( 'CODE' eq ref( $_[-1] ) );
304 if scalar @_ == 3 && all { ref($_) =~ /^(?:CODE|HASH)$/ } @_[ 1, 2 ];
305 goto &_create_type_constraint;
308 sub class_type ($;$) {
309 register_type_constraint(
310 create_class_type_constraint(
312 ( defined($_[1]) ? $_[1] : () ),
317 sub role_type ($;$) {
318 register_type_constraint(
319 create_role_type_constraint(
321 ( defined($_[1]) ? $_[1] : () ),
327 my ($type_name, @coercion_map) = @_;
328 _install_type_coercions($type_name, \@coercion_map);
332 sub from ($) { $_[0] }
333 sub where (&) { $_[0] }
334 sub via (&) { $_[0] }
336 sub message (&) { +{ message => $_[0] } }
337 sub optimize_as (&) { +{ optimized => $_[0] } }
340 my ($type_name, @values) = @_;
342 # if only an array-ref is passed then
343 # you get an anon-enum
345 if (ref $type_name eq 'ARRAY' && !@values) {
346 @values = @$type_name;
349 (scalar @values >= 2)
350 || Moose->throw_error("You must have at least two values to enumerate through");
351 my %valid = map { $_ => 1 } @values;
353 register_type_constraint(
354 create_enum_type_constraint(
361 sub create_enum_type_constraint ($$) {
362 my ( $type_name, $values ) = @_;
364 Moose::Meta::TypeConstraint::Enum->new(
365 name => $type_name || '__ANON__',
370 ## --------------------------------------------------------
371 ## desugaring functions ...
372 ## --------------------------------------------------------
374 sub _create_type_constraint ($$$;$$) {
379 my ($message, $optimized);
381 $message = $_->{message} if exists $_->{message};
382 $optimized = $_->{optimized} if exists $_->{optimized};
385 my $pkg_defined_in = scalar(caller(0));
388 my $type = $REGISTRY->get_type_constraint($name);
390 ( $type->_package_defined_in eq $pkg_defined_in )
392 "The type constraint '$name' has already been created in "
393 . $type->_package_defined_in
394 . " and cannot be created again in "
399 my $class = "Moose::Meta::TypeConstraint";
401 # FIXME should probably not be a special case
402 if ( defined $parent and $parent = find_or_parse_type_constraint($parent) ) {
403 $class = "Moose::Meta::TypeConstraint::Parameterizable"
404 if $parent->isa("Moose::Meta::TypeConstraint::Parameterizable");
407 my $constraint = $class->new(
408 name => $name || '__ANON__',
409 package_defined_in => $pkg_defined_in,
411 ($parent ? (parent => $parent ) : ()),
412 ($check ? (constraint => $check) : ()),
413 ($message ? (message => $message) : ()),
414 ($optimized ? (optimized => $optimized) : ()),
418 # if we have a type constraint union, and no
419 # type check, this means we are just aliasing
420 # the union constraint, which means we need to
421 # handle this differently.
423 if (not(defined $check)
424 && $parent->isa('Moose::Meta::TypeConstraint::Union')
425 && $parent->has_coercion
427 $constraint->coercion(Moose::Meta::TypeCoercion::Union->new(
428 type_constraint => $parent
432 $REGISTRY->add_type_constraint($constraint)
438 sub _install_type_coercions ($$) {
439 my ($type_name, $coercion_map) = @_;
440 my $type = find_type_constraint($type_name);
442 || Moose->throw_error("Cannot find type '$type_name', perhaps you forgot to load it.");
443 if ($type->has_coercion) {
444 $type->coercion->add_type_coercions(@$coercion_map);
447 my $type_coercion = Moose::Meta::TypeCoercion->new(
448 type_coercion_map => $coercion_map,
449 type_constraint => $type
451 $type->coercion($type_coercion);
455 ## --------------------------------------------------------
456 ## type notation parsing ...
457 ## --------------------------------------------------------
460 # All I have to say is mugwump++ cause I know
461 # do not even have enough regexp-fu to be able
462 # to have written this (I can only barely
463 # understand it as it is)
468 my $valid_chars = qr{[\w:]};
469 my $type_atom = qr{ $valid_chars+ };
473 my $type = qr{ $valid_chars+ (?: \[ \s* (??{$any}) \s* \] )? }x;
474 my $type_capture_parts = qr{ ($valid_chars+) (?: \[ \s* ((??{$any})) \s* \] )? }x;
475 my $type_with_parameter = qr{ $valid_chars+ \[ \s* (??{$any}) \s* \] }x;
477 my $op_union = qr{ \s* \| \s* }x;
478 my $union = qr{ $type (?: $op_union $type )+ }x;
480 ## New Stuff for structured types.
482 my $indirection = qr{=>};
483 my $divider_ops = qr{ $comma | $indirection }x;
484 my $structure_divider = qr{\s* $divider_ops \s*}x;
485 my $structure_elements = qr{ ($type $structure_divider*)+ }x;
487 $any = qr{ $type | $union | $structure_elements }x;
489 sub _parse_parameterized_type_constraint {
490 { no warnings 'void'; $any; } # force capture of interpolated lexical
491 my($base, $elements) = ($_[0] =~ m{ $type_capture_parts }x);
492 return ($base,$elements);
495 sub _detect_parameterized_type_constraint {
496 { no warnings 'void'; $any; } # force capture of interpolated lexical
497 $_[0] =~ m{ ^ $type_with_parameter $ }x;
500 sub _parse_type_constraint_union {
501 { no warnings 'void'; $any; } # force capture of interpolated lexical
504 while ( $given =~ m{ \G (?: $op_union )? ($type) }gcx ) {
507 (pos($given) eq length($given))
508 || Moose->throw_error("'$given' didn't parse (parse-pos="
516 sub _detect_type_constraint_union {
517 { no warnings 'void'; $any; } # force capture of interpolated lexical
518 $_[0] =~ m{^ $type $op_union $type ( $op_union .* )? $}x;
522 ## --------------------------------------------------------
523 # define some basic built-in types
524 ## --------------------------------------------------------
526 type 'Any' => where { 1 }; # meta-type including all
527 type 'Item' => where { 1 }; # base-type
529 subtype 'Undef' => as 'Item' => where { !defined($_) };
530 subtype 'Defined' => as 'Item' => where { defined($_) };
534 => where { !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' };
538 => where { !ref($_) }
539 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Value;
544 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Ref;
549 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Str;
553 => where { Scalar::Util::looks_like_number($_) }
554 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Num;
558 => where { "$_" =~ /^-?[0-9]+$/ }
559 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Int;
561 subtype 'ScalarRef' => as 'Ref' => where { ref($_) eq 'SCALAR' } => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::ScalarRef;
562 subtype 'CodeRef' => as 'Ref' => where { ref($_) eq 'CODE' } => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::CodeRef;
563 subtype 'RegexpRef' => as 'Ref' => where { ref($_) eq 'Regexp' } => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::RegexpRef;
564 subtype 'GlobRef' => as 'Ref' => where { ref($_) eq 'GLOB' } => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::GlobRef;
567 # scalar filehandles are GLOB refs,
568 # but a GLOB ref is not always a filehandle
571 => where { Scalar::Util::openhandle($_) || ( blessed($_) && $_->isa("IO::Handle") ) }
572 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::FileHandle;
575 # blessed(qr/.../) returns true,.. how odd
578 => where { blessed($_) && blessed($_) ne 'Regexp' }
579 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Object;
583 => where { $_->can('does') }
584 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Role;
586 my $_class_name_checker = sub {
591 => where { Class::MOP::is_class_loaded($_) }
592 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::ClassName;
594 ## --------------------------------------------------------
595 # parameterizable types ...
597 $REGISTRY->add_type_constraint(
598 Moose::Meta::TypeConstraint::Parameterizable->new(
600 package_defined_in => __PACKAGE__,
601 parent => find_type_constraint('Ref'),
602 constraint => sub { ref($_) eq 'ARRAY' },
603 optimized => \&Moose::Util::TypeConstraints::OptimizedConstraints::ArrayRef,
604 constraint_generator => sub {
605 my $type_parameter = shift;
606 my $check = $type_parameter->_compiled_type_constraint;
608 foreach my $x (@$_) {
609 ($check->($x)) || return
616 $REGISTRY->add_type_constraint(
617 Moose::Meta::TypeConstraint::Parameterizable->new(
619 package_defined_in => __PACKAGE__,
620 parent => find_type_constraint('Ref'),
621 constraint => sub { ref($_) eq 'HASH' },
622 optimized => \&Moose::Util::TypeConstraints::OptimizedConstraints::HashRef,
623 constraint_generator => sub {
624 my $type_parameter = shift;
625 my $check = $type_parameter->_compiled_type_constraint;
627 foreach my $x (values %$_) {
628 ($check->($x)) || return
635 $REGISTRY->add_type_constraint(
636 Moose::Meta::TypeConstraint::Parameterizable->new(
638 package_defined_in => __PACKAGE__,
639 parent => find_type_constraint('Item'),
640 constraint => sub { 1 },
641 constraint_generator => sub {
642 my $type_parameter = shift;
643 my $check = $type_parameter->_compiled_type_constraint;
645 return 1 if not(defined($_)) || $check->($_);
652 my @PARAMETERIZABLE_TYPES = map {
653 $REGISTRY->get_type_constraint($_)
654 } qw[ArrayRef HashRef Maybe];
656 sub get_all_parameterizable_types { @PARAMETERIZABLE_TYPES }
657 sub add_parameterizable_type {
659 (blessed $type && $type->isa('Moose::Meta::TypeConstraint::Parameterizable'))
660 || Moose->throw_error("Type must be a Moose::Meta::TypeConstraint::Parameterizable not $type");
661 push @PARAMETERIZABLE_TYPES => $type;
664 ## --------------------------------------------------------
665 # end of built-in types ...
666 ## --------------------------------------------------------
669 my @BUILTINS = list_all_type_constraints();
670 sub list_all_builtin_type_constraints { @BUILTINS }
681 Moose::Util::TypeConstraints - Type constraint system for Moose
685 use Moose::Util::TypeConstraints;
687 type 'Num' => where { Scalar::Util::looks_like_number($_) };
693 subtype 'NaturalLessThanTen'
696 => message { "This number ($_) is not less than ten!" };
702 enum 'RGBColors' => qw(red green blue);
706 This module provides Moose with the ability to create custom type
707 contraints to be used in attribute definition.
709 =head2 Important Caveat
711 This is B<NOT> a type system for Perl 5. These are type constraints,
712 and they are not used by Moose unless you tell it to. No type
713 inference is performed, expression are not typed, etc. etc. etc.
715 This is simply a means of creating small constraint functions which
716 can be used to simplify your own type-checking code, with the added
717 side benefit of making your intentions clearer through self-documentation.
719 =head2 Slightly Less Important Caveat
721 It is B<always> a good idea to quote your type and subtype names.
723 This is to prevent perl from trying to execute the call as an indirect
724 object call. This issue only seems to come up when you have a subtype
725 the same name as a valid class, but when the issue does arise it tends
726 to be quite annoying to debug.
728 So for instance, this:
730 subtype DateTime => as Object => where { $_->isa('DateTime') };
732 will I<Just Work>, while this:
735 subtype DateTime => as Object => where { $_->isa('DateTime') };
737 will fail silently and cause many headaches. The simple way to solve
738 this, as well as future proof your subtypes from classes which have
739 yet to have been created yet, is to simply do this:
742 subtype 'DateTime' => as 'Object' => where { $_->isa('DateTime') };
744 =head2 Default Type Constraints
746 This module also provides a simple hierarchy for Perl 5 types, here is
747 that hierarchy represented visually.
771 B<NOTE:> Any type followed by a type parameter C<[`a]> can be
772 parameterized, this means you can say:
774 ArrayRef[Int] # an array of intergers
775 HashRef[CodeRef] # a hash of str to CODE ref mappings
776 Maybe[Str] # value may be a string, may be undefined
778 B<NOTE:> Unless you parameterize a type, then it is invalid to
779 include the square brackets. I.e. C<ArrayRef[]> will be
780 literally interpreted as a type name.
782 B<NOTE:> The C<Undef> type constraint for the most part works
783 correctly now, but edge cases may still exist, please use it
786 B<NOTE:> The C<ClassName> type constraint does a complex package
787 existence check. This means that your class B<must> be loaded for
788 this type constraint to pass. I know this is not ideal for all,
789 but it is a saner restriction than most others.
791 =head2 Type Constraint Naming
793 Since the types created by this module are global, it is suggested
794 that you namespace your types just as you would namespace your
795 modules. So instead of creating a I<Color> type for your B<My::Graphics>
796 module, you would call the type I<My::Graphics::Color> instead.
798 =head2 Use with Other Constraint Modules
800 This module should play fairly nicely with other constraint
801 modules with only some slight tweaking. The C<where> clause
802 in types is expected to be a C<CODE> reference which checks
803 it's first argument and returns a boolean. Since most constraint
804 modules work in a similar way, it should be simple to adapt
805 them to work with Moose.
807 For instance, this is how you could use it with
808 L<Declare::Constraints::Simple> to declare a completely new type.
810 type 'HashOfArrayOfObjects'
813 -values => IsArrayRef( IsObject ));
815 For more examples see the F<t/200_examples/204_example_w_DCS.t>
818 Here is an example of using L<Test::Deep> and it's non-test
819 related C<eq_deeply> function.
821 type 'ArrayOfHashOfBarsAndRandomNumbers'
824 array_each(subhashof({
826 random_number => ignore()
830 For a complete example see the
831 F<t/200_examples/205_example_w_TestDeep.t> test file.
835 =head2 Type Constraint Constructors
837 The following functions are used to create type constraints.
838 They will then register the type constraints in a global store
839 where Moose can get to them if it needs to.
841 See the L<SYNOPSIS> for an example of how to use these.
845 =item B<type ($name, $where_clause)>
847 This creates a base type, which has no parent.
849 =item B<subtype ($name, $parent, $where_clause, ?$message)>
851 This creates a named subtype.
853 =item B<subtype ($parent, $where_clause, ?$message)>
855 This creates an unnamed subtype and will return the type
856 constraint meta-object, which will be an instance of
857 L<Moose::Meta::TypeConstraint>.
859 =item B<class_type ($class, ?$options)>
861 Creates a type constraint with the name C<$class> and the metaclass
862 L<Moose::Meta::TypeConstraint::Class>.
864 =item B<role_type ($role, ?$options)>
866 Creates a type constraint with the name C<$role> and the metaclass
867 L<Moose::Meta::TypeConstraint::Role>.
869 =item B<enum ($name, @values)>
871 This will create a basic subtype for a given set of strings.
872 The resulting constraint will be a subtype of C<Str> and
873 will match any of the items in C<@values>. It is case sensitive.
874 See the L<SYNOPSIS> for a simple example.
876 B<NOTE:> This is not a true proper enum type, it is simple
877 a convient constraint builder.
879 =item B<enum (\@values)>
881 If passed an ARRAY reference instead of the C<$name>, C<@values> pair,
882 this will create an unnamed enum. This can then be used in an attribute
885 has 'sort_order' => (
887 isa => enum([qw[ ascending descending ]]),
892 This is just sugar for the type constraint construction syntax.
896 This is just sugar for the type constraint construction syntax.
898 Takes a block/code ref as an argument. When the type constraint is
899 tested, the supplied code is run with the value to be tested in
900 $_. This block should return true or false to indicate whether or not
901 the constraint check passed.
905 This is just sugar for the type constraint construction syntax.
907 Takes a block/code ref as an argument. When the type constraint fails,
908 then the code block is run (with the value provided in $_). This code
909 ref should return a string, which will be used in the text of the
914 This can be used to define a "hand optimized" version of your
915 type constraint which can be used to avoid traversing a subtype
916 constraint heirarchy.
918 B<NOTE:> You should only use this if you know what you are doing,
919 all the built in types use this, so your subtypes (assuming they
920 are shallow) will not likely need to use this.
924 =head2 Type Coercion Constructors
926 Type constraints can also contain type coercions as well. If you
927 ask your accessor to coerce, then Moose will run the type-coercion
928 code first, followed by the type constraint check. This feature
929 should be used carefully as it is very powerful and could easily
930 take off a limb if you are not careful.
932 See the L<SYNOPSIS> for an example of how to use these.
940 This is just sugar for the type coercion construction syntax.
944 This is just sugar for the type coercion construction syntax.
948 =head2 Type Constraint Construction & Locating
952 =item B<normalize_type_constraint_name ($type_constraint_name)>
954 Given a string that is expected to match a type constraint, will normalize the
955 string so that extra whitespace and newlines are removed.
957 =item B<create_type_constraint_union ($pipe_seperated_types | @type_constraint_names)>
959 Given string with C<$pipe_seperated_types> or a list of C<@type_constraint_names>,
960 this will return a L<Moose::Meta::TypeConstraint::Union> instance.
962 =item B<create_parameterized_type_constraint ($type_name)>
964 Given a C<$type_name> in the form of:
966 BaseType[ContainerType]
968 this will extract the base type and container type and build an instance of
969 L<Moose::Meta::TypeConstraint::Parameterized> for it.
971 =item B<create_class_type_constraint ($class, ?$options)>
973 Given a class name it will create a new L<Moose::Meta::TypeConstraint::Class>
974 object for that class name.
976 =item B<create_role_type_constraint ($role, ?$options)>
978 Given a role name it will create a new L<Moose::Meta::TypeConstraint::Role>
979 object for that role name.
981 =item B<create_enum_type_constraint ($name, $values)>
983 =item B<find_or_parse_type_constraint ($type_name)>
985 This will attempt to find or create a type constraint given the a C<$type_name>.
986 If it cannot find it in the registry, it will see if it should be a union or
987 container type an create one if appropriate
989 =item B<find_or_create_type_constraint ($type_name, ?$options_for_anon_type)>
991 This function will first call C<find_or_parse_type_constraint> with the type name.
993 If no type is found or created, but C<$options_for_anon_type> are provided, it
994 will create the corresponding type.
996 This was used by the C<does> and C<isa> parameters to L<Moose::Meta::Attribute>
997 and are now superseded by C<find_or_create_isa_type_constraint> and
998 C<find_or_create_does_type_constraint>.
1000 =item B<find_or_create_isa_type_constraint ($type_name)>
1002 =item B<find_or_create_does_type_constraint ($type_name)>
1004 Attempts to parse the type name using L<find_or_parse_type_constraint> and if
1005 no appropriate constraint is found will create a new anonymous one.
1007 The C<isa> variant will use C<create_class_type_constraint> and the C<does>
1008 variant will use C<create_role_type_constraint>.
1010 =item B<find_type_constraint ($type_name)>
1012 This function can be used to locate a specific type constraint
1013 meta-object, of the class L<Moose::Meta::TypeConstraint> or a
1014 derivative. What you do with it from there is up to you :)
1016 =item B<register_type_constraint ($type_object)>
1018 This function will register a named type constraint with the type registry.
1020 =item B<get_type_constraint_registry>
1022 Fetch the L<Moose::Meta::TypeConstraint::Registry> object which
1023 keeps track of all type constraints.
1025 =item B<list_all_type_constraints>
1027 This will return a list of type constraint names, you can then
1028 fetch them using C<find_type_constraint ($type_name)> if you
1031 =item B<list_all_builtin_type_constraints>
1033 This will return a list of builtin type constraints, meaning,
1034 those which are defined in this module. See the section
1035 labeled L<Default Type Constraints> for a complete list.
1037 =item B<export_type_constraints_as_functions>
1039 This will export all the current type constraints as functions
1040 into the caller's namespace. Right now, this is mostly used for
1041 testing, but it might prove useful to others.
1043 =item B<get_all_parameterizable_types>
1045 This returns all the parameterizable types that have been registered.
1047 =item B<add_parameterizable_type ($type)>
1049 Adds C<$type> to the list of parameterizable types
1053 =head2 Namespace Management
1059 This will remove all the type constraint keywords from the
1060 calling class namespace.
1066 All complex software has bugs lurking in it, and this module is no
1067 exception. If you find a bug please either email me, or add the bug
1072 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1074 =head1 COPYRIGHT AND LICENSE
1076 Copyright 2006-2008 by Infinity Interactive, Inc.
1078 L<http://www.iinteractive.com>
1080 This library is free software; you can redistribute it and/or modify
1081 it under the same terms as Perl itself.