• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

eg/H31-Jul-2018-618542

lib/Params/H31-Jul-2018-1,536866

t/H31-Jul-2018-1,7841,480

xt/H31-Jul-2018-1,097871

CODE_OF_CONDUCT.mdH A D31-Jul-20183.2 KiB7656

CONTRIBUTING.mdH A D31-Jul-20184 KiB11275

ChangesH A D31-Jul-20186.6 KiB236128

INSTALLH A D31-Jul-20181.9 KiB6037

LICENSEH A D31-Jul-20188.8 KiB208154

MANIFESTH A D31-Jul-20181.1 KiB5958

META.jsonH A D31-Jul-201837.2 KiB1,1061,104

META.ymlH A D31-Jul-201823.4 KiB803802

Makefile.PLH A D31-Jul-20182 KiB8169

README.mdH A D31-Jul-20189.4 KiB276192

appveyor.ymlH A D31-Jul-2018371 1816

cpanfileH A D31-Jul-20182.4 KiB7671

dist.iniH A D31-Jul-2018873 3934

perlcriticrcH A D31-Jul-20181.8 KiB6847

perltidyrcH A D31-Jul-2018301 2322

test-matrix.alsH A D31-Jul-2018526 3625

tidyall.iniH A D31-Jul-2018554 2822

README.md

1# NAME
2
3Params::ValidationCompiler - Build an optimized subroutine parameter validator once, use it forever
4
5# VERSION
6
7version 0.30
8
9# SYNOPSIS
10
11    use Types::Standard qw( Int Str );
12    use Params::ValidationCompiler qw( validation_for );
13
14    {
15        my $validator = validation_for(
16            params => {
17                foo => { type => Int },
18                bar => {
19                    type     => Str,
20                    optional => 1,
21                },
22                baz => {
23                    type    => Int,
24                    default => 42,
25                },
26            },
27        );
28
29        sub foo {
30            my %args = $validator->(@_);
31        }
32    }
33
34    {
35        my $validator = validation_for(
36            params => [
37                { type => Int },
38                {
39                    type     => Str,
40                    optional => 1,
41                },
42            ],
43        );
44
45        sub bar {
46            my ( $int, $str ) = $validator->(@_);
47        }
48    }
49
50    {
51        my $validator = validation_for(
52            params => [
53                foo => { type => Int },
54                bar => {
55                    type     => Str,
56                    optional => 1,
57                },
58            ],
59            named_to_list => 1,
60        );
61
62        sub baz {
63            my ( $foo, $bar ) = $validator->(@_);
64        }
65    }
66
67# DESCRIPTION
68
69This module creates a customized, highly efficient parameter checking
70subroutine. It can handle named or positional parameters, and can return the
71parameters as key/value pairs or a list of values.
72
73In addition to type checks, it also supports parameter defaults, optional
74parameters, and extra "slurpy" parameters.
75
76# PARAMETERS
77
78This module has two options exports, `validation_for` and `source_for`. Both
79of these subs accept the same options:
80
81## params
82
83An arrayref or hashref containing a parameter specification.
84
85If you pass a hashref then the generated validator sub will expect named
86parameters. The `params` value should be a hashref where the parameter names
87are keys and the specs are the values.
88
89If you pass an arrayref and `named_to_list` is false, the validator will
90expect positional params. Each element of the `params` arrayref should be a
91parameter spec.
92
93If you pass an arrayref and `named_to_list` is true, the validator will
94expect named params, but will return a list of values. In this case the
95arrayref should contain a _list_ of key/value pairs, where parameter names
96are the keys and the specs are the values.
97
98Each spec can contain either a boolean or hashref. If the spec is a boolean,
99this indicates required (true) or optional (false).
100
101The spec hashref accepts the following keys:
102
103- type
104
105    A type object. This can be a [Moose](https://metacpan.org/pod/Moose) type (from [Moose](https://metacpan.org/pod/Moose) or
106    [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.
107
108    If the type has coercions, those will always be used.
109
110- default
111
112    This can either be a simple (non-reference) scalar or a subroutine
113    reference. The sub ref will be called without any arguments (for now).
114
115- optional
116
117    A boolean indicating whether or not the parameter is optional. By default,
118    parameters are required unless you provide a default.
119
120## slurpy
121
122If this is a simple true value, then the generated subroutine accepts
123additional arguments not specified in `params`. By default, extra arguments
124cause an exception.
125
126You can also pass a type constraint here, in which case all extra arguments
127must be values of the specified type.
128
129## named\_to\_list
130
131If this is true, the generated subroutine will expect a list of key-value
132pairs or a hashref and it will return a list containing only values. The
133`params` you pass must be a arrayref of key-value pairs. The order of these
134pairs determines the order in which values are returned.
135
136You cannot combine `slurpy` with `named_to_list` as there is no way to know
137how to order the extra return values.
138
139## return\_object
140
141If this is true, the generated subroutine will return an object instead of a
142hashref. You cannot set this option to true if you set either or `slurpy` or
143`named_to_list`.
144
145The object's methods correspond to the parameter names passed to the
146subroutine. While calling methods on an object is slower than accessing a
147hashref, the advantage is that if you typo a parameter name you'll get a
148helpful error.
149
150If you have [Class::XSAccessor](https://metacpan.org/pod/Class::XSAccessor) installed then this will be used to create
151the class's methods, which makes it fairly fast.
152
153The returned object is in a generated class. Do not rely on this class name
154being anything in specific, and don't check this object using `isa`, `DOES`,
155or anything similar.
156
157When `return_object` is true, the parameter spec hashref also accepts to the
158following additional keys:
159
160- getter
161
162    Use this to set an explicit getter method name for the parameter. By default
163    the method name will be the same as the parameter name. Note that if the
164    parameter name is not a valid sub name, then you will get an error compiling
165    the validation sub unless you specify a getter for the parameter.
166
167- predicate
168
169    Use this to ask for a predicate method to be created for this parameter. The
170    predicate method returns true if the parameter was passed and false if it
171    wasn't. Note that this is only useful for optional parameters, but you can ask
172    for a predicate for any parameter.
173
174# EXPORTS
175
176The exported subs are:
177
178## validation\_for(...)
179
180This returns a subroutine that implements the specific parameter
181checking. This subroutine expects to be given the parameters to validate in
182`@_`. If all the parameters are valid, it will return the validated
183parameters (with defaults as appropriate), either as a list of key-value pairs
184or as a list of just values. If any of the parameters are invalid it will
185throw an exception.
186
187For validators expected named params, the generated subroutine accepts either
188a list of key-value pairs or a single hashref. Otherwise the validator expects
189a list of values.
190
191For now, you must shift off the invocant yourself.
192
193This subroutine accepts the following additional parameters:
194
195- name
196
197    If this is given, then the generated subroutine will be named using
198    [Sub::Util](https://metacpan.org/pod/Sub::Util). This is strongly recommended as it makes it possible to
199    distinguish different check subroutines when profiling or in stack traces.
200
201    This name will also be used in some exception messages, even if [Sub::Util](https://metacpan.org/pod/Sub::Util)
202    is not available.
203
204    Note that you must install [Sub::Util](https://metacpan.org/pod/Sub::Util) yourself separately, as it is not
205    required by this distribution, in order to avoid requiring a compiler.
206
207- name\_is\_optional
208
209    If this is true, then the name is ignored when `Sub::Util` is not
210    installed. If this is false, then passing a name when [Sub::Util](https://metacpan.org/pod/Sub::Util) cannot be
211    loaded causes an exception.
212
213    This is useful for CPAN modules where you want to set a name if you can, but
214    you do not want to add a prerequisite on [Sub::Util](https://metacpan.org/pod/Sub::Util).
215
216- debug
217
218    Sets the `EVAL_CLOSURE_PRINT_SOURCE` environment variable to true before
219    calling `Eval::Closure::eval_closure()`. This causes the source of the
220    subroutine to be printed before it's `eval`'d.
221
222## source\_for(...)
223
224This returns a two element list. The first is a string containing the source
225code for the generated sub. The second is a hashref of "environment" variables
226to be used when generating the subroutine. These are the arguments that are
227passed to [Eval::Closure](https://metacpan.org/pod/Eval::Closure).
228
229# SUPPORT
230
231Bugs may be submitted at [https://github.com/houseabsolute/Params-ValidationCompiler/issues](https://github.com/houseabsolute/Params-ValidationCompiler/issues).
232
233I am also usually active on IRC as 'autarch' on `irc://irc.perl.org`.
234
235# SOURCE
236
237The source code repository for Params-ValidationCompiler can be found at [https://github.com/houseabsolute/Params-ValidationCompiler](https://github.com/houseabsolute/Params-ValidationCompiler).
238
239# DONATIONS
240
241If you'd like to thank me for the work I've done on this module, please
242consider making a "donation" to me via PayPal. I spend a lot of free time
243creating free software, and would appreciate any support you'd care to offer.
244
245Please note that **I am not suggesting that you must do this** in order for me
246to continue working on this particular software. I will continue to do so,
247inasmuch as I have in the past, for as long as it interests me.
248
249Similarly, a donation made in this way will probably not make me work on this
250software much more, unless I get so many donations that I can consider working
251on free software full time (let's all have a chuckle at that together).
252
253To donate, log into PayPal and send money to autarch@urth.org, or use the
254button at [http://www.urth.org/~autarch/fs-donation.html](http://www.urth.org/~autarch/fs-donation.html).
255
256# AUTHOR
257
258Dave Rolsky <autarch@urth.org>
259
260# CONTRIBUTORS
261
262- Gregory Oschwald <goschwald@maxmind.com>
263- Gregory Oschwald <oschwald@gmail.com>
264- Tomasz Konojacki <me@xenu.pl>
265
266# COPYRIGHT AND LICENSE
267
268This software is Copyright (c) 2016 - 2018 by Dave Rolsky.
269
270This is free software, licensed under:
271
272    The Artistic License 2.0 (GPL Compatible)
273
274The full text of the license can be found in the
275`LICENSE` file included with this distribution.
276