Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Params::Validate - a powerful tool for robust subroutine design

by autarch (Hermit)
on Jan 24, 2006 at 07:16 UTC ( #525121=note: print w/replies, xml ) Need Help??


in reply to Params::Validate - a powerful tool for robust subroutine design

One style note I'd like to make. At $DAYJOB other coders liked the concept of declaring an API and checking it, but they felt like having a big clock of param spec code in the subroutine body was distracting, especially for short subs.

I've started using this style whenever possible to work around that problem:

{ Readonly my $spec => { size => { type => SCALAR }, color => { type => SCALAR, regex => qr/^(?:red|green|blue)$/i }, }; sub item_by_size_and_color { my %p = validate( @_, $spec ); ... } }

This pulls the parameter declaration out of the sub body but puts it right next to the sub. The use of Reaodnly is entirely optional, and I should point out that it only works with the XS version of Readonly, the pure Perl version will cause segfaults if you use the XS version of Params::Validate (perl magic in the internals is a huge PITA for XS code).

Generally speaking, I'm really liking the above style, though if you need to calculate some part of the spec dynamically per request, it won't work.

We also created a library to simplify "type declaration", so we have code like this:

{ size => SCALAR_TYPE, color => RGB_COLOR_TYPE( default => 'blue' ), ... }

I'd like to turn this into a CPAN module eventually, maybe something like Params::Validate::Types.

And finally, it's worth noting that Params::Validate is fairly flexible in its inputs. The parameters can be given as a hash (in @_) or a hashref (in $_[0]).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://525121]
help
Chatterbox?
[ambrus]: Corion: those are good rules.
[ambrus]: Discipulus: oh sure. the input data has different filenames every time I get them.
[ambrus]: the directory structure may be 1, 2, or 3 deep, it may have spaces in the filename or not, it has dates in various format, different keywords for the same meanings, and the dates and other keywords are assembled in various ways.
[Discipulus]: no ambrus by specification i mean for example license per core instead of per socket, so fields are different

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2017-03-29 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (350 votes). Check out past polls.