MouseX::Params::Validate - Extension of Params::Validate using Mouse's types.
Version 0.04
Method parameter validation extension to Mouse.
Borrowed code entirely from MooseX::Params::Validate and stripped Moose footprints.
By default, this module exports the following:
validated_hash
validated_list
pos_validated_list
It isn't possible to introspect the method parameter specs they are created as needed when the method is called and cached for subsequent calls.
When a validation subroutine is called the first time, the parameter spec is prepared & cached to avoid unnecessary regeneration. It uses the fully qualified name of the subroutine (package +subname) as the cache key. In 99.999% of the use cases for this module that will be the right thing to do. You can do a couple things to better control the caching behavior.
Passing in the MX_PARAMS_VALIDATE_NO_CACHE flag in the parameter spec this will prevent the parameter spec from being cached.
MX_PARAMS_VALIDATE_NO_CACHE
Passing in MX_PARAMS_VALIDATE_CACHE_KEY with value to be used as the cache key will bypass the normal cache key generation.
MX_PARAMS_VALIDATE_CACHE_KEY
This behaves similarly to the standard Params::Validate validate function and returns the captured values in a HASH. The one exception is where if it spots an instance in the @_ , then it will handle it appropriately.
validate
@_
The values in @_ can either be a set of name-value pairs or a single hash reference.
The %parameter_spec accepts the following options:
%parameter_spec
The isa option can be either; class name, Mouse type constraint name or an anon Mouse type constraint.
isa
The does option can be either; role name or an anon Mouse type constraint.
does
This is the default value to be used if the value is not supplied.
As with Params::Validate, all options are considered required unless otherwise specified. This option is passed directly to Params::Validate.
If this is true and the parameter has a type constraint which has coercions, then the coercion will be called for this parameter. If the type does have coercions, then this parameter is ignored.
use Mouse; use MouseX::Params::Validate; sub foo { my ($self, %params) = validated_hash( \@_, bar => {isa => 'Str', default => 'Mouse'}, ); ... ... }
The %parameter_spec accepts the same options as above but returns the params as positional values instead of a HASH.
We capture the order in which you defined the parameters and then return them as a list in the same order. If a param is marked optional and not included, then it will be set to undef.
undef
Like validated_hash, if it spots an object instance as the first parameter of @_ it will handle it appropriately, returning it as the first argument.
use Mouse; use MouseX::Params::Validate; sub foo { my ($self, $foo, $bar) = validated_list( \@_, foo => {isa => 'Foo'}, bar => {isa => 'Bar'}, ); ... ... }
This function validates a list of positional parameters. Each $spec should validate one of the parameters in the list.
$spec
Unlike the other functions, this function cannot find $self in the argument list. Make sure to shift it off yourself before doing validation.
$self
The values in @_ must be a list of values.You cannot pass the values as an array reference, because this cannot be distinguished from passing one value which itself an array reference.
If a parameter is marked as optional and is not present, it will simply not be returned.
If you want to pass in any of the cache control parameters described below, simply pass them after the list of parameter validation specs.
use Mouse; use MouseX::Params::Validate; sub foo { my $self = shift; my ($foo, $bar) = pos_validated_list( \@_, {isa => 'Foo'}, {isa => 'Bar'}, MX_PARAMS_VALIDATE_NO_CACHE => 1, ); ... ... }
Mohammad S Anwar, <mohammad.anwar at yahoo.com>
<mohammad.anwar at yahoo.com>
Please report any bugs or feature requests to bug-mousex-params-validate at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MouseX-Params-Validate. I will be notified and then you'll automatically be notified of progress on your bug as I make changes.
bug-mousex-params-validate at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc MouseX::Params::Validate
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=MouseX-Params-Validate
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/MouseX-Params-Validate
CPAN Ratings
http://cpanratings.perl.org/d/MouseX-Params-Validate
Search CPAN
http://search.cpan.org/dist/MouseX-Params-Validate/
Stevan Little <stevan.little@iinteractive.com> (Author of MooseX::Params::Validate).
Dave Rolsky <autarch@urth.org> (Maintainer of MooseX::Params::Validate).
Copyright 2011 Mohammad S Anwar.
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.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
To install MouseX::Params::Validate, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MouseX::Params::Validate
CPAN shell
perl -MCPAN -e shell install MouseX::Params::Validate
For more information on module installation, please visit the detailed CPAN module installation guide.