Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Params::Validate

by rinceWind (Monsignor)
on May 16, 2004 at 22:08 UTC ( [id://353826]=modulereview: print w/replies, xml ) Need Help??

Item Description: Validation of parameters passed to a subroutine

Review Synopsis:

Whereas C++ and Java go to great lengths to ensure that the data types of any arguments passed to a function, match what is expected, perl is often criticised for not enforcing anything.

Perl 6 will address this issue, but in the mean time, any subroutines you write are passed an array @_ containing a potential hotch-potch of scalars and references, blessed or otherwise.

Although it's nice to write code that can DWIM given a parameter of varying data types, this is always an extra effort to implement. Also, what will your sub do when passed a SCALAR when it's expecting an ARRAYREF? At best, it will die with a run-time error, and a message which likely as not, would not be immediately obvious.

Enter Params::Validate

By default, this module exports the functions validate and validate_pos. Params::Validate caters for two calling conventions: named parameters and positional parameters, which are validated by validate and validate_pos respectively. Unfortunately you can't mix them; any positional parameters before your list of key/value pairs need to be removed first. In fact, this is the way to use Params::Validate to handle method calls. Examples:
sub mymethod { my $self = shift; my %args = validate( @_, { foo => 1, bar => { default => 99} } ); ... } sub mymethod2 { my $self = shift; my ($meenie,$minie,$mo) = validate_pos( @_, { type => SCALAR }, { type => SCALAR | UNDEF }, { type => ARRAYREF, default => [] } ); ... }
As is apparent, the call to validate or validate_pos is quite straightforward and edifying to someone else looking at the code.

It's also quite easy to add such validation to existing code, for an immediate gain in robustness without too much cognitive effort. The module provides a whole host of tools for validating your argument list - I have just scratched the surface.

Error handling

When the parameter validation fails, the default action it to croak, with quite a helpful message about which parameter is invalid and why. You can elect to use a callback to catch validation errors instead.

Conclusion

I am a convert to using this module. I recommend it for CPAN modules and for corporate coding standards.

Update

I have had some interesting dependency issues with modules of mine that are using Params::Validate. I have seen CPAN pull in Ponie via Attribute::Handlers. Why would I want the Parrot/Ponie stuff coming into my production environment ?! I asked Arthur Bergman about this, and apparently it is a spurious dependency picked up by CPAN.pm, apparently sorted in a later release of Attribute::Handlers.

I did notice also that ActiveState's module status page was showing any of my modules that use Params::Validate, as having a dependency on Ponie.

Replies are listed 'Best First'.
Re: Params::Validate
by diotalevi (Canon) on May 17, 2004 at 10:35 UTC
    Be sure to also remember to turn this off when debugging strange problems. Sometimes Params::Validate is the source of the problem (or so goes my experience with Alzabo).
      Curious. I know that the author of Params::Validate and Alzabo are one and the same person.

      I haven't had any strange problems (yet!) with Params::Validate. I trust you reported them to rt.cpan.org, as I know that Params::Validate is being actively maintained, and the latest release is dated April 04.

      --
      I'm Not Just Another Perl Hacker

        I didn't report this to rt.cpan.org because they were all discussed on the Alzabo mailing list with discussion and Dave was already there.

        Nice guy by the way. You should stop by the Minneapolis perl monger meeting sometime if you ever find yourself here at the right time.

        I don't remember the specific problem in question, but now that Params::Validate uses XS internally, I'm not surprised if it occasionally produces weird errors. That said, I use it in production code all the time, and it's very widely used as a result of being required by Mason, and I don't get a lot of bug reports.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: modulereview [id://353826]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-28 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found