File: Manual.pod

package info (click to toggle)
libtype-tiny-perl 2.002001-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,948 kB
  • sloc: perl: 14,610; makefile: 2; sh: 1
file content (233 lines) | stat: -rw-r--r-- 6,685 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
=pod

=encoding utf-8

=head1 NAME

Type::Tiny::Manual - an overview of Type::Tiny

=head1 SYNOPSIS

L<Type::Tiny> is a small L<Perl|http://www.perl.org/> class for writing
type constraints, inspired by L<Moose>'s type constraint API and
L<MooseX::Types>. It has only one non-core dependency (and even that is
simply a module that was previously distributed as part of Type::Tiny
but has since been spun off), and can be used with L<Moose>, L<Mouse>,
or L<Moo> (or none of the above).

Type::Tiny is used by over 800 Perl distributions on the CPAN (Comprehensive
Perl Archive Network) and can be considered a stable and mature framework for
efficiently and reliably enforcing data types.

Type::Tiny is bundled with L<Type::Library> a framework for organizing type
constraints into collections. Also bundled is L<Types::Standard>, a
Moose-inspired library of useful type constraints. L<Type::Params> is also
provided, to allow very fast checking and coercion of function and method
parameters.

The following example gives you an idea of some of the features of these
modules. If you don't understand it all, that's fine; that's what the rest
of the manual is for. Although the example uses Moo, the C<use Moo> could
be changed to C<use Moose> or C<use Mouse> and it would still work.

 use v5.12;
 use strict;
 use warnings;
 
 package Horse {
   use Moo;
   use Types::Standard qw( Str Int Enum ArrayRef InstanceOf );
   use Type::Params qw( signature );
   use namespace::autoclean;
   
   has name => (
     is       => 'ro',
     isa      => Str,
     required => 1,
   );
   has gender => (
     is       => 'ro',
     isa      => Enum[qw( f m )],
   );
   has age => (
     is       => 'rw',
     isa      => Int->where( '$_ >= 0' ),
   );
   has children => (
     is       => 'ro',
     isa      => ArrayRef[ InstanceOf['Horse'] ],
     default  => sub { return [] },
   );
   
   sub add_child {
     state $check = signature(
       method     => Object,
       positional => [ InstanceOf['Horse'] ]
     );
     
     my ( $self, $child ) = $check->(@_);   # unpack @_
     push @{ $self->children }, $child;
     
     return $self;
   }
 }
 
 package main;
 
 my $boldruler = Horse->new(
   name    => "Bold Ruler",
   gender  => 'm',
   age     => 16,
 );
 
 my $secretariat = Horse->new(
   name    => "Secretariat",
   gender  => 'm',
   age     => 0,
 );
 
 $boldruler->add_child( $secretariat );
 
 use Types::Standard qw( is_Object assert_Object );
 
 # is_Object will return a boolean
 #
 if ( is_Object($boldruler) ) {
   say $boldruler->name;
 }
 
 # assert_Object will return $secretariat or die
 #
 say assert_Object( $secretariat )->name;

=head1 MANUAL

Even if you are using Type::Tiny with other object-oriented programming
toolkits (such as Moose or Mouse), you should start with the Moo sections
of the manual. Most of the information is directly transferrable and the
Moose and Mouse sections of the manual list the minor differences between
using Type::Tiny with Moo and with them.

In general, this manual assumes you use Perl 5.12 or above and may use
examples that do not work on older versions of Perl. Type::Tiny does work
on earlier versions of Perl, but not all the examples and features in
the manual will run without adjustment. (For instance, you may need to
replace C<state> variables with lexical variables, avoid the
C<< package NAME { BLOCK } >> syntax, etc.)

=over

=item * L<Type::Tiny::Manual::Installation>

How to install Type::Tiny. If Type::Tiny is already installed, you can
skip this. 

=item * L<Type::Tiny::Manual::UsingWithMoo>

Basic use of Type::Tiny with Moo, including attribute type constraints,
parameterized type constraints, coercions, and method parameter checking.

=item * L<Type::Tiny::Manual::UsingWithMoo2>

Advanced use of Type::Tiny with Moo, including unions and intersections,
C<stringifies_to>, C<numifies_to>, C<with_attribute_values>, and C<where>.

=item * L<Type::Tiny::Manual::UsingWithMoo3>

There's more than one way to do it! Alternative ways of using Type::Tiny,
including type registries, exported functions, and C<dwim_type>.

=item * L<Type::Tiny::Manual::Libraries>

Defining your own type libraries, including extending existing libraries,
defining new types, adding coercions, defining parameterizable types,
and the declarative style.

=item * L<Type::Tiny::Manual::UsingWithMoose>

How to use Type::Tiny with Moose, including the advantages of Type::Tiny
over built-in type constraints, and Moose-specific features.

=item * L<Type::Tiny::Manual::UsingWithMouse>

How to use Type::Tiny with Mouse, including the advantages of Type::Tiny
over built-in type constraints, and Mouse-specific features.

=item * L<Type::Tiny::Manual::UsingWithMite>

How to use Type::Tiny with Mite, including how to write an entire Perl
project using clean Moose-like code and no non-core dependencies.
(Not even dependencies on Mite or Type::Tiny!)

=item * L<Type::Tiny::Manual::UsingWithClassTiny>

Including how to Type::Tiny in your object's C<BUILD> method, and
third-party shims between Type::Tiny and Class::Tiny.

=item * L<Type::Tiny::Manual::UsingWithOther>

Using Type::Tiny with Class::InsideOut, Params::Check, and Object::Accessor.

=item * L<Type::Tiny::Manual::UsingWithTestMore>

Type::Tiny for test suites.

=item * L<Type::Tiny::Manual::Params>

Advanced information on Type::Params, and using Type::Tiny with other
signature modules like Function::Parameters and Kavorka.

=item * L<Type::Tiny::Manual::NonOO>

Type::Tiny in non-object-oriented code.

=item * L<Type::Tiny::Manual::Optimization>

Squeeze the most out of your CPU.

=item * L<Type::Tiny::Manual::Coercions>

Advanced information on coercions.

=item * L<Type::Tiny::Manual::AllTypes>

An alphabetical list of all type constraints bundled with Type::Tiny.

=item * L<Type::Tiny::Manual::Policies>

Policies related to Type::Tiny development.

=item * L<Type::Tiny::Manual::Contributing>

Contributing to Type::Tiny development.

=back

=head1 BUGS

Please report any bugs to
L<https://github.com/tobyink/p5-type-tiny/issues>.

=head1 SEE ALSO

L<The Type::Tiny homepage|https://typetiny.toby.ink/>.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=head1 DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

=cut