Params::Validate is a Perl module to validate method/function parameters.
SYNOPSIS
use Params::Validate qw(:all);
# takes named params (hash or hashref)
sub foo
{
validate( @_, { foo => 1, # mandatory
bar => 0, # optional
}
);
}
# takes positional params
sub bar
{
# first two are mandatory, third is optional
validate_pos( @_, 1, 1, 0 );
}
sub foo2
{
validate( @_,
{ 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 with_defaults
{
my %p = validate( @_, { foo => 1, # required
# $p{bar} will be 99 if bar is not
# given. bar is now optional.
bar => { default => 99 } } );
}
sub pos_with_defaults
{
my @p = validate_pos( @_, 1, { default => 99 } );
}
sub sets_options_on_call
{
my %p = validate_with
( params => @_,
spec => { foo => { type SCALAR, default => 2 } },
normalize_keys => sub { $_[0] =~ s/^-//; lc $_[0] },
);
}
The Params::Validate module allows you to validate method or function call parameters to an arbitrary level of specificity. At the simplest level, it is capable of validating the required parameters were given and that no unspecified additional parameters were passed in.
It is also capable of determining that a parameter is of a specific type, that it is an object of a certain class hierarchy, that it possesses certain methods, or applying validation callbacks to arguments.
Product's homepage
Requirements:
· Perl