.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Type::Tiny::Manual::NonOO 3pm" .TH Type::Tiny::Manual::NonOO 3pm "2023-07-21" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Type::Tiny::Manual::NonOO \- Type::Tiny in non\-object\-oriented code .SH "MANUAL" .IX Header "MANUAL" Although Type::Tiny was designed with object-oriented programming in mind, especially Moose-style classes and roles, it can be used in procedural and imperative programming. .PP If you have read Type::Tiny::Manual::UsingWithMoo, you should understand how Type::Params can be used to validate method parameters. This same technique can be applied to regular subs too. More information about checking parameters can be found in Type::Tiny::Manual::Params. .PP The \f(CW\*(C`is_*\*(C'\fR and \f(CW\*(C`assert_*\*(C'\fR functions exported by type libraries may be useful in non-OO code too. See Type::Tiny::Manual::UsingWithMoo3. .SS "Type::Tiny and Smart Match" .IX Subsection "Type::Tiny and Smart Match" Perl 5.10 introduced the smart match operator \f(CW\*(C`~~\*(C'\fR, which has since been deprecated because though the general idea is fairly sound, the details were a bit messy. .PP Nevertheless, Type::Tiny has support for smart match and I'm documenting it here because there's nowhere better to put it. .PP The following can be used as to check if a value passes a type constraint: .PP .Vb 1 \& $value ~~ SomeType .Ve .PP Where it gets weird is if \f(CW$value\fR is an object and overloads \f(CW\*(C`~~\*(C'\fR. Which overload of \f(CW\*(C`~~\*(C'\fR wins? I don't know. .PP Better to use: .PP .Vb 2 \& SomeType\->check( $value ) # more reliable, probably faster \& is_SomeType($value) # more reliable, definitely faster .Ve .PP It's also possible to do: .PP .Vb 1 \& $value ~~ SomeType\->coercion .Ve .PP This checks to see if \f(CW$value\fR matches any type that can be coerced to \fBSomeType\fR. .PP But better to use: .PP .Vb 1 \& SomeType\->coercion\->has_coercion_for_value( $value ) .Ve .ie n .SS """given"" and ""when""" .el .SS "\f(CWgiven\fP and \f(CWwhen\fP" .IX Subsection "given and when" Related to the smart match operator is the \f(CW\*(C`given\*(C'\fR/\f(CW\*(C`when\*(C'\fR syntax. .PP This will not do what you want it to do: .PP .Vb 1 \& use Types::Standard qw( Str Int ); \& \& given ($value) { \& when (Int) { ... } \& when (Str) { ... } \& } .Ve .PP This will do what you wanted: .PP .Vb 1 \& use Types::Standard qw( is_Str is_Int ); \& \& given ($value) { \& when (\e&is_Int) { ... } \& when (\e&is_Str) { ... } \& } .Ve .PP Sorry, that's just how Perl be. .PP Better though: .PP .Vb 2 \& use Types::Standard qw( Str Int ); \& use Type::Utils qw( match_on_type ); \& \& match_on_type $value => ( \& Str, sub { ... }, \& Int, sub { ... }, \& ); .Ve .PP If this is part of a loop or other frequently called bit of code, you can compile the checks once and use them many times: .PP .Vb 2 \& use Types::Standard qw( Str Int ); \& use Type::Utils qw( compile_match_on_type ); \& \& my $dispatch_table = compile_match_on_type( \& Str, sub { ... }, \& Int, sub { ... }, \& ); \& \& $dispatch_table\->($_) for @lots_of_values; .Ve .PP As with most things in Type::Tiny, those coderefs can be replaced by strings of Perl code. .SH "NEXT STEPS" .IX Header "NEXT STEPS" Here's your next step: .IP "\(bu" 4 Type::Tiny::Manual::Optimization .Sp Squeeze the most out of your \s-1CPU.\s0 .SH "AUTHOR" .IX Header "AUTHOR" Toby Inkster . .SH "COPYRIGHT AND LICENCE" .IX Header "COPYRIGHT AND LICENCE" This software is copyright (c) 2013\-2014, 2017\-2023 by Toby Inkster. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. .SH "DISCLAIMER OF WARRANTIES" .IX Header "DISCLAIMER OF WARRANTIES" \&\s-1THIS PACKAGE IS PROVIDED \*(L"AS IS\*(R" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.\s0