The mistake Params::Validate makes is that it doesn't allow you to create what your simple case is.
That's not quite true. There is the validation_options function, which sets per-package defaults.
Params::Validate::validation_options(
normalize_keys => sub { $_[0] =~ s/^-//; lc $_[0] }
);
You can also define parameter specs up front and re-use them. (Or put them in a config file/module for reuse across an app.)
my %specs_for = (
foo =>
# specify a type
{ type => ARRAYREF },
bar =>
# specify an interface
{ can => [ 'print', 'flush', 'frobnicate' ] },
baz =>
{ type => SCALAR, # a scalar ...
# ... that is a plain integer ...
regex => qr/^\d+$/,
callbacks =>
{ # ... and smaller than 90
'less than 90' => sub { shift() < 90 },
},
);
sub wibble {
my %args = validate( @_, {
foo => $specs_for{foo},
bar => $specs_for{baz},
});
# ...
}
So with very little up-front work, you can create re-usable parameter validation specs with a preset default case.
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
|