1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "Params::Check 3perl"
.TH Params::Check 3perl "2023-11-25" "perl v5.36.0" "Perl Programmers Reference Guide"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
Params::Check \- A generic input parsing/checking mechanism.
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\& use Params::Check qw[check allow last_error];
\&
\& sub fill_personal_info {
\& my %hash = @_;
\& my $x;
\&
\& my $tmpl = {
\& firstname => { required => 1, defined => 1 },
\& lastname => { required => 1, store => \e$x },
\& gender => { required => 1,
\& allow => [qr/M/i, qr/F/i],
\& },
\& married => { allow => [0,1] },
\& age => { default => 21,
\& allow => qr/^\ed+$/,
\& },
\&
\& phone => { allow => [ sub { return 1 if /$valid_re/ },
\& \*(Aq1\-800\-PERL\*(Aq ]
\& },
\& id_list => { default => [],
\& strict_type => 1
\& },
\& employer => { default => \*(AqNSA\*(Aq, no_override => 1 },
\& };
\&
\& ### check() returns a hashref of parsed args on success ###
\& my $parsed_args = check( $tmpl, \e%hash, $VERBOSE )
\& or die qw[Could not parse arguments!];
\&
\& ... other code here ...
\& }
\&
\& my $ok = allow( $colour, [qw|blue green yellow|] );
\&
\& my $error = Params::Check::last_error();
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
Params::Check is a generic input parsing/checking mechanism.
.PP
It allows you to validate input via a template. The only requirement
is that the arguments must be named.
.PP
Params::Check can do the following things for you:
.IP "\(bu" 4
Convert all keys to lowercase
.IP "\(bu" 4
Check if all required arguments have been provided
.IP "\(bu" 4
Set arguments that have not been provided to the default
.IP "\(bu" 4
Weed out arguments that are not supported and warn about them to the
user
.IP "\(bu" 4
Validate the arguments given by the user based on strings, regexes,
lists or even subroutines
.IP "\(bu" 4
Enforce type integrity if required
.PP
Most of Params::Check's power comes from its template, which we'll
discuss below:
.SH "Template"
.IX Header "Template"
As you can see in the synopsis, based on your template, the arguments
provided will be validated.
.PP
The template can take a different set of rules per key that is used.
.PP
The following rules are available:
.IP "default" 4
.IX Item "default"
This is the default value if none was provided by the user.
This is also the type \f(CW\*(C`strict_type\*(C'\fR will look at when checking type
integrity (see below).
.IP "required" 4
.IX Item "required"
A boolean flag that indicates if this argument was a required
argument. If marked as required and not provided, \fBcheck()\fR will fail.
.IP "strict_type" 4
.IX Item "strict_type"
This does a \f(CW\*(C`ref()\*(C'\fR check on the argument provided. The \f(CW\*(C`ref\*(C'\fR of the
argument must be the same as the \f(CW\*(C`ref\*(C'\fR of the default value for this
check to pass.
.Sp
This is very useful if you insist on taking an array reference as
argument for example.
.IP "defined" 4
.IX Item "defined"
If this template key is true, enforces that if this key is provided by
user input, its value is \f(CW\*(C`defined\*(C'\fR. This just means that the user is
not allowed to pass \f(CW\*(C`undef\*(C'\fR as a value for this key and is equivalent
to:
allow => sub { defined \f(CW$_\fR[0] && \s-1OTHER TESTS\s0 }
.IP "no_override" 4
.IX Item "no_override"
This allows you to specify \f(CW\*(C`constants\*(C'\fR in your template. ie, they
keys that are not allowed to be altered by the user. It pretty much
allows you to keep all your \f(CW\*(C`configurable\*(C'\fR data in one place; the
\&\f(CW\*(C`Params::Check\*(C'\fR template.
.IP "store" 4
.IX Item "store"
This allows you to pass a reference to a scalar, in which the data
will be stored:
.Sp
.Vb 2
\& my $x;
\& my $args = check(foo => { default => 1, store => \e$x }, $input);
.Ve
.Sp
This is basically shorthand for saying:
.Sp
.Vb 2
\& my $args = check( { foo => { default => 1 }, $input );
\& my $x = $args\->{foo};
.Ve
.Sp
You can alter the global variable \f(CW$Params::Check::NO_DUPLICATES\fR to
control whether the \f(CW\*(C`store\*(C'\fR'd key will still be present in your
result set. See the \*(L"Global Variables\*(R" section below.
.IP "allow" 4
.IX Item "allow"
A set of criteria used to validate a particular piece of data if it
has to adhere to particular rules.
.Sp
See the \f(CW\*(C`allow()\*(C'\fR function for details.
.SH "Functions"
.IX Header "Functions"
.SS "check( \e%tmpl, \e%args, [$verbose] );"
.IX Subsection "check( %tmpl, %args, [$verbose] );"
This function is not exported by default, so you'll have to ask for it
via:
.PP
.Vb 1
\& use Params::Check qw[check];
.Ve
.PP
or use its fully qualified name instead.
.PP
\&\f(CW\*(C`check\*(C'\fR takes a list of arguments, as follows:
.IP "Template" 4
.IX Item "Template"
This is a hash reference which contains a template as explained in the
\&\f(CW\*(C`SYNOPSIS\*(C'\fR and \f(CW\*(C`Template\*(C'\fR section.
.IP "Arguments" 4
.IX Item "Arguments"
This is a reference to a hash of named arguments which need checking.
.IP "Verbose" 4
.IX Item "Verbose"
A boolean to indicate whether \f(CW\*(C`check\*(C'\fR should be verbose and warn
about what went wrong in a check or not.
.Sp
You can enable this program wide by setting the package variable
\&\f(CW$Params::Check::VERBOSE\fR to a true value. For details, see the
section on \f(CW\*(C`Global Variables\*(C'\fR below.
.PP
\&\f(CW\*(C`check\*(C'\fR will return when it fails, or a hashref with lowercase
keys of parsed arguments when it succeeds.
.PP
So a typical call to check would look like this:
.PP
.Vb 2
\& my $parsed = check( \e%template, \e%arguments, $VERBOSE )
\& or warn q[Arguments could not be parsed!];
.Ve
.PP
A lot of the behaviour of \f(CW\*(C`check()\*(C'\fR can be altered by setting
package variables. See the section on \f(CW\*(C`Global Variables\*(C'\fR for details
on this.
.ie n .SS "allow( $test_me, \e@criteria );"
.el .SS "allow( \f(CW$test_me\fP, \e@criteria );"
.IX Subsection "allow( $test_me, @criteria );"
The function that handles the \f(CW\*(C`allow\*(C'\fR key in the template is also
available for independent use.
.PP
The function takes as first argument a key to test against, and
as second argument any form of criteria that are also allowed by
the \f(CW\*(C`allow\*(C'\fR key in the template.
.PP
You can use the following types of values for allow:
.IP "string" 4
.IX Item "string"
The provided argument \s-1MUST\s0 be equal to the string for the validation
to pass.
.IP "regexp" 4
.IX Item "regexp"
The provided argument \s-1MUST\s0 match the regular expression for the
validation to pass.
.IP "subroutine" 4
.IX Item "subroutine"
The provided subroutine \s-1MUST\s0 return true in order for the validation
to pass and the argument accepted.
.Sp
(This is particularly useful for more complicated data).
.IP "array ref" 4
.IX Item "array ref"
The provided argument \s-1MUST\s0 equal one of the elements of the array
ref for the validation to pass. An array ref can hold all the above
values.
.PP
It returns true if the key matched the criteria, or false otherwise.
.SS "\fBlast_error()\fP"
.IX Subsection "last_error()"
Returns a string containing all warnings and errors reported during
the last time \f(CW\*(C`check\*(C'\fR was called.
.PP
This is useful if you want to report then some other way than
\&\f(CW\*(C`carp\*(C'\fR'ing when the verbose flag is on.
.PP
It is exported upon request.
.SH "Global Variables"
.IX Header "Global Variables"
The behaviour of Params::Check can be altered by changing the
following global variables:
.ie n .SS "$Params::Check::VERBOSE"
.el .SS "\f(CW$Params::Check::VERBOSE\fP"
.IX Subsection "$Params::Check::VERBOSE"
This controls whether Params::Check will issue warnings and
explanations as to why certain things may have failed.
If you set it to 0, Params::Check will not output any warnings.
.PP
The default is 1 when warnings are enabled, 0 otherwise;
.ie n .SS "$Params::Check::STRICT_TYPE"
.el .SS "\f(CW$Params::Check::STRICT_TYPE\fP"
.IX Subsection "$Params::Check::STRICT_TYPE"
This works like the \f(CW\*(C`strict_type\*(C'\fR option you can pass to \f(CW\*(C`check\*(C'\fR,
which will turn on \f(CW\*(C`strict_type\*(C'\fR globally for all calls to \f(CW\*(C`check\*(C'\fR.
.PP
The default is 0;
.ie n .SS "$Params::Check::ALLOW_UNKNOWN"
.el .SS "\f(CW$Params::Check::ALLOW_UNKNOWN\fP"
.IX Subsection "$Params::Check::ALLOW_UNKNOWN"
If you set this flag, unknown options will still be present in the
return value, rather than filtered out. This is useful if your
subroutine is only interested in a few arguments, and wants to pass
the rest on blindly to perhaps another subroutine.
.PP
The default is 0;
.ie n .SS "$Params::Check::STRIP_LEADING_DASHES"
.el .SS "\f(CW$Params::Check::STRIP_LEADING_DASHES\fP"
.IX Subsection "$Params::Check::STRIP_LEADING_DASHES"
If you set this flag, all keys passed in the following manner:
.PP
.Vb 1
\& function( \-key => \*(Aqval\*(Aq );
.Ve
.PP
will have their leading dashes stripped.
.ie n .SS "$Params::Check::NO_DUPLICATES"
.el .SS "\f(CW$Params::Check::NO_DUPLICATES\fP"
.IX Subsection "$Params::Check::NO_DUPLICATES"
If set to true, all keys in the template that are marked as to be
stored in a scalar, will also be removed from the result set.
.PP
Default is false, meaning that when you use \f(CW\*(C`store\*(C'\fR as a template
key, \f(CW\*(C`check\*(C'\fR will put it both in the scalar you supplied, as well as
in the hashref it returns.
.ie n .SS "$Params::Check::PRESERVE_CASE"
.el .SS "\f(CW$Params::Check::PRESERVE_CASE\fP"
.IX Subsection "$Params::Check::PRESERVE_CASE"
If set to true, Params::Check will no longer convert all keys from
the user input to lowercase, but instead expect them to be in the
case the template provided. This is useful when you want to use
similar keys with different casing in your templates.
.PP
Understand that this removes the case-insensitivity feature of this
module.
.PP
Default is 0;
.ie n .SS "$Params::Check::ONLY_ALLOW_DEFINED"
.el .SS "\f(CW$Params::Check::ONLY_ALLOW_DEFINED\fP"
.IX Subsection "$Params::Check::ONLY_ALLOW_DEFINED"
If set to true, Params::Check will require all values passed to be
\&\f(CW\*(C`defined\*(C'\fR. If you wish to enable this on a 'per key' basis, use the
template option \f(CW\*(C`defined\*(C'\fR instead.
.PP
Default is 0;
.ie n .SS "$Params::Check::SANITY_CHECK_TEMPLATE"
.el .SS "\f(CW$Params::Check::SANITY_CHECK_TEMPLATE\fP"
.IX Subsection "$Params::Check::SANITY_CHECK_TEMPLATE"
If set to true, Params::Check will sanity check templates, validating
for errors and unknown keys. Although very useful for debugging, this
can be somewhat slow in hot-code and large loops.
.PP
To disable this check, set this variable to \f(CW\*(C`false\*(C'\fR.
.PP
Default is 1;
.ie n .SS "$Params::Check::WARNINGS_FATAL"
.el .SS "\f(CW$Params::Check::WARNINGS_FATAL\fP"
.IX Subsection "$Params::Check::WARNINGS_FATAL"
If set to true, Params::Check will \f(CW\*(C`croak\*(C'\fR when an error during
template validation occurs, rather than return \f(CW\*(C`false\*(C'\fR.
.PP
Default is 0;
.ie n .SS "$Params::Check::CALLER_DEPTH"
.el .SS "\f(CW$Params::Check::CALLER_DEPTH\fP"
.IX Subsection "$Params::Check::CALLER_DEPTH"
This global modifies the argument given to \f(CW\*(C`caller()\*(C'\fR by
\&\f(CW\*(C`Params::Check::check()\*(C'\fR and is useful if you have a custom wrapper
function around \f(CW\*(C`Params::Check::check()\*(C'\fR. The value must be an
integer, indicating the number of wrapper functions inserted between
the real function call and \f(CW\*(C`Params::Check::check()\*(C'\fR.
.PP
Example wrapper function, using a custom stacktrace:
.PP
.Vb 2
\& sub check {
\& my ($template, $args_in) = @_;
\&
\& local $Params::Check::WARNINGS_FATAL = 1;
\& local $Params::Check::CALLER_DEPTH = $Params::Check::CALLER_DEPTH + 1;
\& my $args_out = Params::Check::check($template, $args_in);
\&
\& my_stacktrace(Params::Check::last_error) unless $args_out;
\&
\& return $args_out;
\& }
.Ve
.PP
Default is 0;
.SH "Acknowledgements"
.IX Header "Acknowledgements"
Thanks to Richard Soderberg for his performance improvements.
.SH "BUG REPORTS"
.IX Header "BUG REPORTS"
Please report bugs or other issues to <bug\-params\-check@rt.cpan.org>.
.SH "AUTHOR"
.IX Header "AUTHOR"
This module by Jos Boumans <kane@cpan.org>.
.SH "COPYRIGHT"
.IX Header "COPYRIGHT"
This library is free software; you may redistribute and/or modify it
under the same terms as Perl itself.
|