Type::T...l::NonOO(3)
π NAME π
Type::Tiny::Manual::NonOO β bestie, this is Type::Tiny in non-object-oriented code β¨
π MANUAL π
okay so, real talk: Type::Tiny was literally designed with object-oriented programming in mind β especially Moose-style classes and roles β but it absolutely slays in procedural and imperative programming too. no cap. π
if you've already read Type::Tiny::Manual::UsingWithMoo, you already know how Type::Params validates method parameters. and the way that the exact same technique works on regular subs too?? iconic. if you want more on checking parameters, go read Type::Tiny::Manual::Params. π«Ά
also the is_* and assert_* functions that type libraries export? they are so useful in non-OO code as well. see Type::Tiny::Manual::UsingWithMoo3. π¦
β¨ Type::Tiny and Smart Match β¨
so Perl 5.10 introduced the smart match operator ~~, which has since been deprecated π β the general idea is fairly sound, but the details were a bit messy. fr fr.
but listen: Type::Tiny does support smart match, and I'm documenting it here because there's literally nowhere better to put it. π
you can use this to check if a value passes a type constraint:
$value ~~ SomeType
and here's where it gets weird: if $value is an object and overloads ~~, which overload of ~~ wins? I don't know. π
better to use:
SomeType->check( $value ) # more reliable, probably faster
is_SomeType($value) # more reliable, definitely faster
it's also possible to do:
$value ~~ SomeType->coercion
which checks to see if $value matches any type that can be coerced to SomeType.
but better to use:
SomeType->coercion->has_coercion_for_value( $value )
πΈ "given" and "when" πΈ
related to the smart match operator is the given/when syntax, bestie.
this will not do what you want it to do:
use Types::Standard qw( Str Int );
given ( $value ) {
when ( Int ) { ... }
when ( Str ) { ... }
}
this will do what you wanted:
use Types::Standard qw( is_Str is_Int );
given ( $value ) {
when ( \&is_Int ) { ... }
when ( \&is_Str ) { ... }
}
sorry, that's just how Perl be. π
better though:
use Types::Standard qw( Str Int );
use Type::Utils qw( match_on_type );
match_on_type $value => (
Str, sub { ... },
Int, sub { ... },
);
and if this is part of a loop or some other frequently called bit of code, you can compile the checks once and use them many times β highkey efficient π
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;
as with most things in Type::Tiny, those coderefs can be replaced by strings of Perl code. β¨
π NEXT STEPS π
here's your next step, bestie:
Type::Tiny::Manual::Optimization
Squeeze the most out of your CPU. π₯
π AUTHOR π
Toby Inkster <tobyink@cpan.org>.
π COPYRIGHT AND LICENCE π
This software is copyright (c) 2013-2014, 2017-2025 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
π«Ά DISCLAIMER OF WARRANTIES π«Ά
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
okay that's the whole page, mwah π this was perl v5.42.0, dated 2026-01-17, and it's the Type::Tiny::Manual::NonOO(3) page from the User Contributed Perl Documentation. go forth and validate, bestie π¦β¨
π¦ where this page comes from
- fedora 44: perl-Type-Tiny 2.010001-2.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms