Add built local::lib
[catagits/Gitalist.git] / local-lib5 / man / man3 / Params::Validate.3pm
CommitLineData
3fea05b9 1.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.3
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "Params::Validate 3"
132.TH Params::Validate 3 "2009-12-09" "perl v5.8.7" "User Contributed Perl Documentation"
133.SH "NAME"
134Params::Validate \- Validate method/function parameters
135.SH "SYNOPSIS"
136.IX Header "SYNOPSIS"
137.Vb 1
138\& use Params::Validate qw(:all);
139.Ve
140.PP
141.Vb 8
142\& # takes named params (hash or hashref)
143\& sub foo
144\& {
145\& validate( @_, { foo => 1, # mandatory
146\& bar => 0, # optional
147\& }
148\& );
149\& }
150.Ve
151.PP
152.Vb 6
153\& # takes positional params
154\& sub bar
155\& {
156\& # first two are mandatory, third is optional
157\& validate_pos( @_, 1, 1, 0 );
158\& }
159.Ve
160.PP
161.Vb 6
162\& sub foo2
163\& {
164\& validate( @_,
165\& { foo =>
166\& # specify a type
167\& { type => ARRAYREF },
168.Ve
169.PP
170.Vb 3
171\& bar =>
172\& # specify an interface
173\& { can => [ 'print', 'flush', 'frobnicate' ] },
174.Ve
175.PP
176.Vb 12
177\& baz =>
178\& { type => SCALAR, # a scalar ...
179\& # ... that is a plain integer ...
180\& regex => qr/^\ed+$/,
181\& callbacks =>
182\& { # ... and smaller than 90
183\& 'less than 90' => sub { shift() < 90 },
184\& },
185\& }
186\& }
187\& );
188\& }
189.Ve
190.PP
191.Vb 7
192\& sub with_defaults
193\& {
194\& my %p = validate( @_, { foo => 1, # required
195\& # $p{bar} will be 99 if bar is not
196\& # given. bar is now optional.
197\& bar => { default => 99 } } );
198\& }
199.Ve
200.PP
201.Vb 4
202\& sub pos_with_defaults
203\& {
204\& my @p = validate_pos( @_, 1, { default => 99 } );
205\& }
206.Ve
207.PP
208.Vb 8
209\& sub sets_options_on_call
210\& {
211\& my %p = validate_with
212\& ( params => \e@_,
213\& spec => { foo => { type SCALAR, default => 2 } },
214\& normalize_keys => sub { $_[0] =~ s/^\-//; lc $_[0] },
215\& );
216\& }
217.Ve
218.SH "DESCRIPTION"
219.IX Header "DESCRIPTION"
220The Params::Validate module allows you to validate method or function
221call parameters to an arbitrary level of specificity. At the simplest
222level, it is capable of validating the required parameters were given
223and that no unspecified additional parameters were passed in.
224.PP
225It is also capable of determining that a parameter is of a specific
226type, that it is an object of a certain class hierarchy, that it
227possesses certain methods, or applying validation callbacks to
228arguments.
229.Sh "\s-1EXPORT\s0"
230.IX Subsection "EXPORT"
231The module always exports the \f(CW\*(C`validate()\*(C'\fR and \f(CW\*(C`validate_pos()\*(C'\fR
232functions.
233.PP
234It also has an additional function available for export,
235\&\f(CW\*(C`validate_with\*(C'\fR, which can be used to validate any type of
236parameters, and set various options on a per-invocation basis.
237.PP
238In addition, it can export the following constants, which are used as
239part of the type checking. These are \f(CW\*(C`SCALAR\*(C'\fR, \f(CW\*(C`ARRAYREF\*(C'\fR,
240\&\f(CW\*(C`HASHREF\*(C'\fR, \f(CW\*(C`CODEREF\*(C'\fR, \f(CW\*(C`GLOB\*(C'\fR, \f(CW\*(C`GLOBREF\*(C'\fR, and \f(CW\*(C`SCALARREF\*(C'\fR,
241\&\f(CW\*(C`UNDEF\*(C'\fR, \f(CW\*(C`OBJECT\*(C'\fR, \f(CW\*(C`BOOLEAN\*(C'\fR, and \f(CW\*(C`HANDLE\*(C'\fR. These are explained
242in the section on Type Validation.
243.PP
244The constants are available via the export tag \f(CW\*(C`:types\*(C'\fR. There is
245also an \f(CW\*(C`:all\*(C'\fR tag which includes all of the constants as well as the
246\&\f(CW\*(C`validation_options()\*(C'\fR function.
247.SH "PARAMETER VALIDATION"
248.IX Header "PARAMETER VALIDATION"
249The validation mechanisms provided by this module can handle both
250named or positional parameters. For the most part, the same features
251are available for each. The biggest difference is the way that the
252validation specification is given to the relevant subroutine. The
253other difference is in the error messages produced when validation
254checks fail.
255.PP
256When handling named parameters, the module will accept either a hash
257or a hash reference.
258.PP
259Subroutines expecting named parameters should call the \f(CW\*(C`validate()\*(C'\fR
260subroutine like this:
261.PP
262.Vb 4
263\& validate( @_, { parameter1 => validation spec,
264\& parameter2 => validation spec,
265\& ...
266\& } );
267.Ve
268.PP
269Subroutines expecting positional parameters should call the
270\&\f(CW\*(C`validate_pos()\*(C'\fR subroutine like this:
271.PP
272.Vb 1
273\& validate_pos( @_, { validation spec }, { validation spec } );
274.Ve
275.Sh "Mandatory/Optional Parameters"
276.IX Subsection "Mandatory/Optional Parameters"
277If you just want to specify that some parameters are mandatory and
278others are optional, this can be done very simply.
279.PP
280For a subroutine expecting named parameters, you would do this:
281.PP
282.Vb 1
283\& validate( @_, { foo => 1, bar => 1, baz => 0 } );
284.Ve
285.PP
286This says that the \*(L"foo\*(R" and \*(L"bar\*(R" parameters are mandatory and that
287the \*(L"baz\*(R" parameter is optional. The presence of any other
288parameters will cause an error.
289.PP
290For a subroutine expecting positional parameters, you would do this:
291.PP
292.Vb 1
293\& validate_pos( @_, 1, 1, 0, 0 );
294.Ve
295.PP
296This says that you expect at least 2 and no more than 4 parameters.
297If you have a subroutine that has a minimum number of parameters but
298can take any maximum number, you can do this:
299.PP
300.Vb 1
301\& validate_pos( @_, 1, 1, (0) x (@_ \- 2) );
302.Ve
303.PP
304This will always be valid as long as at least two parameters are
305given. A similar construct could be used for the more complex
306validation parameters described further on.
307.PP
308Please note that this:
309.PP
310.Vb 1
311\& validate_pos( @_, 1, 1, 0, 1, 1 );
312.Ve
313.PP
314makes absolutely no sense, so don't do it. Any zeros must come at the
315end of the validation specification.
316.PP
317In addition, if you specify that a parameter can have a default, then
318it is considered optional.
319.Sh "Type Validation"
320.IX Subsection "Type Validation"
321This module supports the following simple types, which can be
322exported as constants:
323.IP "* \s-1SCALAR\s0" 4
324.IX Item "SCALAR"
325A scalar which is not a reference, such as \f(CW10\fR or \f(CW'hello'\fR. A
326parameter that is undefined is \fBnot\fR treated as a scalar. If you
327want to allow undefined values, you will have to specify \f(CW\*(C`SCALAR |
328UNDEF\*(C'\fR.
329.IP "* \s-1ARRAYREF\s0" 4
330.IX Item "ARRAYREF"
331An array reference such as \f(CW\*(C`[1, 2, 3]\*(C'\fR or \f(CW\*(C`\e@foo\*(C'\fR.
332.IP "* \s-1HASHREF\s0" 4
333.IX Item "HASHREF"
334A hash reference such as \f(CW\*(C`{ a => 1, b => 2 }\*(C'\fR or \f(CW\*(C`\e%bar\*(C'\fR.
335.IP "* \s-1CODEREF\s0" 4
336.IX Item "CODEREF"
337A subroutine reference such as \f(CW\*(C`\e&foo_sub\*(C'\fR or \f(CW\*(C`sub { print "hello" }\*(C'\fR.
338.IP "* \s-1GLOB\s0" 4
339.IX Item "GLOB"
340This one is a bit tricky. A glob would be something like \f(CW*FOO\fR, but
341not \f(CW\*(C`\e*FOO\*(C'\fR, which is a glob reference. It should be noted that this
342trick:
343.Sp
344.Vb 1
345\& my $fh = do { local *FH; };
346.Ve
347.Sp
348makes \f(CW$fh\fR a glob, not a glob reference. On the other hand, the
349return value from \f(CW\*(C`Symbol::gensym\*(C'\fR is a glob reference. Either can
350be used as a file or directory handle.
351.IP "* \s-1GLOBREF\s0" 4
352.IX Item "GLOBREF"
353A glob reference such as \f(CW\*(C`\e*FOO\*(C'\fR. See the \s-1GLOB\s0 entry above
354for more details.
355.IP "* \s-1SCALARREF\s0" 4
356.IX Item "SCALARREF"
357A reference to a scalar such as \f(CW\*(C`\e$x\*(C'\fR.
358.IP "* \s-1UNDEF\s0" 4
359.IX Item "UNDEF"
360An undefined value
361.IP "* \s-1OBJECT\s0" 4
362.IX Item "OBJECT"
363A blessed reference.
364.IP "* \s-1BOOLEAN\s0" 4
365.IX Item "BOOLEAN"
366This is a special option, and is just a shortcut for \f(CW\*(C`UNDEF | SCALAR\*(C'\fR.
367.IP "* \s-1HANDLE\s0" 4
368.IX Item "HANDLE"
369This option is also special, and is just a shortcut for \f(CW\*(C`GLOB |
370GLOBREF\*(C'\fR. However, it seems likely that most people interested in
371either globs or glob references are likely to really be interested in
372whether the parameter in question could be a valid file or directory
373handle.
374.PP
375To specify that a parameter must be of a given type when using named
376parameters, do this:
377.PP
378.Vb 2
379\& validate( @_, { foo => { type => SCALAR },
380\& bar => { type => HASHREF } } );
381.Ve
382.PP
383If a parameter can be of more than one type, just use the bitwise or
384(\f(CW\*(C`|\*(C'\fR) operator to combine them.
385.PP
386.Vb 1
387\& validate( @_, { foo => { type => GLOB | GLOBREF } );
388.Ve
389.PP
390For positional parameters, this can be specified as follows:
391.PP
392.Vb 1
393\& validate_pos( @_, { type => SCALAR | ARRAYREF }, { type => CODEREF } );
394.Ve
395.Sh "Interface Validation"
396.IX Subsection "Interface Validation"
397To specify that a parameter is expected to have a certain set of
398methods, we can do the following:
399.PP
400.Vb 4
401\& validate( @_,
402\& { foo =>
403\& # just has to be able to \->bar
404\& { can => 'bar' } } );
405.Ve
406.PP
407.Vb 1
408\& ... or ...
409.Ve
410.PP
411.Vb 4
412\& validate( @_,
413\& { foo =>
414\& # must be able to \->bar and \->print
415\& { can => [ qw( bar print ) ] } } );
416.Ve
417.Sh "Class Validation"
418.IX Subsection "Class Validation"
419A word of warning. When constructing your external interfaces, it is
420probably better to specify what methods you expect an object to
421have rather than what class it should be of (or a child of). This
422will make your \s-1API\s0 much more flexible.
423.PP
424With that said, if you want to validate that an incoming parameter
425belongs to a class (or child class) or classes, do:
426.PP
427.Vb 3
428\& validate( @_,
429\& { foo =>
430\& { isa => 'My::Frobnicator' } } );
431.Ve
432.PP
433.Vb 1
434\& ... or ...
435.Ve
436.PP
437.Vb 4
438\& validate( @_,
439\& { foo =>
440\& { isa => [ qw( My::Frobnicator IO::Handle ) ] } } );
441\& # must be both, not either!
442.Ve
443.Sh "Regex Validation"
444.IX Subsection "Regex Validation"
445If you want to specify that a given parameter must match a specific
446regular expression, this can be done with \*(L"regex\*(R" spec key. For
447example:
448.PP
449.Vb 3
450\& validate( @_,
451\& { foo =>
452\& { regex => qr/^\ed+$/ } } );
453.Ve
454.PP
455The value of the \*(L"regex\*(R" key may be either a string or a pre-compiled
456regex created via \f(CW\*(C`qr\*(C'\fR.
457.PP
458If the value being checked against a regex is undefined, the regex is
459explicitly checked against the empty string ('') instead, in order to
460avoid \*(L"Use of uninitialized value\*(R" warnings.
461.PP
462The \f(CW\*(C`Regexp::Common\*(C'\fR module on \s-1CPAN\s0 is an excellent source of regular
463expressions suitable for validating input.
464.Sh "Callback Validation"
465.IX Subsection "Callback Validation"
466If none of the above are enough, it is possible to pass in one or more
467callbacks to validate the parameter. The callback will be given the
468\&\fBvalue\fR of the parameter as its first argument. Its second argument
469will be all the parameters, as a reference to either a hash or array.
470Callbacks are specified as hash reference. The key is an id for the
471callback (used in error messages) and the value is a subroutine
472reference, such as:
473.PP
474.Vb 6
475\& validate( @_,
476\& { foo =>
477\& { callbacks =>
478\& { 'smaller than a breadbox' => sub { shift() < $breadbox },
479\& 'green or blue' =>
480\& sub { $_[0] eq 'green' || $_[0] eq 'blue' } } } );
481.Ve
482.PP
483.Vb 4
484\& validate( @_,
485\& { foo =>
486\& { callbacks =>
487\& { 'bigger than baz' => sub { $_[0] > $_[1]\->{baz} } } } } );
488.Ve
489.Sh "Untainting"
490.IX Subsection "Untainting"
491If you want values untainted, set the \*(L"untaint\*(R" key in a spec hashref
492to a true value, like this:
493.PP
494.Vb 5
495\& my %p =
496\& validate( @_, { foo =>
497\& { type => SCALAR, untaint => 1 },
498\& bar =>
499\& { type => ARRAYREF } } );
500.Ve
501.PP
502This will untaint the \*(L"foo\*(R" parameter if the parameters are valid.
503.PP
504Note that untainting is only done if \fIall parameters\fR are valid.
505Also, only the return values are untainted, not the original values
506passed into the validation function.
507.PP
508Asking for untainting of a reference value will not do anything, as
509\&\f(CW\*(C`Params::Validate\*(C'\fR will only attempt to untaint the reference itself.
510.Sh "Mandatory/Optional Revisited"
511.IX Subsection "Mandatory/Optional Revisited"
512If you want to specify something such as type or interface, plus the
513fact that a parameter can be optional, do this:
514.PP
515.Vb 4
516\& validate( @_, { foo =>
517\& { type => SCALAR },
518\& bar =>
519\& { type => ARRAYREF, optional => 1 } } );
520.Ve
521.PP
522or this for positional parameters:
523.PP
524.Vb 1
525\& validate_pos( @_, { type => SCALAR }, { type => ARRAYREF, optional => 1 } );
526.Ve
527.PP
528By default, parameters are assumed to be mandatory unless specified as
529optional.
530.Sh "Dependencies"
531.IX Subsection "Dependencies"
532It also possible to specify that a given optional parameter depends on
533the presence of one or more other optional parameters.
534.PP
535.Vb 9
536\& validate( @_, { cc_number =>
537\& { type => SCALAR, optional => 1,
538\& depends => [ 'cc_expiration', 'cc_holder_name' ],
539\& },
540\& cc_expiration
541\& { type => SCALAR, optional => 1 },
542\& cc_holder_name
543\& { type => SCALAR, optional => 1 },
544\& } );
545.Ve
546.PP
547In this case, \*(L"cc_number\*(R", \*(L"cc_expiration\*(R", and \*(L"cc_holder_name\*(R" are
548all optional. However, if \*(L"cc_number\*(R" is provided, then
549\&\*(L"cc_expiration\*(R" and \*(L"cc_holder_name\*(R" must be provided as well.
550.PP
551This allows you to group together sets of parameters that all must be
552provided together.
553.PP
554The \f(CW\*(C`validate_pos()\*(C'\fR version of dependencies is slightly different,
555in that you can only depend on one other parameter. Also, if for
556example, the second parameter 2 depends on the fourth parameter, then
557it implies a dependency on the third parameter as well. This is
558because if the fourth parameter is required, then the user must also
559provide a third parameter so that there can be four parameters in
560total.
561.PP
562\&\f(CW\*(C`Params::Validate\*(C'\fR will die if you try to depend on a parameter not
563declared as part of your parameter specification.
564.Sh "Specifying defaults"
565.IX Subsection "Specifying defaults"
566If the \f(CW\*(C`validate()\*(C'\fR or \f(CW\*(C`validate_pos()\*(C'\fR functions are called in a
567list context, they will return an array or hash containing the
568original parameters plus defaults as indicated by the validation spec.
569.PP
570If the function is not called in a list context, providing a default
571in the validation spec still indicates that the parameter is optional.
572.PP
573The hash or array returned from the function will always be a copy of
574the original parameters, in order to leave \f(CW@_\fR untouched for the
575calling function.
576.PP
577Simple examples of defaults would be:
578.PP
579.Vb 1
580\& my %p = validate( @_, { foo => 1, bar => { default => 99 } } );
581.Ve
582.PP
583.Vb 1
584\& my @p = validate( @_, 1, { default => 99 } );
585.Ve
586.PP
587In scalar context, a hash reference or array reference will be
588returned, as appropriate.
589.SH "USAGE NOTES"
590.IX Header "USAGE NOTES"
591.Sh "Validation failure"
592.IX Subsection "Validation failure"
593By default, when validation fails \f(CW\*(C`Params::Validate\*(C'\fR calls
594\&\f(CW\*(C`Carp::confess()\*(C'\fR. This can be overridden by setting the \f(CW\*(C`on_fail\*(C'\fR
595option, which is described in the \*(L"\s-1GLOBAL\s0\*(R" \s-1OPTIONS\s0
596section.
597.Sh "Method calls"
598.IX Subsection "Method calls"
599When using this module to validate the parameters passed to a method
600call, you will probably want to remove the class/object from the
601parameter list \fBbefore\fR calling \f(CW\*(C`validate()\*(C'\fR or \f(CW\*(C`validate_pos()\*(C'\fR.
602If your method expects named parameters, then this is necessary for
603the \f(CW\*(C`validate()\*(C'\fR function to actually work, otherwise \f(CW@_\fR will not
604be useable as a hash, because it will first have your object (or
605class) \fBfollowed\fR by a set of keys and values.
606.PP
607Thus the idiomatic usage of \f(CW\*(C`validate()\*(C'\fR in a method call will look
608something like this:
609.PP
610.Vb 3
611\& sub method
612\& {
613\& my $self = shift;
614.Ve
615.PP
616.Vb 2
617\& my %params = validate( @_, { foo => 1, bar => { type => ARRAYREF } } );
618\& }
619.Ve
620.ie n .SH """GLOBAL"" OPTIONS"
621.el .SH "``GLOBAL'' OPTIONS"
622.IX Header "GLOBAL OPTIONS"
623Because the \s-1API\s0 for the \f(CW\*(C`validate()\*(C'\fR and \f(CW\*(C`validate_pos()\*(C'\fR functions
624does not make it possible to specify any options other than the the
625validation spec, it is possible to set some options as
626pseudo\-'globals'. These allow you to specify such things as whether
627or not the validation of named parameters should be case sensitive,
628for one example.
629.PP
630These options are called pseudo\-'globals' because these settings are
631\&\fBonly applied to calls originating from the package that set the
632options\fR.
633.PP
634In other words, if I am in package \f(CW\*(C`Foo\*(C'\fR and I call
635\&\f(CW\*(C`validation_options()\*(C'\fR, those options are only in effect when I call
636\&\f(CW\*(C`validate()\*(C'\fR from package \f(CW\*(C`Foo\*(C'\fR.
637.PP
638While this is quite different from how most other modules operate, I
639feel that this is necessary in able to make it possible for one
640module/application to use Params::Validate while still using other
641modules that also use Params::Validate, perhaps with different
642options set.
643.PP
644The downside to this is that if you are writing an app with a standard
645calling style for all functions, and your app has ten modules, \fBeach
646module must include a call to \f(CB\*(C`validation_options()\*(C'\fB\fR. You could of
647course write a module that all your modules use which uses various
648trickery to do this when imported.
649.Sh "Options"
650.IX Subsection "Options"
651.ie n .IP "* normalize_keys => $callback" 4
652.el .IP "* normalize_keys => \f(CW$callback\fR" 4
653.IX Item "normalize_keys => $callback"
654This option is only relevant when dealing with named parameters.
655.Sp
656This callback will be used to transform the hash keys of both the
657parameters and the parameter spec when \f(CW\*(C`validate()\*(C'\fR or
658\&\f(CW\*(C`validate_with()\*(C'\fR are called.
659.Sp
660Any alterations made by this callback will be reflected in the
661parameter hash that is returned by the validation function. For
662example:
663.Sp
664.Vb 7
665\& sub foo {
666\& return
667\& validate_with( params => \e@_,
668\& spec => { foo => { type => SCALAR } },
669\& normalize_keys =>
670\& sub { my $k = shift; $k =~ s/^\-//; return uc $k },
671\& );
672.Ve
673.Sp
674.Vb 1
675\& }
676.Ve
677.Sp
678.Vb 1
679\& %p = foo( foo => 20 );
680.Ve
681.Sp
682.Vb 1
683\& # $p{FOO} is now 20
684.Ve
685.Sp
686.Vb 1
687\& %p = foo( \-fOo => 50 );
688.Ve
689.Sp
690.Vb 1
691\& # $p{FOO} is now 50
692.Ve
693.Sp
694The callback must return a defined value.
695.Sp
696If a callback is given than the deprecated \*(L"ignore_case\*(R" and
697\&\*(L"strip_leading\*(R" options are ignored.
698.ie n .IP "* allow_extra => $boolean" 4
699.el .IP "* allow_extra => \f(CW$boolean\fR" 4
700.IX Item "allow_extra => $boolean"
701If true, then the validation routine will allow extra parameters not
702named in the validation specification. In the case of positional
703parameters, this allows an unlimited number of maximum parameters
704(though a minimum may still be set). Defaults to false.
705.ie n .IP "* on_fail => $callback" 4
706.el .IP "* on_fail => \f(CW$callback\fR" 4
707.IX Item "on_fail => $callback"
708If given, this callback will be called whenever a validation check
709fails. It will be called with a single parameter, which will be a
710string describing the failure. This is useful if you wish to have
711this module throw exceptions as objects rather than as strings, for
712example.
713.Sp
714This callback is expected to \f(CW\*(C`die()\*(C'\fR internally. If it does not, the
715validation will proceed onwards, with unpredictable results.
716.Sp
717The default is to simply use the Carp module's \f(CW\*(C`confess()\*(C'\fR function.
718.ie n .IP "* stack_skip => $number" 4
719.el .IP "* stack_skip => \f(CW$number\fR" 4
720.IX Item "stack_skip => $number"
721This tells Params::Validate how many stack frames to skip when finding
722a subroutine name to use in error messages. By default, it looks one
723frame back, at the immediate caller to \f(CW\*(C`validate()\*(C'\fR or
724\&\f(CW\*(C`validate_pos()\*(C'\fR. If this option is set, then the given number of
725frames are skipped instead.
726.ie n .IP "* ignore_case => $boolean" 4
727.el .IP "* ignore_case => \f(CW$boolean\fR" 4
728.IX Item "ignore_case => $boolean"
729\&\s-1DEPRECATED\s0
730.Sp
731This is only relevant when dealing with named parameters. If it is
732true, then the validation code will ignore the case of parameter
733names. Defaults to false.
734.ie n .IP "* strip_leading => $characters" 4
735.el .IP "* strip_leading => \f(CW$characters\fR" 4
736.IX Item "strip_leading => $characters"
737\&\s-1DEPRECATED\s0
738.Sp
739This too is only relevant when dealing with named parameters. If this
740is given then any parameters starting with these characters will be
741considered equivalent to parameters without them entirely. For
742example, if this is specified as '\-', then \f(CW\*(C`\-foo\*(C'\fR and \f(CW\*(C`foo\*(C'\fR would be
743considered identical.
744.SH "PER-INVOCATION OPTIONS"
745.IX Header "PER-INVOCATION OPTIONS"
746The \f(CW\*(C`validate_with()\*(C'\fR function can be used to set the options listed
747above on a per-invocation basis. For example:
748.PP
749.Vb 7
750\& my %p =
751\& validate_with
752\& ( params => \e@_,
753\& spec => { foo => { type => SCALAR },
754\& bar => { default => 10 } },
755\& allow_extra => 1,
756\& );
757.Ve
758.PP
759In addition to the options listed above, it is also possible to set
760the option \*(L"called\*(R", which should be a string. This string will be
761used in any error messages caused by a failure to meet the validation
762spec.
763.PP
764This subroutine will validate named parameters as a hash if the \*(L"spec\*(R"
765parameter is a hash reference. If it is an array reference, the
766parameters are assumed to be positional.
767.PP
768.Vb 8
769\& my %p =
770\& validate_with
771\& ( params => \e@_,
772\& spec => { foo => { type => SCALAR },
773\& bar => { default => 10 } },
774\& allow_extra => 1,
775\& called => 'The Quux::Baz class constructor',
776\& );
777.Ve
778.PP
779.Vb 8
780\& my @p =
781\& validate_with
782\& ( params => \e@_,
783\& spec => [ { type => SCALAR },
784\& { default => 10 } ],
785\& allow_extra => 1,
786\& called => 'The Quux::Baz class constructor',
787\& );
788.Ve
789.SH "DISABLING VALIDATION"
790.IX Header "DISABLING VALIDATION"
791If the environment variable \f(CW\*(C`PERL_NO_VALIDATION\*(C'\fR is set to something
792true, then validation is turned off. This may be useful if you only
793want to use this module during development but don't want the speed
794hit during production.
795.PP
796The only error that will be caught will be when an odd number of
797parameters are passed into a function/method that expects a hash.
798.PP
799If you want to selectively turn validation on and off at runtime, you
800can directly set the \f(CW$Params::Validate::NO_VALIDATION\fR global
801variable. It is \fBstrongly\fR recommended that you \fBlocalize\fR any
802changes to this variable, because other modules you are using may
803expect validation to be on when they execute. For example:
804.PP
805.Vb 5
806\& {
807\& local $Params::Validate::NO_VALIDATION = 1;
808\& # no error
809\& foo( bar => 2 );
810\& }
811.Ve
812.PP
813.Vb 2
814\& # error
815\& foo( bar => 2 );
816.Ve
817.PP
818.Vb 5
819\& sub foo
820\& {
821\& my %p = validate( @_, { foo => 1 } );
822\& ...
823\& }
824.Ve
825.PP
826But if you want to shoot yourself in the foot and just turn it off, go
827ahead!
828.SH "LIMITATIONS"
829.IX Header "LIMITATIONS"
830Right now there is no way (short of a callback) to specify that
831something must be of one of a list of classes, or that it must possess
832one of a list of methods. If this is desired, it can be added in the
833future.
834.PP
835Ideally, there would be only one validation function. If someone
836figures out how to do this, please let me know.
837.SH "SUPPORT"
838.IX Header "SUPPORT"
839Please submit bugs and patches to the \s-1CPAN\s0 \s-1RT\s0 system at
840http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Params%3A%3AValidate or
841via email at bug\-params\-validate@rt.cpan.org.
842.PP
843Support questions can be sent to Dave at autarch@urth.org.
844.PP
845The code repository is at https://svn.urth.org/svn/Params\-Validate/
846.SH "DONATIONS"
847.IX Header "DONATIONS"
848If you'd like to thank me for the work I've done on this module,
849please consider making a \*(L"donation\*(R" to me via PayPal. I spend a lot of
850free time creating free software, and would appreciate any support
851you'd care to offer.
852.PP
853Please note that \fBI am not suggesting that you must do this\fR in order
854for me to continue working on this particular software. I will
855continue to do so, inasmuch as I have in the past, for as long as it
856interests me.
857.PP
858Similarly, a donation made in this way will probably not make me work
859on this software much more, unless I get so many donations that I can
860consider working on free software full time, which seems unlikely at
861best.
862.PP
863To donate, log into PayPal and send money to autarch@urth.org or use
864the button on this page:
865<http://www.urth.org/~autarch/fs\-donation.html>
866.SH "AUTHORS"
867.IX Header "AUTHORS"
868Dave Rolsky, <autarch@urth.org> and Ilya Martynov <ilya@martynov.org>
869.SH "COPYRIGHT"
870.IX Header "COPYRIGHT"
871Copyright (c) 2004\-2007 David Rolsky. All rights reserved. This
872program is free software; you can redistribute it and/or modify it
873under the same terms as Perl itself.