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