1Type::Tiny::Manual::NonUOsOe(r3)Contributed Perl DocumenTtyaptei:o:nTiny::Manual::NonOO(3)
2
3
4

NAME

6       Type::Tiny::Manual::NonOO - Type::Tiny in non-object-oriented code
7

MANUAL

9       Although Type::Tiny was designed with object-oriented programming in
10       mind, especially Moose-style classes and roles, it can be used in
11       procedural and imperative programming.
12
13       If you have read Type::Tiny::Manual::UsingWithMoo, you should
14       understand how Type::Params can be used to validate method parameters.
15       This same technique can be applied to regular subs too. More
16       information about checking parameters can be found in
17       Type::Tiny::Manual::Params.
18
19       The "is_*" and "assert_*" functions exported by type libraries may be
20       useful in non-OO code too. See Type::Tiny::Manual::UsingWithMoo3.
21
22   Type::Tiny and Smart Match
23       Perl 5.10 introduced the smart match operator "~~", which has since
24       been deprecated because though the general idea is fairly sound, the
25       details were a bit messy.
26
27       Nevertheless, Type::Tiny has support for smart match and I'm
28       documenting it here because there's nowhere better to put it.
29
30       The following can be used as to check if a value passes a type
31       constraint:
32
33         $value ~~ SomeType
34
35       Where it gets weird is if $value is an object and overloads "~~".
36       Which overload of "~~" wins? I don't know.
37
38       Better to use:
39
40         SomeType->check( $value )   # more reliable, probably faster
41         is_SomeType($value)         # more reliable, definitely faster
42
43       It's also possible to do:
44
45         $value ~~ SomeType->coercion
46
47       This checks to see if $value matches any type that can be coerced to
48       SomeType.
49
50       But better to use:
51
52         SomeType->coercion->has_coercion_for_value( $value )
53
54   "given" and "when"
55       Related to the smart match operator is the "given"/"when" syntax.
56
57       This will not do what you want it to do:
58
59         use Types::Standard qw( Str Int );
60
61         given ($value) {
62           when (Int) { ... }
63           when (Str) { ... }
64         }
65
66       This will do what you wanted:
67
68         use Types::Standard qw( is_Str is_Int );
69
70         given ($value) {
71           when (\&is_Int) { ... }
72           when (\&is_Str) { ... }
73         }
74
75       Sorry, that's just how Perl be.
76
77       Better though:
78
79         use Types::Standard qw( Str Int );
80         use Type::Utils qw( match_on_type );
81
82         match_on_type $value => (
83           Str, sub { ... },
84           Int, sub { ... },
85         );
86
87       If this is part of a loop or other frequently called bit of code, you
88       can compile the checks once and use them many times:
89
90         use Types::Standard qw( Str Int );
91         use Type::Utils qw( compile_match_on_type );
92
93         my $dispatch_table = compile_match_on_type(
94           Str, sub { ... },
95           Int, sub { ... },
96         );
97
98         $dispatch_table->($_) for @lots_of_values;
99
100       As with most things in Type::Tiny, those coderefs can be replaced by
101       strings of Perl code.
102

NEXT STEPS

104       Here's your next step:
105
106       •   Type::Tiny::Manual::Optimization
107
108           Squeeze the most out of your CPU.
109

AUTHOR

111       Toby Inkster <tobyink@cpan.org>.
112
114       This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.
115
116       This is free software; you can redistribute it and/or modify it under
117       the same terms as the Perl 5 programming language system itself.
118

DISCLAIMER OF WARRANTIES

120       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
121       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
122       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
123
124
125
126perl v5.36.0                      2023-01-04      Type::Tiny::Manual::NonOO(3)
Impressum