NAME
Moose::Util::TypeConstraints - Type constraint system for Moose
SYNOPSIS
use Moose::Util::TypeConstraints;
type Num => where { Scalar::Util::looks_like_number($_) };
subtype Natural
=> as Num
=> where { $_ > 0 };
subtype NaturalLessThanTen
=> as Natural
=> where { $_ < 10 };
coerce Num
=> from Str
=> via { 0+$_ };
DESCRIPTION
This module provides Moose with the ability to create type contraints to be are used in both attribute definitions and for method argument validation.
Important Caveat
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 simply your own type-checking code.
Default Type Constraints
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
Value
Int
Str
Ref
ScalarRef
ArrayRef
HashRef
CodeRef
RegexpRef
Object
Suggestions for improvement are welcome.
FUNCTIONS
Type Constraint Registry
- find_type_constraint ($type_name)
-
This function can be used to locate a specific type constraint meta-object. What you do with it from there is up to you :)
- export_type_contstraints_as_functions
-
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.
Type Constraint Constructors
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.
- type ($name, $where_clause)
-
This creates a base type, which has no parent.
- subtype ($name, $parent, $where_clause)
-
This creates a named subtype.
- subtype ($parent, $where_clause)
-
This creates an unnamed subtype and will return the type constraint meta-object, which will be an instance of Moose::Meta::TypeConstraint.
- as
-
This is just sugar for the type constraint construction syntax.
- where
-
This is just sugar for the type constraint construction syntax.
Type Coercion Constructors
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.
See the SYNOPOSIS for an example of how to use these.
- coerce
- from
-
This is just sugar for the type coercion construction syntax.
- via
-
This is just sugar for the type coercion construction syntax.
BUGS
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.
AUTHOR
Stevan Little <stevan@iinteractive.com>
COPYRIGHT AND LICENSE
Copyright 2006 by Infinity Interactive, Inc.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.