1Type::Tiny::Manual(3) User Contributed Perl DocumentationType::Tiny::Manual(3)
2
3
4
6 Type::Tiny::Manual - an overview of Type::Tiny
7
9 Type::Tiny is a small Perl <http://www.perl.org/> class for writing
10 type constraints, inspired by Moose's type constraint API and
11 MooseX::Types. It has only one non-core dependency (and even that is
12 simply a module that was previously distributed as part of Type::Tiny
13 but has since been spun off), and can be used with Moose, Mouse, or Moo
14 (or none of the above).
15
16 Type::Tiny is used by over 800 Perl distributions on the CPAN
17 (Comprehensive Perl Archive Network) and can be considered a stable and
18 mature framework for efficiently and reliably enforcing data types.
19
20 Type::Tiny is bundled with Type::Library a framework for organizing
21 type constraints into collections. Also bundled is Types::Standard, a
22 Moose-inspired library of useful type constraints. Type::Params is also
23 provided, to allow very fast checking and coercion of function and
24 method parameters.
25
26 The following example gives you an idea of some of the features of
27 these modules. If you don't understand it all, that's fine; that's what
28 the rest of the manual is for. Although the example uses Moo, the "use
29 Moo" could be changed to "use Moose" or "use Mouse" and it would still
30 work.
31
32 use v5.12;
33 use strict;
34 use warnings;
35
36 package Horse {
37 use Moo;
38 use Types::Standard qw( Str Int Enum ArrayRef InstanceOf );
39 use Type::Params qw( compile );
40 use namespace::autoclean;
41
42 has name => (
43 is => 'ro',
44 isa => Str,
45 required => 1,
46 );
47 has gender => (
48 is => 'ro',
49 isa => Enum[qw( f m )],
50 );
51 has age => (
52 is => 'rw',
53 isa => Int->where( '$_ >= 0' ),
54 );
55 has children => (
56 is => 'ro',
57 isa => ArrayRef[ InstanceOf['Horse'] ],
58 default => sub { return [] },
59 );
60
61 sub add_child {
62 # method signature
63 state $check = compile( InstanceOf['Horse'], InstanceOf['Horse'] );
64
65 my ($self, $child) = $check->(@_); # unpack @_
66 push @{ $self->children }, $child;
67
68 return $self;
69 }
70 }
71
72 package main;
73
74 my $boldruler = Horse->new(
75 name => "Bold Ruler",
76 gender => 'm',
77 age => 16,
78 );
79
80 my $secretariat = Horse->new(
81 name => "Secretariat",
82 gender => 'm',
83 age => 0,
84 );
85
86 $boldruler->add_child( $secretariat );
87
88 use Types::Standard qw( is_Object assert_Object );
89
90 # is_Object will return a boolean
91 #
92 if ( is_Object($boldruler) ) {
93 say $boldruler->name;
94 }
95
96 # assert_Object will return $secretariat or die
97 #
98 say assert_Object($secretariat)->name;
99
101 Even if you are using Type::Tiny with other object-oriented programming
102 toolkits (such as Moose or Mouse), you should start with the Moo
103 sections of the manual. Most of the information is directly
104 transferrable and the Moose and Mouse sections of the manual list the
105 minor differences between using Type::Tiny with Moo and with them.
106
107 In general, this manual assumes you use Perl 5.12 or above and may use
108 examples that do not work on older versions of Perl. Type::Tiny does
109 work on earlier versions of Perl, but not all the examples and features
110 in the manual will run without adjustment. (For instance, you may need
111 to replace "state" variables with lexical variables, avoid the "package
112 NAME { BLOCK }" syntax, etc.)
113
114 • Type::Tiny::Manual::Installation
115
116 How to install Type::Tiny. If Type::Tiny is already installed, you
117 can skip this.
118
119 • Type::Tiny::Manual::UsingWithMoo
120
121 Basic use of Type::Tiny with Moo, including attribute type
122 constraints, parameterized type constraints, coercions, and method
123 parameter checking.
124
125 • Type::Tiny::Manual::UsingWithMoo2
126
127 Advanced use of Type::Tiny with Moo, including unions and
128 intersections, "stringifies_to", "numifies_to",
129 "with_attribute_values", and "where".
130
131 • Type::Tiny::Manual::UsingWithMoo3
132
133 There's more than one way to do it! Alternative ways of using
134 Type::Tiny, including type registries, exported functions, and
135 "dwim_type".
136
137 • Type::Tiny::Manual::Libraries
138
139 Defining your own type libraries, including extending existing
140 libraries, defining new types, adding coercions, defining
141 parameterizable types, and the declarative style.
142
143 • Type::Tiny::Manual::UsingWithMoose
144
145 How to use Type::Tiny with Moose, including the advantages of
146 Type::Tiny over built-in type constraints, and Moose-specific
147 features.
148
149 • Type::Tiny::Manual::UsingWithMouse
150
151 How to use Type::Tiny with Mouse, including the advantages of
152 Type::Tiny over built-in type constraints, and Mouse-specific
153 features.
154
155 • Type::Tiny::Manual::UsingWithClassTiny
156
157 Including how to Type::Tiny in your object's "BUILD" method, and
158 third-party shims between Type::Tiny and Class::Tiny.
159
160 • Type::Tiny::Manual::UsingWithOther
161
162 Using Type::Tiny with Class::InsideOut, Params::Check, and
163 Object::Accessor.
164
165 • Type::Tiny::Manual::UsingWithTestMore
166
167 Type::Tiny for test suites.
168
169 • Type::Tiny::Manual::Params
170
171 Advanced information on Type::Params, and using Type::Tiny with
172 other signature modules like Function::Parameters and Kavorka.
173
174 • Type::Tiny::Manual::NonOO
175
176 Type::Tiny in non-object-oriented code.
177
178 • Type::Tiny::Manual::Optimization
179
180 Squeeze the most out of your CPU.
181
182 • Type::Tiny::Manual::Coercions
183
184 Advanced information on coercions.
185
186 • Type::Tiny::Manual::AllTypes
187
188 An alphabetical list of all type constraints bundled with
189 Type::Tiny.
190
191 • Type::Tiny::Manual::Policies
192
193 Policies related to Type::Tiny development.
194
195 • Type::Tiny::Manual::Contributing
196
197 Contributing to Type::Tiny development.
198
200 Please report any bugs to
201 <https://github.com/tobyink/p5-type-tiny/issues>.
202
204 The Type::Tiny homepage <https://typetiny.toby.ink/>.
205
207 Toby Inkster <tobyink@cpan.org>.
208
210 This software is copyright (c) 2013-2014, 2017-2021 by Toby Inkster.
211
212 This is free software; you can redistribute it and/or modify it under
213 the same terms as the Perl 5 programming language system itself.
214
216 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
217 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
218 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
219
220
221
222perl v5.32.1 2021-04-27 Type::Tiny::Manual(3)