So there's finally a version of MooseX::Types::Parameterizable on CPAN that I don't hate. This latest release (v 0.03) fixes up some documentation stuff, mostly clarifying how coercions work, and some additional tests. I also removed a few bad / unclear examples. The previous version (v 0.02) fixed major errors including some missing dependencies and fixed coercions to work correctly.
MooseX::Types::Parameterizable (previously blogged about here, here and here) is a type constraint that generates other type constraints that can be constrained with runtime parameters. The idea is to allow you to easily declare your own parameterized types that work similarly to the Moose builtins like HashRef and ArrayRef.
For example, How many times have you done the following:
subtype StrLessThanOrEqualTo40Chars,
as Str,
where { 40 >= length($_) };
subtype StrLessThanOrEqualTo60Chars,
as Str,
where { 60 >= length($_) };
subtype StrLessThanOrEqualTo100Chars,
as Str,
where { 100 >= length($_) };
Now, you can create a 'generic" Str type that accepts a parameter:
subtype MyStrWithLength,
as Parameterizable[Str, Int],
where {
my($string, $int) = @_;
$int >= length($string) ? 1:0;
};
And just use it:
MyStrWithLength[40]
More complicated parameterized types can be made, and coercions can also be applied. For example, you can create a type constraint that validates a given value is unique in a database. Feel free to fork on Github if you want to add tests or make changes.
Plans for the future include using this to build a new set of 'faceted' core types (enhance core Moose times to add all the types of parametric abilities you'd commonly use). This is going to probably be something like "MooseX::Types::Faceted" and I'll be working on that in a week or so, after I see that Parameterized is actually stable. Also, I'm started to plan for an "MooseX::Types::Declare" or similar to pretty up MooseX::Types based libraries, reduce boilerplate, etc.