1Type::Tiny::Manual::UsiUnsgeWritChoCnltarsisbTuitneydT(y3Pp)eer:l:TDioncyu:m:eMnatnautailo:n:UsingWithClassTiny(3)
2
3
4

NAME

6       Type::Tiny::Manual::UsingWithClassTiny - use of Type::Tiny with
7       Class::Tiny
8

MANUAL

10       Class::Tiny is an even-smaller-than-Moo class builder.
11
12       Let's translate the classic Horse class from Moo to Class::Tiny.
13
14       Moo:
15
16         package Horse {
17           use Moo;
18           use Types::Standard qw( Str Num ArrayRef );
19           use namespace::autoclean;
20
21           has name       => ( is => 'ro', isa => Str, required => 1 );
22           has gender     => ( is => 'ro', isa => Str );
23           has age        => ( is => 'rw', isa => Num );
24           has children   => (
25             is       => 'ro',
26             isa      => ArrayRef,
27             default  => sub { return [] },
28           );
29         }
30
31       Class::Tiny:
32
33         package Horse {
34           use Class::Tiny qw( gender age ), {
35             name     => sub { die "name is required"; },
36             children => sub { return [] },
37           };
38           use Types::Standard qw( Str Num ArrayRef Dict Optional Slurpy Any Object );
39           use Type::Params qw( signature_for );
40           use namespace::autoclean;
41
42           # type checks
43           signature_for BUILD => (
44             method => Object,
45             named  => [
46               name       => Str,
47               gender     => Optional[Str],
48               age        => Optional[Num],
49               children   => Optional[ArrayRef],
50               ()         => Slurpy[Any],
51             ],
52             fallback => 1,
53           );
54           signature_for [ 'name', 'gender', 'children' ] => (
55             method     => Object,
56             positional => [],
57           );
58           signature_for age => (
59             method     => Object,
60             positional => [ Optional[Num] ],
61           );
62         }
63
64       What's going on here?
65
66       Well, Class::Tiny, after it has built a new object, will do this:
67
68         $self->BUILD($args);
69
70       (Technically, it calls "BUILD" not just for the current class, but for
71       all parent classes too.) We can hook onto this in order to check type
72       constraints for the constructor.
73
74       We use "signature_for" from Type::Params to wrap the original "BUILD"
75       method (which doesn't exist, so "fallback => 1" will just assume an
76       empty sub) with a type check for its arguments. The type check is just
77       a Dict that checks the class's required and optional attributes and
78       includes Slurpy[Any] at the end to be flexible for subclasses adding
79       new attributes.
80
81       Then we wrap the "name", "gender", and "children" methods with checks
82       to make sure they're only being called as getters, and we wrap "age",
83       allowing it to be called as a setter with a Num.
84
85       There are also a couple of CPAN modules that can help you out.
86
87   Class::Tiny::ConstrainedAccessor
88       Class::Tiny::ConstrainedAccessor creates a "BUILD" and accessors that
89       enforce Type::Tiny constraints.  Attribute types are passed to
90       Class::Tiny::ConstrainedAccessor; attribute defaults are passed to
91       Class::Tiny.
92
93         package Horse {
94           use Types::Standard qw( Str Num ArrayRef );
95           use Class::Tiny::ConstrainedAccessor {
96             name     => Str,
97             gender   => Str,
98             age      => Num,
99             children => ArrayRef,
100           };
101           use Class::Tiny qw( gender age ), {
102             name     => sub { die "name is required"; },
103             children => sub { return [] },
104           };
105         }
106
107   Class::Tiny::Antlers
108       Class::Tiny::Antlers provides Moose-like syntax for Class::Tiny,
109       including support for "isa".  You do not also need to use Class::Tiny
110       itself.
111
112         package Horse {
113           use Class::Tiny::Antlers qw(has);
114           use Types::Standard qw( Str Num ArrayRef );
115           use namespace::autoclean;
116
117           has name       => (
118             is        => 'ro',
119             isa       => Str,
120             default   => sub { die "name is required" },
121           );
122           has gender     => ( is => 'ro',    isa => Str );
123           has age        => ( is => 'rw',    isa => Num );
124           has children   => (
125             is        => 'ro',
126             isa       => ArrayRef,
127             default   => sub { return [] },
128           );
129         }
130

NEXT STEPS

132       Here's your next step:
133
134       •   Type::Tiny::Manual::UsingWithOther
135
136           Using Type::Tiny with Class::InsideOut, Params::Check, and
137           Object::Accessor.
138

AUTHOR

140       Toby Inkster <tobyink@cpan.org>.
141
143       This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.
144
145       This is free software; you can redistribute it and/or modify it under
146       the same terms as the Perl 5 programming language system itself.
147

DISCLAIMER OF WARRANTIES

149       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
150       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
151       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
152
153
154
155perl v5.36.0                      2023T-y0p1e-:0:4Tiny::Manual::UsingWithClassTiny(3)
Impressum