The Perl and Raku Conference 2025: Greenville, South Carolina - June 27-29 Learn more

NAME

Params::Validate::Array - provide an alternative version of Param::Validate's validate() function which will return parameters as a list.

VERSION

Version 0.03

SYNOPSIS

This module's validate() function is a replacement for the Params::Validate module's validate() function, returning the arguments as a list, and not a hash as Params::Validate::validate() does.

This replacement validate() requires the argument descriptor to be an array reference, not a hash reference as in Params::Validate::validate().

Examples:

use Params::Validate::Array qw(SCALAR ARRAYREF validate ...);
sub foo1 {
my ($a, $b, $c) = validate( @_,
# note the arrayref, not a hashref as in Params::Validate
[ a => 1, # types of a, b, & c are
b => 0, #+ unspecified. a is mandatory,
c => 0, #+ b & c optional
]
);
print "a = $a\n";
print "b = ", $b // "undefined", "\n";
print "c = ", $c // "undefined", "\n";
}
foo1(a => 'hello', c => 'there');
# prints:
# a = hello
# b = undefined
# c = there
foo1(b => 1, c => 'foo');
# throws error:
# "Mandatory parameter 'a' missing in call to main::foo1 ..."
}
sub foo2 {
my ($x, $y) = validate( @_,
# arrayref, not hashref
[ x => {type => HASHREF, optional => 1}, # hashref 'x' is optional,
y => {type => SCALAR }, #+ scalar 'y' mandatory
]
);
$x->{$y} = 'foo'
if defined $x;
}

Note that if this module's validate() function is called with a hashref argument descriptor, the behaviour reverts to that of Params::Validate::validate():

use Params::Validate::Array qw(SCALAR HASHREF validate);
sub foo3 {
my %arg = validate( @_,
# Note hashref
{ x => {type => HASHREF, optional => 1},
y => {type => SCALAR },
}
);
print "y arg is ", $arg{y}, "\n";
...
}

EXPORT

Params::Validate::Array exports everything that Params::Validate does, including validate() and validate_pos() by default. The functions validate_with() and validation_options() as well as constants SCALAR, ARRAYREF, etc are also available for export. See Params::Validate for details.

Only the behaviour of validate() is changed, and only when the argument descriptor is an arrayref. All other routines are identical to those in Params::Validate (Except for validation_options() which will increment any stack_skip => ... argument to hide the extra layer of call stack).

SUBROUTINES/METHODS

my @args = validate(@_, [ descriptor ]);

In contrast to the validate() subroutine in Params::Validate, which is called as:

my %args = validate(@_, { ... } );

(where the hashref argument {...} is a descriptor for the subroutine arguments to be validated), the Params::Validate::Array::validate() subroutine in this package is called as

my ($arg1, $arg2, ...) = validate(@_, [ ... ] );

where the contents of the descriptor [...] are identical to those in the hashref descriptor {...} of the Params::Validate call.

In fact Params::Validate::Array::validate() is little more than a wrapper, and uses Params::Validate::validate() to do all the hard work.

AUTHOR

Sam Brain, <samb at stanford.edu>

BUGS

Please report any bugs or feature requests to bug-test at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

ACKNOWLEDGEMENTS

Thanks to Dave Rolsky for the Params::Validate and MooseX::Params::Validate modules, whose perl code and test suites I plagiarized shamelessly for this module.

LICENSE AND COPYRIGHT

Copyright 2011 Sam Brain.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.