\x89\x50\x4E\x47\x0D\x0A\x1A\x0A PNG  \x89\x50\x4E\x47\x0D\x0A\x1A\x0A =pod =encoding utf-8 =head1 NAME Type::Tiny::Manual::UsingWithMoose - how to use Type::Tiny and Type::Library with Moose =head1 SYNOPSIS { package Person; use Moose; use Types::Standard qw( Str Int ); has name => ( is => "ro", isa => Str, ); my $PositiveInt = Int -> where( sub { $_ > 0 } ) -> plus_coercions( Int, sub { 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); } } =head1 DESCRIPTION Type::Tiny is tested with L 2.0007 and above. L type constraints have an API almost identical to that of L. 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 C 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. =head2 Per-Attribute Coercions Type::Tiny offers convenience methods to alter the list of coercions associated with a type constraint. Let's imagine we wish to allow our C attribute to be coerced from an arrayref of strings. has name => ( is => "ro", isa => Str->plus_coercions( ArrayRef[Str], sub { join " ", @{$_} }, ), coerce => 1, ); This coercion will apply to the C attribute only; other attributes using the C type constraint will be unaffected. See the documentation for C, C and C in L. =head2 Optimization 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 L to do some internal inlining. See also L. =head2 Interactions with MooseX-Types Type::Tiny and MooseX::Types type constraints should "play nice". If, for example, C is taken from L (i.e. a Type::Tiny-based type library), and C is taken from L, then the following should "just work": isa => ArrayRef[ PositiveInt ] isa => PositiveInt | ArrayRef =head1 SEE ALSO For examples using Type::Tiny with L see the SYNOPSIS sections of L and L, and the L, and L in the test suite. =head1 AUTHOR Toby Inkster Etobyink@cpan.orgE. =head1 COPYRIGHT AND LICENCE This software is copyright (c) 2013-2014, 2017-2019 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. =head1 DISCLAIMER OF WARRANTIES 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. =cut