Moose::Util::TypeConstraints - Type constraint system for Moose
use Moose::Util::TypeConstraints; type 'Num' => where { Scalar::Util::looks_like_number($_) }; subtype 'Natural' => as 'Num' => where { $_ > 0 }; subtype 'NaturalLessThanTen' => as 'Natural' => where { $_ < 10 } => message { "This number ($_) is not less than ten!" }; coerce 'Num' => from 'Str' => via { 0+$_ }; enum 'RGBColors' => qw(red green blue);
This module provides Moose with the ability to create type contraints to be are used in both attribute definitions and for method argument validation.
This is NOT a type system for Perl 5. These are type constraints, and they are not used by Moose unless you tell it to. No type inference is performed, expression are not typed, etc. etc. etc.
This is simply a means of creating small constraint functions which can be used to simplify your own type-checking code.
It is almost always a good idea to quote your type and subtype names. This is to prevent perl from trying to execute the call as an indirect object call. This issue only seems to come up when you have a subtype the same name as a valid class, but when the issue does arise it tends to be quite annoying to debug.
So for instance, this:
subtype DateTime => as Object => where { $_->isa('DateTime') };
will Just Work, while this:
use DateTime; subtype DateTime => as Object => where { $_->isa('DateTime') };
will fail silently and cause many headaches. The simple way to solve this, as well as future proof your subtypes from classes which have yet to have been created yet, is to simply do this:
use DateTime; subtype 'DateTime' => as Object => where { $_->isa('DateTime') };
This module also provides a simple hierarchy for Perl 5 types, this could probably use some work, but it works for me at the moment.
Any Item Bool Undef Defined Value Num Int Str Ref ScalarRef ArrayRef HashRef CodeRef RegexpRef GlobRef FileHandle Object Role
Suggestions for improvement are welcome.
NOTE: The Undef type constraint does not work correctly in every occasion, please use it sparringly.
Undef
This function can be used to locate a specific type constraint meta-object. What you do with it from there is up to you :)
Given a list of @type_constraint_names, this will return a Moose::Meta::TypeConstraint::Union instance.
@type_constraint_names
This will export all the current type constraints as functions into the caller's namespace. Right now, this is mostly used for testing, but it might prove useful to others.
The following functions are used to create type constraints. They will then register the type constraints in a global store where Moose can get to them if it needs to.
See the SYNOPOSIS for an example of how to use these.
This creates a base type, which has no parent.
This creates a named subtype.
This creates an unnamed subtype and will return the type constraint meta-object, which will be an instance of Moose::Meta::TypeConstraint.
This will create a basic subtype for a given set of strings. The resulting constraint will be a subtype of Str and will match any of the items in @values. See the SYNOPSIS for a simple example.
Str
@values
NOTE: This is not a true proper enum type, it is simple a convient constraint builder.
This is just sugar for the type constraint construction syntax.
Type constraints can also contain type coercions as well. In most cases Moose will run the type-coercion code first, followed by the type constraint check. This feature should be used carefully as it is very powerful and could easily take off a limb if you are not careful.
This is just sugar for the type coercion construction syntax.
All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.
Stevan Little <stevan@iinteractive.com>
Copyright 2006 by Infinity Interactive, Inc.
http://www.iinteractive.com
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Moose, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Moose
CPAN shell
perl -MCPAN -e shell install Moose
For more information on module installation, please visit the detailed CPAN module installation guide.