1Type::Tiny::Manual::UsiUnsgeWritChoMnotursieb(u3t)ed PerTlypDeo:c:uTmiennyt:a:tMiaonnual::UsingWithMouse(3)
2
3
4
6 Type::Tiny::Manual::UsingWithMouse - how to use Type::Tiny and
7 Type::Library with Mouse
8
10 {
11 package Person;
12
13 use Mouse;
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
41 Mouse support in Type::Tiny was somewhat of an afterthought. It should
42 work, but is not anywhere near as well-tested as Moo or Moose support.
43
45 Type::Tiny is tested with Mouse 1.00 and above.
46
47 Type::Tiny type constraints have an API almost identical to that of
48 Mouse::Meta::TypeConstraint. As a result, you can use a Type::Tiny
49 object pretty much anywhere you'd use a Mouse::Meta::TypeConstraint and
50 you are unlikely to notice the difference. (And Mouse is unlikely to
51 notice the difference too!)
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 Mouse's built-in type constraints are implemented using XS and are
74 stupidly fast. For many type constraints, if Type::Tiny notices Mouse
75 is loaded early enough, Type::Tiny will borrow Mouse's XS subs.
76
77 See also Type::Tiny::Manual::Optimization.
78
79 Interactions with MouseX-Types
80 Type::Tiny and MouseX::Types type constraints should "play nice". If,
81 for example, "ArrayRef" is taken from Types::Standard (i.e. a
82 Type::Tiny-based type library), and "PositiveInt" is taken from
83 MouseX::Types::Common::Numeric, then the following should "just work":
84
85 isa => ArrayRef[ PositiveInt ]
86
87 isa => PositiveInt | ArrayRef
88
90 For examples using Type::Tiny with Mouse see the SYNOPSIS sections of
91 Type::Tiny and Type::Library, and the Mouse integration tests
92 <https://github.com/tobyink/p5-type-
93 tiny/tree/master/t/30-integration/Mouse>, and MouseX-Types integration
94 tests <https://github.com/tobyink/p5-type-
95 tiny/tree/master/t/30-integration/MouseX-Types> in the test suite.
96
98 Toby Inkster <tobyink@cpan.org>.
99
101 This software is copyright (c) 2013-2014, 2017-2019 by Toby Inkster.
102
103 This is free software; you can redistribute it and/or modify it under
104 the same terms as the Perl 5 programming language system itself.
105
107 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
108 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
109 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
110
111
112
113perl v5.28.1 2019-01-T0y8pe::Tiny::Manual::UsingWithMouse(3)