1Type::Tiny::Manual::ParUasmesr(3C)ontributed Perl DocumeTnytpaet:i:oTniny::Manual::Params(3)
2
3
4

NAME

6       Type::Tiny::Manual::Params - coerce and validate arguments to functions
7       and methods
8

DESCRIPTION

10       There is a module called Type::Params available to wrap up type
11       coercion and constraint checks into a single, simple and fast check. If
12       you care about speed, and your sub signatures are fairly simple, then
13       this is the way to go...
14
15          use feature qw( state );
16          use Types::Standard qw( Str );
17          use Type::Utils;
18          use Type::Params qw( compile );
19
20          my $Invocant = class_type { class => __PACKAGE__ };
21
22          sub set_name
23          {
24             state $check = compile($Invocant, Str);
25             my ($self, $name) = $check->(@_);
26
27             ...;
28          }
29
30       See the COOKBOOK section of Type::Params for further information.
31
32   The Somewhat More Manual Way...
33       In general, Type::Params should be sufficient to cover most needs, and
34       will probably run faster than almost anything you could cook up
35       yourself.  However, sometimes you need to deal with unusual function
36       signatures that it does not support. For example, imagine function
37       "format_string" takes an optional hashref of formatting instructions,
38       followed by a required string. You might expect to be able to handle it
39       like this:
40
41          sub format_string
42          {
43             state $check = compile(Optional[HashRef], Str);
44             my ($instructions, $string) = $check->(@_);
45
46             ...;
47          }
48
49       However, this won't work, as Type::Params expects required parameters
50       to always precede optional ones. So there are times you need to handle
51       parameters more manually.
52
53       In these cases, bear in mind that for any type constraint object you
54       have several useful checking methods available:
55
56         Str->check($var)            # returns a boolean
57         is_Str($var)                # ditto
58         Str->($var)                 # returns $var or dies
59         assert_Str($var)            # ditto
60
61       Here's how you might handle the "format_string" function:
62
63          sub format_string
64          {
65             my $instructions;
66             $instructions = shift if HashRef->check($_[0]);
67
68             my $string = Str->(shift);
69
70             ...;
71          }
72
73       Alternatively, you could manipulate @_ before passing it to the
74       compiled check:
75
76          sub format_string
77          {
78             state $check = compile(HashRef, Str);
79             my ($instructions, $str) = $check->(@_==1 ? ({}, @_) : @_);
80
81             ...;
82          }
83
84   Signatures
85       Don't you wish your subs could look like this?
86
87          sub set_name (Object $self, Str $name)
88          {
89             $self->{name} = $name;
90          }
91
92       Well; here are a few solutions for sub signatures that work with
93       Type::Tiny...
94
95       Kavorka
96
97       Kavorka is a sub signatures implementation written to natively use
98       Type::Utils' "dwim_type" for type constraints, and take advantage of
99       Type::Tiny's features such as inlining, and coercions.
100
101          method set_name (Str $name)
102          {
103             $self->{name} = $name;
104          }
105
106       Kavorka's signatures provide a lot more flexibility, and slightly more
107       speed than Type::Params. (The speed comes from inlining almost all type
108       checks into the body of the sub being declared.)
109
110       Kavorka also includes support for type checking of the returned value.
111
112       Kavorka can also be used as part of Moops, a larger framework for
113       object oriented programming in Perl.
114
115       Function::Parameters
116
117       The following should work with Function::Parameters 1.0201 or above:
118
119          use Type::Utils;
120          use Function::Parameters {
121             method => {
122                strict     => 1,
123                reify_type => sub { Type::Utils::dwim_type($_[0]) },
124             },
125          };
126
127          method set_name (Str $name)
128          {
129             $self->{name} = $name;
130          }
131
132       Note that by default, Function::Parameters uses Moose's type
133       constraints. The "reify_type" option above (introduced in
134       Function::Parameters 1.0201) allows you to "divert" type constraint
135       lookups. Using Type::Tiny constraints will gain you about a 7% speed-up
136       in function signature checks.
137
138       An alternative way to use Function::Parameter with Type::Tiny is to
139       provide type constraint expressions in parentheses:
140
141          use Types::Standard;
142          use Function::Parameters ':strict';
143
144          method set_name ((Str) $name)
145          {
146             $self->{name} = $name;
147          }
148
149       Attribute::Contract
150
151       Both Kavorka and Function::Parameters require a relatively recent
152       version of Perl. Attribute::Contract supports older versions by using a
153       lot less magic.
154
155       You want Attribute::Contract 0.03 or above.
156
157          use Attribute::Contract -types => [qw/Object Str/];
158
159          sub set_name :ContractRequires(Object, Str)
160          {
161             my ($self, $name) = @_;
162             $self->{name} = $name;
163          }
164
165       Attribute::Contract also includes support for type checking of the
166       returned value.
167

AUTHOR

169       Toby Inkster <tobyink@cpan.org>.
170
172       This software is copyright (c) 2013-2014, 2017-2019 by Toby Inkster.
173
174       This is free software; you can redistribute it and/or modify it under
175       the same terms as the Perl 5 programming language system itself.
176

DISCLAIMER OF WARRANTIES

178       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
179       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
180       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
181
182
183
184perl v5.28.1                      2019-01-08     Type::Tiny::Manual::Params(3)
Impressum