Type::Utils - utility functions to make defining type constraints a little easier
package Types::Mine; use Type::Library -base; use Type::Utils; extends "Types::Standard"; declare "AllCaps", as "Str", where { uc($_) eq $_ }, inline_as { my $varname = $_[1]; "uc($varname) eq $varname" }; coerce "AllCaps", from "Str", via { uc($_) };
This module provides utility functions to make defining type constraints a little easier. By default, all of the functions documented below are exported, except subtype and type (use declare instead).
subtype
type
declare
The following are similar to the similarly named functions described in Moose::Util::TypeConstraints.
subtype $name, %options
subtype %options
type $name, %options
type %options
as $parent
where { BLOCK }
message { BLOCK }
inline_as { BLOCK }
class_type $name, { class => $package, %options }
class_type { class => $package, %options }
role_type $name, { role => $package, %options }
role_type { role => $package, %options }
duck_type $name, \@methods
duck_type \@methods
union $name, \@constraints
union \@constraints
enum $name, \@values
enum \@values
coerce $target, @coercions
from $source
via { BLOCK }
declare $name, %options
declare %options
declare is a function which works like subtype and type. In fact, the latter pair are just aliases for the former.
If the caller package inherits from Type::Library then any non-anonymous types declared in the package will be automatically installed into the library.
intersection $name, \@constraints
intersection \@constraints
Defines a type constraint which is the intersection of several existing constraints.
extends @library
Indicates that this type library extends other type libraries, importing their type constraints.
declare_coercion $name, \%opts, $type1, $code1, ...
declare_coercion \%opts, $type1, $code1, ...
Declares a coercion that is not explicitly attached to any type in the library. For example:
declare_coercion "ArrayRefFromAny", from "Any", via { [$_] };
This coercion will be exportable from the library as a Type::Coercion object, but the ArrayRef type exported by the library won't automatically use it.
Coercions declared this way are immutable (frozen).
to_type $type
Used with declare_coercion to declare the target type constraint for a coercion, but still without explicitly attaching the coercion to the type constraint:
declare_coercion
declare_coercion "ArrayRefFromAny", to_type "ArrayRef", from "Any", via { [$_] };
You should pretty much always use this when declaring an unattached coercion because it's exceedingly useful for a type coercion to know what it will coerce to - this allows it to skip coercion when no coercion is needed (e.g. avoiding coercing [] to [ [] ]) and allows assert_coerce to work properly.
[]
[ [] ]
assert_coerce
Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=Type-Tiny.
Type::Tiny::Manual.
Type::Tiny, Type::Library, Types::Standard, Type::Coercion.
Type::Tiny::Class, Type::Tiny::Role, Type::Tiny::Duck, Type::Tiny::Enum, Type::Tiny::Union.
Moose::Util::TypeConstraints, Mouse::Util::TypeConstraints.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2013 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
To install Type::Tiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Type::Tiny
CPAN shell
perl -MCPAN -e shell install Type::Tiny
For more information on module installation, please visit the detailed CPAN module installation guide.