This is an old article. Some links may be broken. Some of the information may no longer be correct. Opinions expressed in this article may no longer be held.
Or 1.002000, because it uses Moo-like versioning.
The Type::Tiny 1.1 (1.001_00x) development cycle has been going on since September 2014. Apparently I’m either very concerned about stability or very lazy. You can make up your own minds about that.
But Type::Tiny 1.2 should be released in a few weeks. If your application uses Type::Tiny, you may want to download the latest development release and check that nothing breaks. (It shouldn’t, but you never know until you try.)
The headline changes are:
- Type::Params now has
compile_named
andvalidate_named
. - Type::Tiny’s
constraint
parameter may be a string of code. - Fixed bug where Types::Standard::Int would sometimes accept an overloaded object. (It never should.)
- Various performance enhancements and bug fixes.
I’ll explain the first two in more detail, because they’re interesting.
compile_named
Type::Params is a module for type-checking the parameters to functions. For example, specifying that the $quantity
parameter should be an integer. It was mostly written with positional parameters in mind, like eat_apples(2, "red")
.
Named parameters like eat_apples( quantity=>2, colour=>"red" )
could be made to work, but it was a bit of a hack. The Type::Tiny 1.1 development versions introduced a better way of doing this. It’s not only neater, but it provides better error messages and it benchmarks a lot faster. Below is some code showing the old way and the new way.
String Constraints
Normally when creating a type constraint, you'd provide a coderef which checks the variable $_
to see if it should pass the constraint. Recently the development versions of Type::Tiny have started accepting a string of Perl code instead. This can not only lead to very concise code, but is an easy way to allow Type::Tiny to optimize its checks. (You can manually optimize them even better by telling Type::Tiny how to inline type checks, but that requires a little bit of extra effort.)