A few weeks ago I wrote an article covering six ways to do subroutine signatures and type constraints in Perl. Some of the feedback I got indicated that there were more options to consider, so for the presentation I gave at Houston Perl Mongers I quickly summarized at the end seven more modules from CPAN that offer these features. Still more feedback showed that I had missed five more modules, so at the risk of becoming “the signatures guy” here they are.
First released in 2000 as part of a larger project, this may be the first general-purpose parameter parser to appear on CPAN. Although the documentation examples use it as a parent class, you should follow the suggestion further down and use its two methods
params_to_array directly from the class. Both take the same list of arguments, and only differ on whether they return a hash reference containing named parameters or an array reference containing positional parameters.
There are options for slurping extra passed values into another hash bucket and lowercasing named parameters, and it will automatically chop off any leading hyphens in your parameter names. You can also alias multiple named parameters to the same resulting argument. The module doesn't provide any facility for type validation, though.
This adds the interesting wrinkle of being configurable with an external JSON file; otherwise, it seems you have to call various methods on a validator object to add parameters (which it calls fields) or methods to validate. It doesn’t seem to support slurping extra values nor positional parameters, but its type validation lets you use arbitrary code references. The documentation could use a bit of work with more examples of real-world usage rather than test script extracts. The author says it’s “just a prototype,” though.
The Mojolicious real-time web framework has a built-in validator, but it’s geared towards validating HTTP parameters rather than subroutine arguments. There’s also a MojoX::Validator wrapper for Input::Validator that performs much the same task. You could press either into service as subroutine validators if you’re already using Mojolicious, but they aren’t structured that way.
This isn’t for input parameters to subroutines; rather, it provides an attribute that specifies what type your subroutines return. As such, it’s complimentary to most of the other techniques covered in this series. It works with Type::Tiny, MooseX::Types, or MouseX::Types, and because Perl functions can tell whether they’re in scalar or list context, you can specify different type constraints for each.
There are several other modules with similar functionality, but these are outside the scope of this article.
Based on Function::Parameters (covered previously), this pragma adds support for a registry for storing Type::Tiny type libraries. It doesn’t appear to offer any other signature features, though. Feel free to correct me in the comments.
As I said in my slide deck, maybe we’re taking TMTOWTDI too far? It’s nice to be spoiled for options, though. I still recommend Type::Tiny’s Type::Params for its performance and flexibility, or Params::ValidationCompiler if you’re using type libraries from Specio or Moose. Some useful side-by-side benchmarks would be nice; maybe I’ll save that for another article and further risk being labeled “the signatures guy.”