2 package Moose::Util::TypeConstraints;
8 use Scalar::Util 'blessed';
11 our $VERSION = '0.57';
12 $VERSION = eval $VERSION;
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 register_type_constraint ($);
25 sub find_or_create_type_constraint ($;$);
26 sub find_or_parse_type_constraint ($);
27 sub find_or_create_isa_type_constraint ($);
28 sub find_or_create_does_type_constraint ($);
29 sub create_type_constraint_union (@);
30 sub create_parameterized_type_constraint ($);
31 sub create_class_type_constraint ($;$);
32 sub create_role_type_constraint ($;$);
33 sub create_enum_type_constraint ($$);
49 sub _create_type_constraint ($$$;$$);
50 sub _install_type_coercions ($$);
52 ## --------------------------------------------------------
54 use Moose::Meta::TypeConstraint;
55 use Moose::Meta::TypeConstraint::Union;
56 use Moose::Meta::TypeConstraint::Parameterized;
57 use Moose::Meta::TypeConstraint::Parameterizable;
58 use Moose::Meta::TypeConstraint::Class;
59 use Moose::Meta::TypeConstraint::Role;
60 use Moose::Meta::TypeConstraint::Enum;
61 use Moose::Meta::TypeCoercion;
62 use Moose::Meta::TypeCoercion::Union;
63 use Moose::Meta::TypeConstraint::Registry;
64 use Moose::Util::TypeConstraints::OptimizedConstraints;
66 Moose::Exporter->setup_import_methods(
69 type subtype class_type role_type as where message optimize_as
73 register_type_constraint )
78 ## --------------------------------------------------------
79 ## type registry and some useful functions for it
80 ## --------------------------------------------------------
82 my $REGISTRY = Moose::Meta::TypeConstraint::Registry->new;
85 my ($msg, @args) = @_;
87 my $caller = caller(2);
88 if ( $caller->can("meta") and my $throw = $caller->meta->can("throw_error") ) {
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 }; # the undef is for compat
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_isa_type_constraint($type_parameter),
151 #should we also support optimized checks?
152 sub create_class_type_constraint ($;$) {
153 my ( $class, $options ) = @_;
155 # too early for this check
156 #find_type_constraint("ClassName")->check($class)
157 # || confess "Can't create a class type constraint because '$class' is not a class name";
165 $options{name} ||= "__ANON__";
167 Moose::Meta::TypeConstraint::Class->new( %options );
170 sub create_role_type_constraint ($;$) {
171 my ( $role, $options ) = @_;
173 # too early for this check
174 #find_type_constraint("ClassName")->check($class)
175 # || confess "Can't create a class type constraint because '$class' is not a class name";
183 $options{name} ||= "__ANON__";
185 Moose::Meta::TypeConstraint::Role->new( %options );
189 sub find_or_create_type_constraint ($;$) {
190 my ( $type_constraint_name, $options_for_anon_type ) = @_;
192 if ( my $constraint = find_or_parse_type_constraint($type_constraint_name) ) {
195 elsif ( defined $options_for_anon_type ) {
197 # if there is no $options_for_anon_type
198 # specified, then we assume they don't
199 # want to create one, and return nothing.
201 # otherwise assume that we should create
202 # an ANON type with the $options_for_anon_type
203 # options which can be passed in. It should
204 # be noted that these don't get registered
205 # so we need to return it.
207 return Moose::Meta::TypeConstraint->new(
209 %{$options_for_anon_type}
216 sub find_or_create_isa_type_constraint ($) {
217 my $type_constraint_name = shift;
218 find_or_parse_type_constraint($type_constraint_name) || create_class_type_constraint($type_constraint_name)
221 sub find_or_create_does_type_constraint ($) {
222 my $type_constraint_name = shift;
223 find_or_parse_type_constraint($type_constraint_name) || create_role_type_constraint($type_constraint_name)
226 sub find_or_parse_type_constraint ($) {
227 my $type_constraint_name = shift;
230 if ($constraint = find_type_constraint($type_constraint_name)) {
232 } elsif (_detect_type_constraint_union($type_constraint_name)) {
233 $constraint = create_type_constraint_union($type_constraint_name);
234 } elsif (_detect_parameterized_type_constraint($type_constraint_name)) {
235 $constraint = create_parameterized_type_constraint($type_constraint_name);
240 $REGISTRY->add_type_constraint($constraint);
244 ## --------------------------------------------------------
245 ## exported functions ...
246 ## --------------------------------------------------------
248 sub find_type_constraint ($) {
251 if ( blessed $type and $type->isa("Moose::Meta::TypeConstraint") ) {
255 return unless $REGISTRY->has_type_constraint($type);
256 return $REGISTRY->get_type_constraint($type);
260 sub register_type_constraint ($) {
261 my $constraint = shift;
262 confess "can't register an unnamed type constraint" unless defined $constraint->name;
263 $REGISTRY->add_type_constraint($constraint);
270 splice(@_, 1, 0, undef);
271 goto &_create_type_constraint;
274 sub subtype ($$;$$$) {
276 # this adds an undef for the name
277 # if this is an anon-subtype:
278 # subtype(Num => where { $_ % 2 == 0 }) # anon 'even' subtype
279 # but if the last arg is not a code
280 # ref then it is a subtype alias:
281 # subtype(MyNumbers => as Num); # now MyNumbers is the same as Num
282 # ... yeah I know it's ugly code
284 unshift @_ => undef if scalar @_ <= 2 && ('CODE' eq ref($_[1]));
285 goto &_create_type_constraint;
288 sub class_type ($;$) {
289 register_type_constraint(
290 create_class_type_constraint(
292 ( defined($_[1]) ? $_[1] : () ),
297 sub role_type ($;$) {
298 register_type_constraint(
299 create_role_type_constraint(
301 ( defined($_[1]) ? $_[1] : () ),
307 my ($type_name, @coercion_map) = @_;
308 _install_type_coercions($type_name, \@coercion_map);
312 sub from ($) { $_[0] }
313 sub where (&) { $_[0] }
314 sub via (&) { $_[0] }
316 sub message (&) { +{ message => $_[0] } }
317 sub optimize_as (&) { +{ optimized => $_[0] } }
320 my ($type_name, @values) = @_;
322 # if only an array-ref is passed then
323 # you get an anon-enum
325 if (ref $type_name eq 'ARRAY' && !@values) {
326 @values = @$type_name;
329 (scalar @values >= 2)
330 || confess "You must have at least two values to enumerate through";
331 my %valid = map { $_ => 1 } @values;
333 register_type_constraint(
334 create_enum_type_constraint(
341 sub create_enum_type_constraint ($$) {
342 my ( $type_name, $values ) = @_;
344 Moose::Meta::TypeConstraint::Enum->new(
345 name => $type_name || '__ANON__',
350 ## --------------------------------------------------------
351 ## desugaring functions ...
352 ## --------------------------------------------------------
354 sub _create_type_constraint ($$$;$$) {
359 my ($message, $optimized);
361 $message = $_->{message} if exists $_->{message};
362 $optimized = $_->{optimized} if exists $_->{optimized};
365 my $pkg_defined_in = scalar(caller(0));
368 my $type = $REGISTRY->get_type_constraint($name);
370 ($type->_package_defined_in eq $pkg_defined_in)
371 || confess ("The type constraint '$name' has already been created in "
372 . $type->_package_defined_in . " and cannot be created again in "
377 my $class = "Moose::Meta::TypeConstraint";
379 # FIXME should probably not be a special case
380 if ( defined $parent and $parent = find_or_parse_type_constraint($parent) ) {
381 $class = "Moose::Meta::TypeConstraint::Parameterizable"
382 if $parent->isa("Moose::Meta::TypeConstraint::Parameterizable");
385 my $constraint = $class->new(
386 name => $name || '__ANON__',
387 package_defined_in => $pkg_defined_in,
389 ($parent ? (parent => $parent ) : ()),
390 ($check ? (constraint => $check) : ()),
391 ($message ? (message => $message) : ()),
392 ($optimized ? (optimized => $optimized) : ()),
396 # if we have a type constraint union, and no
397 # type check, this means we are just aliasing
398 # the union constraint, which means we need to
399 # handle this differently.
401 if (not(defined $check)
402 && $parent->isa('Moose::Meta::TypeConstraint::Union')
403 && $parent->has_coercion
405 $constraint->coercion(Moose::Meta::TypeCoercion::Union->new(
406 type_constraint => $parent
410 $REGISTRY->add_type_constraint($constraint)
416 sub _install_type_coercions ($$) {
417 my ($type_name, $coercion_map) = @_;
418 my $type = find_type_constraint($type_name);
420 || confess "Cannot find type '$type_name', perhaps you forgot to load it.";
421 if ($type->has_coercion) {
422 $type->coercion->add_type_coercions(@$coercion_map);
425 my $type_coercion = Moose::Meta::TypeCoercion->new(
426 type_coercion_map => $coercion_map,
427 type_constraint => $type
429 $type->coercion($type_coercion);
433 ## --------------------------------------------------------
434 ## type notation parsing ...
435 ## --------------------------------------------------------
438 # All I have to say is mugwump++ cause I know
439 # do not even have enough regexp-fu to be able
440 # to have written this (I can only barely
441 # understand it as it is)
446 my $valid_chars = qr{[\w:]};
447 my $type_atom = qr{ $valid_chars+ };
451 my $type = qr{ $valid_chars+ (?: \[ (??{$any}) \] )? }x;
452 my $type_capture_parts = qr{ ($valid_chars+) (?: \[ ((??{$any})) \] )? }x;
453 my $type_with_parameter = qr{ $valid_chars+ \[ (??{$any}) \] }x;
455 my $op_union = qr{ \s* \| \s* }x;
456 my $union = qr{ $type (?: $op_union $type )+ }x;
458 $any = qr{ $type | $union }x;
460 sub _parse_parameterized_type_constraint {
461 { no warnings 'void'; $any; } # force capture of interpolated lexical
462 $_[0] =~ m{ $type_capture_parts }x;
466 sub _detect_parameterized_type_constraint {
467 { no warnings 'void'; $any; } # force capture of interpolated lexical
468 $_[0] =~ m{ ^ $type_with_parameter $ }x;
471 sub _parse_type_constraint_union {
472 { no warnings 'void'; $any; } # force capture of interpolated lexical
475 while ( $given =~ m{ \G (?: $op_union )? ($type) }gcx ) {
478 (pos($given) eq length($given))
479 || confess "'$given' didn't parse (parse-pos="
487 sub _detect_type_constraint_union {
488 { no warnings 'void'; $any; } # force capture of interpolated lexical
489 $_[0] =~ m{^ $type $op_union $type ( $op_union .* )? $}x;
493 ## --------------------------------------------------------
494 # define some basic built-in types
495 ## --------------------------------------------------------
497 type 'Any' => where { 1 }; # meta-type including all
498 type 'Item' => where { 1 }; # base-type
500 subtype 'Undef' => as 'Item' => where { !defined($_) };
501 subtype 'Defined' => as 'Item' => where { defined($_) };
505 => where { !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' };
509 => where { !ref($_) }
510 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Value;
515 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Ref;
520 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Str;
524 => where { Scalar::Util::looks_like_number($_) }
525 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Num;
529 => where { "$_" =~ /^-?[0-9]+$/ }
530 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Int;
532 subtype 'ScalarRef' => as 'Ref' => where { ref($_) eq 'SCALAR' } => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::ScalarRef;
533 subtype 'CodeRef' => as 'Ref' => where { ref($_) eq 'CODE' } => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::CodeRef;
534 subtype 'RegexpRef' => as 'Ref' => where { ref($_) eq 'Regexp' } => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::RegexpRef;
535 subtype 'GlobRef' => as 'Ref' => where { ref($_) eq 'GLOB' } => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::GlobRef;
538 # scalar filehandles are GLOB refs,
539 # but a GLOB ref is not always a filehandle
542 => where { Scalar::Util::openhandle($_) || ( blessed($_) && $_->isa("IO::Handle") ) }
543 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::FileHandle;
546 # blessed(qr/.../) returns true,.. how odd
549 => where { blessed($_) && blessed($_) ne 'Regexp' }
550 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Object;
554 => where { $_->can('does') }
555 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::Role;
557 my $_class_name_checker = sub {
562 => where { Class::MOP::is_class_loaded($_) }
563 => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::ClassName;
565 ## --------------------------------------------------------
566 # parameterizable types ...
568 $REGISTRY->add_type_constraint(
569 Moose::Meta::TypeConstraint::Parameterizable->new(
571 package_defined_in => __PACKAGE__,
572 parent => find_type_constraint('Ref'),
573 constraint => sub { ref($_) eq 'ARRAY' },
574 optimized => \&Moose::Util::TypeConstraints::OptimizedConstraints::ArrayRef,
575 constraint_generator => sub {
576 my $type_parameter = shift;
577 my $check = $type_parameter->_compiled_type_constraint;
579 foreach my $x (@$_) {
580 ($check->($x)) || return
587 $REGISTRY->add_type_constraint(
588 Moose::Meta::TypeConstraint::Parameterizable->new(
590 package_defined_in => __PACKAGE__,
591 parent => find_type_constraint('Ref'),
592 constraint => sub { ref($_) eq 'HASH' },
593 optimized => \&Moose::Util::TypeConstraints::OptimizedConstraints::HashRef,
594 constraint_generator => sub {
595 my $type_parameter = shift;
596 my $check = $type_parameter->_compiled_type_constraint;
598 foreach my $x (values %$_) {
599 ($check->($x)) || return
606 $REGISTRY->add_type_constraint(
607 Moose::Meta::TypeConstraint::Parameterizable->new(
609 package_defined_in => __PACKAGE__,
610 parent => find_type_constraint('Item'),
611 constraint => sub { 1 },
612 constraint_generator => sub {
613 my $type_parameter = shift;
614 my $check = $type_parameter->_compiled_type_constraint;
616 return 1 if not(defined($_)) || $check->($_);
623 my @PARAMETERIZABLE_TYPES = map {
624 $REGISTRY->get_type_constraint($_)
625 } qw[ArrayRef HashRef Maybe];
627 sub get_all_parameterizable_types { @PARAMETERIZABLE_TYPES }
628 sub add_parameterizable_type {
630 (blessed $type && $type->isa('Moose::Meta::TypeConstraint::Parameterizable'))
631 || confess "Type must be a Moose::Meta::TypeConstraint::Parameterizable not $type";
632 push @PARAMETERIZABLE_TYPES => $type;
635 ## --------------------------------------------------------
636 # end of built-in types ...
637 ## --------------------------------------------------------
640 my @BUILTINS = list_all_type_constraints();
641 sub list_all_builtin_type_constraints { @BUILTINS }
652 Moose::Util::TypeConstraints - Type constraint system for Moose
656 use Moose::Util::TypeConstraints;
658 type 'Num' => where { Scalar::Util::looks_like_number($_) };
664 subtype 'NaturalLessThanTen'
667 => message { "This number ($_) is not less than ten!" };
673 enum 'RGBColors' => qw(red green blue);
677 This module provides Moose with the ability to create custom type
678 contraints to be used in attribute definition.
680 =head2 Important Caveat
682 This is B<NOT> a type system for Perl 5. These are type constraints,
683 and they are not used by Moose unless you tell it to. No type
684 inference is performed, expression are not typed, etc. etc. etc.
686 This is simply a means of creating small constraint functions which
687 can be used to simplify your own type-checking code, with the added
688 side benefit of making your intentions clearer through self-documentation.
690 =head2 Slightly Less Important Caveat
692 It is B<always> a good idea to quote your type and subtype names.
694 This is to prevent perl from trying to execute the call as an indirect
695 object call. This issue only seems to come up when you have a subtype
696 the same name as a valid class, but when the issue does arise it tends
697 to be quite annoying to debug.
699 So for instance, this:
701 subtype DateTime => as Object => where { $_->isa('DateTime') };
703 will I<Just Work>, while this:
706 subtype DateTime => as Object => where { $_->isa('DateTime') };
708 will fail silently and cause many headaches. The simple way to solve
709 this, as well as future proof your subtypes from classes which have
710 yet to have been created yet, is to simply do this:
713 subtype 'DateTime' => as 'Object' => where { $_->isa('DateTime') };
715 =head2 Default Type Constraints
717 This module also provides a simple hierarchy for Perl 5 types, here is
718 that hierarchy represented visually.
742 B<NOTE:> Any type followed by a type parameter C<[`a]> can be
743 parameterized, this means you can say:
745 ArrayRef[Int] # an array of intergers
746 HashRef[CodeRef] # a hash of str to CODE ref mappings
747 Maybe[Str] # value may be a string, may be undefined
749 B<NOTE:> Unless you parameterize a type, then it is invalid to
750 include the square brackets. I.e. C<ArrayRef[]> will be
751 literally interpreted as a type name.
753 B<NOTE:> The C<Undef> type constraint for the most part works
754 correctly now, but edge cases may still exist, please use it
757 B<NOTE:> The C<ClassName> type constraint does a complex package
758 existence check. This means that your class B<must> be loaded for
759 this type constraint to pass. I know this is not ideal for all,
760 but it is a saner restriction than most others.
762 =head2 Type Constraint Naming
764 Since the types created by this module are global, it is suggested
765 that you namespace your types just as you would namespace your
766 modules. So instead of creating a I<Color> type for your B<My::Graphics>
767 module, you would call the type I<My::Graphics::Color> instead.
769 =head2 Use with Other Constraint Modules
771 This module should play fairly nicely with other constraint
772 modules with only some slight tweaking. The C<where> clause
773 in types is expected to be a C<CODE> reference which checks
774 it's first argument and returns a boolean. Since most constraint
775 modules work in a similar way, it should be simple to adapt
776 them to work with Moose.
778 For instance, this is how you could use it with
779 L<Declare::Constraints::Simple> to declare a completely new type.
781 type 'HashOfArrayOfObjects'
784 -values => IsArrayRef( IsObject ));
786 For more examples see the F<t/200_examples/204_example_w_DCS.t>
789 Here is an example of using L<Test::Deep> and it's non-test
790 related C<eq_deeply> function.
792 type 'ArrayOfHashOfBarsAndRandomNumbers'
795 array_each(subhashof({
797 random_number => ignore()
801 For a complete example see the
802 F<t/200_examples/205_example_w_TestDeep.t> test file.
806 =head2 Type Constraint Constructors
808 The following functions are used to create type constraints.
809 They will then register the type constraints in a global store
810 where Moose can get to them if it needs to.
812 See the L<SYNOPSIS> for an example of how to use these.
816 =item B<type ($name, $where_clause)>
818 This creates a base type, which has no parent.
820 =item B<subtype ($name, $parent, $where_clause, ?$message)>
822 This creates a named subtype.
824 =item B<subtype ($parent, $where_clause, ?$message)>
826 This creates an unnamed subtype and will return the type
827 constraint meta-object, which will be an instance of
828 L<Moose::Meta::TypeConstraint>.
830 =item B<class_type ($class, ?$options)>
832 Creates a type constraint with the name C<$class> and the metaclass
833 L<Moose::Meta::TypeConstraint::Class>.
835 =item B<role_type ($role, ?$options)>
837 Creates a type constraint with the name C<$role> and the metaclass
838 L<Moose::Meta::TypeConstraint::Role>.
840 =item B<enum ($name, @values)>
842 This will create a basic subtype for a given set of strings.
843 The resulting constraint will be a subtype of C<Str> and
844 will match any of the items in C<@values>. It is case sensitive.
845 See the L<SYNOPSIS> for a simple example.
847 B<NOTE:> This is not a true proper enum type, it is simple
848 a convient constraint builder.
850 =item B<enum (\@values)>
852 If passed an ARRAY reference instead of the C<$name>, C<@values> pair,
853 this will create an unnamed enum. This can then be used in an attribute
856 has 'sort_order' => (
858 isa => enum([qw[ ascending descending ]]),
863 This is just sugar for the type constraint construction syntax.
867 This is just sugar for the type constraint construction syntax.
869 Takes a block/code ref as an argument. When the type constraint is
870 tested, the supplied code is run with the value to be tested in
871 $_. This block should return true or false to indicate whether or not
872 the constraint check passed.
876 This is just sugar for the type constraint construction syntax.
878 Takes a block/code ref as an argument. When the type constraint fails,
879 then the code block is run (with the value provided in $_). This code
880 ref should return a string, which will be used in the text of the
885 This can be used to define a "hand optimized" version of your
886 type constraint which can be used to avoid traversing a subtype
887 constraint heirarchy.
889 B<NOTE:> You should only use this if you know what you are doing,
890 all the built in types use this, so your subtypes (assuming they
891 are shallow) will not likely need to use this.
895 =head2 Type Coercion Constructors
897 Type constraints can also contain type coercions as well. If you
898 ask your accessor to coerce, then Moose will run the type-coercion
899 code first, followed by the type constraint check. This feature
900 should be used carefully as it is very powerful and could easily
901 take off a limb if you are not careful.
903 See the L<SYNOPSIS> for an example of how to use these.
911 This is just sugar for the type coercion construction syntax.
915 This is just sugar for the type coercion construction syntax.
919 =head2 Type Constraint Construction & Locating
923 =item B<create_type_constraint_union ($pipe_seperated_types | @type_constraint_names)>
925 Given string with C<$pipe_seperated_types> or a list of C<@type_constraint_names>,
926 this will return a L<Moose::Meta::TypeConstraint::Union> instance.
928 =item B<create_parameterized_type_constraint ($type_name)>
930 Given a C<$type_name> in the form of:
932 BaseType[ContainerType]
934 this will extract the base type and container type and build an instance of
935 L<Moose::Meta::TypeConstraint::Parameterized> for it.
937 =item B<create_class_type_constraint ($class, ?$options)>
939 Given a class name it will create a new L<Moose::Meta::TypeConstraint::Class>
940 object for that class name.
942 =item B<create_role_type_constraint ($role, ?$options)>
944 Given a role name it will create a new L<Moose::Meta::TypeConstraint::Role>
945 object for that role name.
947 =item B<create_enum_type_constraint ($name, $values)>
949 =item B<find_or_parse_type_constraint ($type_name)>
951 This will attempt to find or create a type constraint given the a C<$type_name>.
952 If it cannot find it in the registry, it will see if it should be a union or
953 container type an create one if appropriate
955 =item B<find_or_create_type_constraint ($type_name, ?$options_for_anon_type)>
957 This function will first call C<find_or_parse_type_constraint> with the type name.
959 If no type is found or created, but C<$options_for_anon_type> are provided, it
960 will create the corresponding type.
962 This was used by the C<does> and C<isa> parameters to L<Moose::Meta::Attribute>
963 and are now superseded by C<find_or_create_isa_type_constraint> and
964 C<find_or_create_does_type_constraint>.
966 =item B<find_or_create_isa_type_constraint ($type_name)>
968 =item B<find_or_create_does_type_constraint ($type_name)>
970 Attempts to parse the type name using L<find_or_parse_type_constraint> and if
971 no appropriate constraint is found will create a new anonymous one.
973 The C<isa> variant will use C<create_class_type_constraint> and the C<does>
974 variant will use C<create_role_type_constraint>.
976 =item B<find_type_constraint ($type_name)>
978 This function can be used to locate a specific type constraint
979 meta-object, of the class L<Moose::Meta::TypeConstraint> or a
980 derivative. What you do with it from there is up to you :)
982 =item B<register_type_constraint ($type_object)>
984 This function will register a named type constraint with the type registry.
986 =item B<get_type_constraint_registry>
988 Fetch the L<Moose::Meta::TypeConstraint::Registry> object which
989 keeps track of all type constraints.
991 =item B<list_all_type_constraints>
993 This will return a list of type constraint names, you can then
994 fetch them using C<find_type_constraint ($type_name)> if you
997 =item B<list_all_builtin_type_constraints>
999 This will return a list of builtin type constraints, meaning,
1000 those which are defined in this module. See the section
1001 labeled L<Default Type Constraints> for a complete list.
1003 =item B<export_type_constraints_as_functions>
1005 This will export all the current type constraints as functions
1006 into the caller's namespace. Right now, this is mostly used for
1007 testing, but it might prove useful to others.
1009 =item B<get_all_parameterizable_types>
1011 This returns all the parameterizable types that have been registered.
1013 =item B<add_parameterizable_type ($type)>
1015 Adds C<$type> to the list of parameterizable types
1019 =head1 Error Management
1025 If the caller is a Moose metaclass, use its L<Moose::Meta::Class/throw_error>
1026 routine, otherwise use L<Carp/confess>.
1030 =head2 Namespace Management
1036 This will remove all the type constraint keywords from the
1037 calling class namespace.
1043 All complex software has bugs lurking in it, and this module is no
1044 exception. If you find a bug please either email me, or add the bug
1049 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1051 =head1 COPYRIGHT AND LICENSE
1053 Copyright 2006-2008 by Infinity Interactive, Inc.
1055 L<http://www.iinteractive.com>
1057 This library is free software; you can redistribute it and/or modify
1058 it under the same terms as Perl itself.