Exploring Type::Tiny Part 1: Using Type::Params for Validating Function Parameters
Type::Tiny is probably best known as a way of having Moose-like type constraints in Moo, but it can be used for so much more. This is the first in a series of posts showing other things you can use Type::Tiny for.
Let's imagine you have a function which takes three parameters, a colour, a string of text, and a filehandle. Something like this:
sub htmlprint { my %arg = @_; $arg{file}->printf( '<span style="color:%s">%s</span>', $arg{colour}, $arg{text}, ); }
Nice little function. Simple enough. But if people call it like this:
htmlprint( file => $fh, text => "Hello world", color => "red" );
... then they'll get weird and unexpected behaviour. Have you spotted the mistake?