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( signature );
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 state $check = signature(
63 method => Object,
64 positional => [ InstanceOf['Horse'] ]
65 );
66
67 my ( $self, $child ) = $check->(@_); # unpack @_
68 push @{ $self->children }, $child;
69
70 return $self;
71 }
72 }
73
74 package main;
75
76 my $boldruler = Horse->new(
77 name => "Bold Ruler",
78 gender => 'm',
79 age => 16,
80 );
81
82 my $secretariat = Horse->new(
83 name => "Secretariat",
84 gender => 'm',
85 age => 0,
86 );
87
88 $boldruler->add_child( $secretariat );
89
90 use Types::Standard qw( is_Object assert_Object );
91
92 # is_Object will return a boolean
93 #
94 if ( is_Object($boldruler) ) {
95 say $boldruler->name;
96 }
97
98 # assert_Object will return $secretariat or die
99 #
100 say assert_Object( $secretariat )->name;
101
103 Even if you are using Type::Tiny with other object-oriented programming
104 toolkits (such as Moose or Mouse), you should start with the Moo
105 sections of the manual. Most of the information is directly
106 transferrable and the Moose and Mouse sections of the manual list the
107 minor differences between using Type::Tiny with Moo and with them.
108
109 In general, this manual assumes you use Perl 5.12 or above and may use
110 examples that do not work on older versions of Perl. Type::Tiny does
111 work on earlier versions of Perl, but not all the examples and features
112 in the manual will run without adjustment. (For instance, you may need
113 to replace "state" variables with lexical variables, avoid the "package
114 NAME { BLOCK }" syntax, etc.)
115
116 • Type::Tiny::Manual::Installation
117
118 How to install Type::Tiny. If Type::Tiny is already installed, you
119 can skip this.
120
121 • Type::Tiny::Manual::UsingWithMoo
122
123 Basic use of Type::Tiny with Moo, including attribute type
124 constraints, parameterized type constraints, coercions, and method
125 parameter checking.
126
127 • Type::Tiny::Manual::UsingWithMoo2
128
129 Advanced use of Type::Tiny with Moo, including unions and
130 intersections, "stringifies_to", "numifies_to",
131 "with_attribute_values", and "where".
132
133 • Type::Tiny::Manual::UsingWithMoo3
134
135 There's more than one way to do it! Alternative ways of using
136 Type::Tiny, including type registries, exported functions, and
137 "dwim_type".
138
139 • Type::Tiny::Manual::Libraries
140
141 Defining your own type libraries, including extending existing
142 libraries, defining new types, adding coercions, defining
143 parameterizable types, and the declarative style.
144
145 • Type::Tiny::Manual::UsingWithMoose
146
147 How to use Type::Tiny with Moose, including the advantages of
148 Type::Tiny over built-in type constraints, and Moose-specific
149 features.
150
151 • Type::Tiny::Manual::UsingWithMouse
152
153 How to use Type::Tiny with Mouse, including the advantages of
154 Type::Tiny over built-in type constraints, and Mouse-specific
155 features.
156
157 • Type::Tiny::Manual::UsingWithMite
158
159 How to use Type::Tiny with Mite, including how to write an entire
160 Perl project using clean Moose-like code and no non-core
161 dependencies. (Not even dependencies on Mite or Type::Tiny!)
162
163 • Type::Tiny::Manual::UsingWithClassTiny
164
165 Including how to Type::Tiny in your object's "BUILD" method, and
166 third-party shims between Type::Tiny and Class::Tiny.
167
168 • Type::Tiny::Manual::UsingWithOther
169
170 Using Type::Tiny with Class::InsideOut, Params::Check, and
171 Object::Accessor.
172
173 • Type::Tiny::Manual::UsingWithTestMore
174
175 Type::Tiny for test suites.
176
177 • Type::Tiny::Manual::Params
178
179 Advanced information on Type::Params, and using Type::Tiny with
180 other signature modules like Function::Parameters and Kavorka.
181
182 • Type::Tiny::Manual::NonOO
183
184 Type::Tiny in non-object-oriented code.
185
186 • Type::Tiny::Manual::Optimization
187
188 Squeeze the most out of your CPU.
189
190 • Type::Tiny::Manual::Coercions
191
192 Advanced information on coercions.
193
194 • Type::Tiny::Manual::AllTypes
195
196 An alphabetical list of all type constraints bundled with
197 Type::Tiny.
198
199 • Type::Tiny::Manual::Policies
200
201 Policies related to Type::Tiny development.
202
203 • Type::Tiny::Manual::Contributing
204
205 Contributing to Type::Tiny development.
206
208 Please report any bugs to
209 <https://github.com/tobyink/p5-type-tiny/issues>.
210
212 The Type::Tiny homepage <https://typetiny.toby.ink/>.
213
215 Toby Inkster <tobyink@cpan.org>.
216
218 This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.
219
220 This is free software; you can redistribute it and/or modify it under
221 the same terms as the Perl 5 programming language system itself.
222
224 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
225 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
226 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
227
228
229
230perl v5.36.0 2023-01-04 Type::Tiny::Manual(3)