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>.


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