NAME
MouseX::Params::Validate - Extension of Params::Validate using Mouse's types.
VERSION
Version 0.06
DESCRIPTION
Method parameter validation extension to Mouse.
Borrowed code entirely from MooseX::Params::Validate and stripped Moose footprints.
EXPORTS
By default, this module exports the following:
validated_hash
validated_list
pos_validated_list
CAVEATS
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.
CACHING
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.Passing in
MX_PARAMS_VALIDATE_CACHE_KEY
with value to be used as the cache key will bypass the normal cache key generation.
METHODS
validated_hash(\@_, %parameter_spec)
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.
The values in @_
can either be a set of name-value pairs or a single hash reference.
The %parameter_spec
accepts the following options:
- isa
-
The
isa
option can be either;class name, Mouse type constraint name or an anon Mouse type constraint. - does
-
The
does
option can be either; role name or an anon Mouse type constraint. - default
-
This is the default value to be used if the value is not supplied.
- optional
-
As with Params::Validate, all options are considered required unless otherwise specified. This option is passed directly to Params::Validate.
- coerce
-
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'},
);
...
...
}
validated_list(\@_, %parameter_spec)
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
.
The values in @_
can either be a set of name-value pairs or a single hash reference.
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'},
);
...
...
}
pos_validated_list(\@_, $spec, $spec, ...)
This function validates a list of positional parameters. Each $spec
should validate one of the parameters in the list.
Unlike the other functions, this function cannot find $self
in the argument list. Make sure to shift it off yourself before doing validation.
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,
);
...
...
}
AUTHOR
Mohammad S Anwar, <mohammad.anwar at yahoo.com>
REPOSITORY
https://github.com/Manwar/MouseX-Params-Validate
CONTRIBUTORS
Hans Staugaard (STAUGAARD)
BUGS
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.
SUPPORT
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
CPAN Ratings
Search CPAN
ACKNOWLEDGEMENTS
Stevan Little <stevan.little@iinteractive.com> (Author of MooseX::Params::Validate).
Dave Rolsky <autarch@urth.org> (Maintainer of MooseX::Params::Validate).
LICENSE AND COPYRIGHT
Copyright (C) 2011 Mohammad S Anwar.
This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:
http://www.perlfoundation.org/artistic_license_2_0
Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License.By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made by someone other than you,you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement,then this Artistic License to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.