Type::Tiny::Manual::UsingWithMoose - how to use Type::Tiny and Type::Library with Moose
{ package Person; use Moose; use Types::Standard qw( Str Int ); use Type::Utils qw( declare as where inline_as coerce from ); has name => ( is => "ro", isa => Str, ); my $PositiveInt = declare as Int, where { $_ > 0 }, inline_as { "$_ =~ /^[0-9]\$/ and $_ > 0" }; coerce $PositiveInt, from Int, q{ abs $_ }; has age => ( is => "ro", isa => $PositiveInt, coerce => 1, writer => "_set_age", ); sub get_older { my $self = shift; my ($years) = @_; $PositiveInt->assert_valid($years); $self->_set_age($self->age + $years); } }
Type::Tiny type constraints have an API almost identical to that of Moose::Meta::TypeConstraint. It is also able to build a Moose::Meta::TypeConstraint constraint from a Type::Tiny constraint, and will do so automatically when needed. When Moose.pm is loaded, Type::Tiny will use Perl's AUTOLOAD feature to proxy method calls through to the Moose::Meta::TypeConstraint object. In short, you can use a Type::Tiny object pretty much anywhere you'd use a Moose::Meta::TypeConstraint and you are unlikely to notice the difference.
AUTOLOAD
Type::Tiny offers convenience methods to alter the list of coercions associated with a type constraint. Let's imagine we wish to allow our name attribute to be coerced from an arrayref of strings.
name
has name => ( is => "ro", isa => Str->plus_coercions( ArrayRef[Str], sub { join " ", @{$_} }, ), coerce => 1, );
This coercion will apply to the name attribute only; other attributes using the Str type constraint will be unaffected.
Str
See the documentation for plus_coercions, minus_coercions and no_coercions in Type::Tiny.
plus_coercions
minus_coercions
no_coercions
The usual advice for optimizing type constraints applies: use type constraints which can be inlined whenever possible.
Defining coercions as strings rather than coderefs won't give you as much of a boost with Moose as it does with Moo, because Moose doesn't inline coercion code. However, it should still improve performance somewhat because it allows Type::Coercion to do some internal inlining.
For examples using Type::Tiny with Moose see the SYNOPSIS sections of Type::Tiny and Type::Library, and the files moose.t, moose-coercion.t and moo-inflation.t in the Type-Tiny test suite.
moose.t
moose-coercion.t
moo-inflation.t
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2013-2014 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.