1Type::Tiny::Manual::UsiUnsgeWritChoMnotorsieb(u3t)ed PerTlypDeo:c:uTmiennyt:a:tMiaonnual::UsingWithMoose(3)
2
3
4

NAME

6       Type::Tiny::Manual::UsingWithMoose - how to use Type::Tiny and
7       Type::Library with Moose
8

SYNOPSIS

10          {
11             package Person;
12
13             use Moose;
14             use Types::Standard qw( Str Int );
15
16             has name => (
17                is      => "ro",
18                isa     => Str,
19             );
20
21             my $PositiveInt = Int
22                -> where( sub { $_ > 0 } )
23                -> plus_coercions( Int, sub { abs $_ } );
24
25             has age => (
26                is      => "ro",
27                isa     => $PositiveInt,
28                coerce  => 1,
29                writer  => "_set_age",
30             );
31
32             sub get_older {
33                my $self = shift;
34                my ($years) = @_;
35                $PositiveInt->assert_valid($years);
36                $self->_set_age($self->age + $years);
37             }
38          }
39

DESCRIPTION

41       Type::Tiny is tested with Moose 2.0007 and above.
42
43       Type::Tiny type constraints have an API almost identical to that of
44       Moose::Meta::TypeConstraint. It is also able to build a
45       Moose::Meta::TypeConstraint constraint from a Type::Tiny constraint,
46       and will do so automatically when needed. When Moose.pm is loaded,
47       Type::Tiny will use Perl's "AUTOLOAD" feature to proxy method calls
48       through to the Moose::Meta::TypeConstraint object. In short, you can
49       use a Type::Tiny object pretty much anywhere you'd use a
50       Moose::Meta::TypeConstraint and you are unlikely to notice the
51       difference.
52
53   Per-Attribute Coercions
54       Type::Tiny offers convenience methods to alter the list of coercions
55       associated with a type constraint. Let's imagine we wish to allow our
56       "name" attribute to be coerced from an arrayref of strings.
57
58             has name => (
59                is      => "ro",
60                isa     => Str->plus_coercions(
61                   ArrayRef[Str], sub { join " ", @{$_} },
62                ),
63                coerce  => 1,
64             );
65
66       This coercion will apply to the "name" attribute only; other attributes
67       using the "Str" type constraint will be unaffected.
68
69       See the documentation for "plus_coercions", "minus_coercions" and
70       "no_coercions" in Type::Tiny.
71
72   Optimization
73       The usual advice for optimizing type constraints applies: use type
74       constraints which can be inlined whenever possible.
75
76       Defining coercions as strings rather than coderefs won't give you as
77       much of a boost with Moose as it does with Moo, because Moose doesn't
78       inline coercion code. However, it should still improve performance
79       somewhat because it allows Type::Coercion to do some internal inlining.
80
81       See also Type::Tiny::Manual::Optimization.
82
83   Interactions with MooseX-Types
84       Type::Tiny and MooseX::Types type constraints should "play nice". If,
85       for example, "ArrayRef" is taken from Types::Standard (i.e. a
86       Type::Tiny-based type library), and "PositiveInt" is taken from
87       MooseX::Types::Common::Numeric, then the following should "just work":
88
89          isa => ArrayRef[ PositiveInt ]
90
91          isa => PositiveInt | ArrayRef
92

SEE ALSO

94       For examples using Type::Tiny with Moose see the SYNOPSIS sections of
95       Type::Tiny and Type::Library, and the Moose integration tests
96       <https://github.com/tobyink/p5-type-
97       tiny/tree/master/t/30-integration/Moose>, and MooseX-Types integration
98       tests <https://github.com/tobyink/p5-type-
99       tiny/tree/master/t/30-integration/MooseX-Types> in the test suite.
100

AUTHOR

102       Toby Inkster <tobyink@cpan.org>.
103
105       This software is copyright (c) 2013-2014, 2017-2019 by Toby Inkster.
106
107       This is free software; you can redistribute it and/or modify it under
108       the same terms as the Perl 5 programming language system itself.
109

DISCLAIMER OF WARRANTIES

111       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
112       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
113       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
114
115
116
117perl v5.30.0                      2019-07-T2y6pe::Tiny::Manual::UsingWithMoose(3)
Impressum