|
Packit Service |
817997 |
# NAME
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
Params::ValidationCompiler - Build an optimized subroutine parameter validator once, use it forever
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
# VERSION
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
version 0.27
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
# SYNOPSIS
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
use Types::Standard qw( Int Str );
|
|
Packit Service |
817997 |
use Params::ValidationCompiler qw( validation_for );
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
{
|
|
Packit Service |
817997 |
my $validator = validation_for(
|
|
Packit Service |
817997 |
params => {
|
|
Packit Service |
817997 |
foo => { type => Int },
|
|
Packit Service |
817997 |
bar => {
|
|
Packit Service |
817997 |
type => Str,
|
|
Packit Service |
817997 |
optional => 1,
|
|
Packit Service |
817997 |
},
|
|
Packit Service |
817997 |
baz => {
|
|
Packit Service |
817997 |
type => Int,
|
|
Packit Service |
817997 |
default => 42,
|
|
Packit Service |
817997 |
},
|
|
Packit Service |
817997 |
},
|
|
Packit Service |
817997 |
);
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
sub foo {
|
|
Packit Service |
817997 |
my %args = $validator->(@_);
|
|
Packit Service |
817997 |
}
|
|
Packit Service |
817997 |
}
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
{
|
|
Packit Service |
817997 |
my $validator = validation_for(
|
|
Packit Service |
817997 |
params => [
|
|
Packit Service |
817997 |
{ type => Int },
|
|
Packit Service |
817997 |
{
|
|
Packit Service |
817997 |
type => Str,
|
|
Packit Service |
817997 |
optional => 1,
|
|
Packit Service |
817997 |
},
|
|
Packit Service |
817997 |
],
|
|
Packit Service |
817997 |
);
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
sub bar {
|
|
Packit Service |
817997 |
my ( $int, $str ) = $validator->(@_);
|
|
Packit Service |
817997 |
}
|
|
Packit Service |
817997 |
}
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
{
|
|
Packit Service |
817997 |
my $validator = validation_for(
|
|
Packit Service |
817997 |
params => [
|
|
Packit Service |
817997 |
foo => { type => Int },
|
|
Packit Service |
817997 |
bar => {
|
|
Packit Service |
817997 |
type => Str,
|
|
Packit Service |
817997 |
optional => 1,
|
|
Packit Service |
817997 |
},
|
|
Packit Service |
817997 |
],
|
|
Packit Service |
817997 |
named_to_list => 1,
|
|
Packit Service |
817997 |
);
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
sub baz {
|
|
Packit Service |
817997 |
my ( $foo, $bar ) = $validator->(@_);
|
|
Packit Service |
817997 |
}
|
|
Packit Service |
817997 |
}
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
# DESCRIPTION
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
This module creates a customized, highly efficient parameter checking
|
|
Packit Service |
817997 |
subroutine. It can handle named or positional parameters, and can return the
|
|
Packit Service |
817997 |
parameters as key/value pairs or a list of values.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
In addition to type checks, it also supports parameter defaults, optional
|
|
Packit Service |
817997 |
parameters, and extra "slurpy" parameters.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
# EXPORTS
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
This module has two options exports, `validation_for` and `source_for`. Both
|
|
Packit Service |
817997 |
of these subs accept the same options:
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
- params
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
An arrayref or hashref containing a parameter specification.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
If you pass a hashref then the generated validator sub will expect named
|
|
Packit Service |
817997 |
parameters. The `params` value should be a hashref where the parameter names
|
|
Packit Service |
817997 |
are keys and the specs are the values.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
If you pass an arrayref and `named_to_list` is false, the validator will
|
|
Packit Service |
817997 |
expect positional params. Each element of the `params` arrayref should be a
|
|
Packit Service |
817997 |
parameter spec.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
If you pass an arrayref and `named_to_list` is true, the validator will
|
|
Packit Service |
817997 |
expect named params, but will return a list of values. In this case the
|
|
Packit Service |
817997 |
arrayref should contain a _list_ of key/value pairs, where parameter names
|
|
Packit Service |
817997 |
are the keys and the specs are the values.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
Each spec can contain either a boolean or hashref. If the spec is a boolean,
|
|
Packit Service |
817997 |
this indicates required (true) or optional (false).
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
The spec hashref accepts the following keys:
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
- type
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
A type object. This can be a [Moose](https://metacpan.org/pod/Moose) type (from [Moose](https://metacpan.org/pod/Moose) or
|
|
Packit Service |
817997 |
[MooseX::Types](https://metacpan.org/pod/MooseX::Types)), a [Type::Tiny](https://metacpan.org/pod/Type::Tiny) type, or a [Specio](https://metacpan.org/pod/Specio) type.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
If the type has coercions, those will always be used.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
- default
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
This can either be a simple (non-reference) scalar or a subroutine
|
|
Packit Service |
817997 |
reference. The sub ref will be called without any arguments (for now).
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
- optional
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
A boolean indicating whether or not the parameter is optional. By default,
|
|
Packit Service |
817997 |
parameters are required unless you provide a default.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
- slurpy
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
If this is a simple true value, then the generated subroutine accepts
|
|
Packit Service |
817997 |
additional arguments not specified in `params`. By default, extra arguments
|
|
Packit Service |
817997 |
cause an exception.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
You can also pass a type constraint here, in which case all extra arguments
|
|
Packit Service |
817997 |
must be values of the specified type.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
- named\_to\_list
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
If this is true, the generated subroutine will expect a list of key-value
|
|
Packit Service |
817997 |
pairs or a hashref and it will return a list containing only values. The
|
|
Packit Service |
817997 |
`params` you pass must be a arrayref of key-value pairs. The order of these
|
|
Packit Service |
817997 |
pairs determines the order in which values are returned.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
You cannot combine `slurpy` with `named_to_list` as there is no way to know
|
|
Packit Service |
817997 |
how to order the extra return values.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
## validation\_for(...)
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
This returns a subroutine that implements the specific parameter
|
|
Packit Service |
817997 |
checking. This subroutine expects to be given the parameters to validate in
|
|
Packit Service |
817997 |
`@_`. If all the parameters are valid, it will return the validated
|
|
Packit Service |
817997 |
parameters (with defaults as appropriate), either as a list of key-value pairs
|
|
Packit Service |
817997 |
or as a list of just values. If any of the parameters are invalid it will
|
|
Packit Service |
817997 |
throw an exception.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
For validators expected named params, the generated subroutine accepts either
|
|
Packit Service |
817997 |
a list of key-value pairs or a single hashref. Otherwise the validator expects
|
|
Packit Service |
817997 |
a list of values.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
For now, you must shift off the invocant yourself.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
This subroutine accepts the following additional parameters:
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
- name
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
If this is given, then the generated subroutine will be named using
|
|
Packit Service |
817997 |
[Sub::Util](https://metacpan.org/pod/Sub::Util). This is strongly recommended as it makes it possible to
|
|
Packit Service |
817997 |
distinguish different check subroutines when profiling or in stack traces.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
This name will also be used in some exception messages, even if [Sub::Util](https://metacpan.org/pod/Sub::Util)
|
|
Packit Service |
817997 |
is not available.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
Note that you must install [Sub::Util](https://metacpan.org/pod/Sub::Util) yourself separately, as it is not
|
|
Packit Service |
817997 |
required by this distribution, in order to avoid requiring a compiler.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
- name\_is\_optional
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
If this is true, then the name is ignored when `Sub::Util` is not
|
|
Packit Service |
817997 |
installed. If this is false, then passing a name when [Sub::Util](https://metacpan.org/pod/Sub::Util) cannot be
|
|
Packit Service |
817997 |
loaded causes an exception.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
This is useful for CPAN modules where you want to set a name if you can, but
|
|
Packit Service |
817997 |
you do not want to add a prerequisite on [Sub::Util](https://metacpan.org/pod/Sub::Util).
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
- debug
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
Sets the `EVAL_CLOSURE_PRINT_SOURCE` environment variable to true before
|
|
Packit Service |
817997 |
calling `Eval::Closure::eval_closure()`. This causes the source of the
|
|
Packit Service |
817997 |
subroutine to be printed before it's `eval`'d.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
## source\_for(...)
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
This returns a two element list. The first is a string containing the source
|
|
Packit Service |
817997 |
code for the generated sub. The second is a hashref of "environment" variables
|
|
Packit Service |
817997 |
to be used when generating the subroutine. These are the arguments that are
|
|
Packit Service |
817997 |
passed to [Eval::Closure](https://metacpan.org/pod/Eval::Closure).
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
# SUPPORT
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
Bugs may be submitted at [https://github.com/houseabsolute/Params-ValidationCompiler/issues](https://github.com/houseabsolute/Params-ValidationCompiler/issues).
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
I am also usually active on IRC as 'autarch' on `irc://irc.perl.org`.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
# SOURCE
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
The source code repository for Params-ValidationCompiler can be found at [https://github.com/houseabsolute/Params-ValidationCompiler](https://github.com/houseabsolute/Params-ValidationCompiler).
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
# DONATIONS
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
If you'd like to thank me for the work I've done on this module, please
|
|
Packit Service |
817997 |
consider making a "donation" to me via PayPal. I spend a lot of free time
|
|
Packit Service |
817997 |
creating free software, and would appreciate any support you'd care to offer.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
Please note that **I am not suggesting that you must do this** in order for me
|
|
Packit Service |
817997 |
to continue working on this particular software. I will continue to do so,
|
|
Packit Service |
817997 |
inasmuch as I have in the past, for as long as it interests me.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
Similarly, a donation made in this way will probably not make me work on this
|
|
Packit Service |
817997 |
software much more, unless I get so many donations that I can consider working
|
|
Packit Service |
817997 |
on free software full time (let's all have a chuckle at that together).
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
To donate, log into PayPal and send money to autarch@urth.org, or use the
|
|
Packit Service |
817997 |
button at [http://www.urth.org/~autarch/fs-donation.html](http://www.urth.org/~autarch/fs-donation.html).
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
# AUTHOR
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
Dave Rolsky <autarch@urth.org>
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
# CONTRIBUTORS
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
- Gregory Oschwald <goschwald@maxmind.com>
|
|
Packit Service |
817997 |
- Gregory Oschwald <oschwald@gmail.com>
|
|
Packit Service |
817997 |
- Tomasz Konojacki <me@xenu.pl>
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
# COPYRIGHT AND LICENSE
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
This software is Copyright (c) 2016 - 2018 by Dave Rolsky.
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
This is free software, licensed under:
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
The Artistic License 2.0 (GPL Compatible)
|
|
Packit Service |
817997 |
|
|
Packit Service |
817997 |
The full text of the license can be found in the
|
|
Packit Service |
817997 |
`LICENSE` file included with this distribution.
|