1Type::Tiny::Manual::UsiUnsgeWritChoCnltarsisbTuitneydT(y3Pp)eer:l:TDioncyu:m:eMnatnautailo:n:UsingWithClassTiny(3)
2
3
4
6 Type::Tiny::Manual::UsingWithClassTiny - use of Type::Tiny with
7 Class::Tiny
8
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);
39 use Type::Params qw( wrap_methods compile );
40 use namespace::autoclean;
41
42 # type checks
43 wrap_methods(
44 BUILD => [Dict[
45 name => Str,
46 gender => Optional[Str],
47 age => Optional[Num],
48 children => Optional[ArrayRef],
49 slurpy Any,
50 ]],
51 name => [],
52 gender => [],
53 age => Optional[Num],
54 children => [],
55 );
56 }
57
58 What's going on here?
59
60 Well, Class::Tiny, after it has built a new object, will do this:
61
62 $self->BUILD($args);
63
64 (Technically, it calls "BUILD" not just for the current class, but for
65 all parent classes too.) We can hook onto this in order to check type
66 constraints for the constructor.
67
68 We use "wrap_methods" from Type::Params to wrap the original "BUILD"
69 method (which doesn't exist, so "wrap_methods" will just assume an
70 empty sub) with a type check for $args. The type check is just a Dict
71 that checks the class's required and optional attributes and includes
72 slurpy Any at the end to be flexible for subclasses adding new
73 attributes.
74
75 Then we wrap the "name", "gender", and "children" methods with checks
76 to make sure they're only being called as getters, and we wrap "age",
77 allowing it to be called as a setter with a Num.
78
79 There are also a couple of CPAN modules that can help you out.
80
81 Class::Tiny::ConstrainedAccessor
82 Class::Tiny::ConstrainedAccessor creates a "BUILD" and accessors that
83 enforce Type::Tiny constraints. Attribute types are passed to
84 Class::Tiny::ConstrainedAccessor; attribute defaults are passed to
85 Class::Tiny.
86
87 package Horse {
88 use Types::Standard qw( Str Num ArrayRef );
89 use Class::Tiny::ConstrainedAccessor {
90 name => Str,
91 gender => Str,
92 age => Num,
93 children => ArrayRef,
94 };
95 use Class::Tiny qw( gender age ), {
96 name => sub { die "name is required"; },
97 children => sub { return [] },
98 };
99 }
100
101 Class::Tiny::Antlers
102 Class::Tiny::Antlers provides Moose-like syntax for Class::Tiny,
103 including support for "isa". You do not also need to use Class::Tiny
104 itself.
105
106 package Horse {
107 use Class::Tiny::Antlers qw(has);
108 use Types::Standard qw( Str Num ArrayRef );
109 use namespace::autoclean;
110
111 has name => (
112 is => 'ro',
113 isa => Str,
114 default => sub { die "name is required" },
115 );
116 has gender => ( is => 'ro', isa => Str );
117 has age => ( is => 'rw', isa => Num );
118 has children => (
119 is => 'ro',
120 isa => ArrayRef,
121 default => sub { return [] },
122 );
123 }
124
126 Here's your next step:
127
128 • Type::Tiny::Manual::UsingWithOther
129
130 Using Type::Tiny with Class::InsideOut, Params::Check, and
131 Object::Accessor.
132
134 Toby Inkster <tobyink@cpan.org>.
135
137 This software is copyright (c) 2013-2014, 2017-2021 by Toby Inkster.
138
139 This is free software; you can redistribute it and/or modify it under
140 the same terms as the Perl 5 programming language system itself.
141
143 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
144 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
145 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
146
147
148
149perl v5.32.1 2021T-y0p4e-:2:7Tiny::Manual::UsingWithClassTiny(3)