Server IP : 103.119.228.120 / Your IP : 3.142.40.195 Web Server : Apache System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64 User : nobody ( 99) PHP Version : 5.6.40 Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/local/ssl/local/ssl/local/ssl/local/ssl/local/ssl/local/share/man/man3/ |
Upload File : |
.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28) .\" .\" 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 turned on, 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 .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Params::Validate 3" .TH Params::Validate 3 "2016-08-24" "perl v5.16.3" "User Contributed Perl Documentation" .\" 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::Validate \- Validate method/function parameters .SH "VERSION" .IX Header "VERSION" version 1.24 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Params::Validate qw(:all); \& \& # takes named params (hash or hashref) \& sub foo { \& validate( \& @_, { \& foo => 1, # mandatory \& bar => 0, # optional \& } \& ); \& } \& \& # takes positional params \& sub bar { \& # first two are mandatory, third is optional \& validate_pos( @_, 1, 1, 0 ); \& } \& \& sub foo2 { \& validate( \& @_, { \& foo => \& # specify a type \& { type => ARRAYREF }, \& bar => \& # specify an interface \& { can => [ \*(Aqprint\*(Aq, \*(Aqflush\*(Aq, \*(Aqfrobnicate\*(Aq ] }, \& baz => { \& type => SCALAR, # a scalar ... \& # ... that is a plain integer ... \& regex => qr/^\ed+$/, \& callbacks => { # ... and smaller than 90 \& \*(Aqless than 90\*(Aq => sub { shift() < 90 }, \& }, \& } \& } \& ); \& } \& \& sub callback_with_custom_error { \& validate( \& @_, \& { \& foo => callbacks => { \& \*(Aqis an integer\*(Aq => sub { \& return 1 if $_[0] =~ /^\-?[1\-9][0\-9]*$/; \& die "$_[0] is not a valid integer value"; \& }, \& } \& } \& ); \& } \& \& sub with_defaults { \& my %p = validate( \& @_, { \& # required \& foo => 1, \& # $p{bar} will be 99 if bar is not given. bar is now \& # optional. \& bar => { default => 99 } \& } \& ); \& } \& \& sub pos_with_defaults { \& my @p = validate_pos( @_, 1, { default => 99 } ); \& } \& \& sub sets_options_on_call { \& my %p = validate_with( \& params => \e@_, \& spec => { foo => { type => SCALAR, default => 2 } }, \& normalize_keys => sub { $_[0] =~ s/^\-//; lc $_[0] }, \& ); \& } .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" The Params::Validate module allows you to validate method or function call parameters to an arbitrary level of specificity. At the simplest level, it is capable of validating the required parameters were given and that no unspecified additional parameters were passed in. .PP It is also capable of determining that a parameter is of a specific type, that it is an object of a certain class hierarchy, that it possesses certain methods, or applying validation callbacks to arguments. .SS "\s-1EXPORT\s0" .IX Subsection "EXPORT" The module always exports the \f(CW\*(C`validate()\*(C'\fR and \f(CW\*(C`validate_pos()\*(C'\fR functions. .PP It also has an additional function available for export, \&\f(CW\*(C`validate_with\*(C'\fR, which can be used to validate any type of parameters, and set various options on a per-invocation basis. .PP In addition, it can export the following constants, which are used as part of the type checking. These are \f(CW\*(C`SCALAR\*(C'\fR, \f(CW\*(C`ARRAYREF\*(C'\fR, \&\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, \&\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 in the section on Type Validation. .PP The constants are available via the export tag \f(CW\*(C`:types\*(C'\fR. There is also an \f(CW\*(C`:all\*(C'\fR tag which includes all of the constants as well as the \&\f(CW\*(C`validation_options()\*(C'\fR function. .SH "PARAMETER VALIDATION" .IX Header "PARAMETER VALIDATION" The validation mechanisms provided by this module can handle both named or positional parameters. For the most part, the same features are available for each. The biggest difference is the way that the validation specification is given to the relevant subroutine. The other difference is in the error messages produced when validation checks fail. .PP When handling named parameters, the module will accept either a hash or a hash reference. .PP Subroutines expecting named parameters should call the \f(CW\*(C`validate()\*(C'\fR subroutine like this: .PP .Vb 7 \& validate( \& @_, { \& parameter1 => validation spec, \& parameter2 => validation spec, \& ... \& } \& ); .Ve .PP Subroutines expecting positional parameters should call the \&\f(CW\*(C`validate_pos()\*(C'\fR subroutine like this: .PP .Vb 1 \& validate_pos( @_, { validation spec }, { validation spec } ); .Ve .SS "Mandatory/Optional Parameters" .IX Subsection "Mandatory/Optional Parameters" If you just want to specify that some parameters are mandatory and others are optional, this can be done very simply. .PP For a subroutine expecting named parameters, you would do this: .PP .Vb 1 \& validate( @_, { foo => 1, bar => 1, baz => 0 } ); .Ve .PP This says that the \*(L"foo\*(R" and \*(L"bar\*(R" parameters are mandatory and that the \*(L"baz\*(R" parameter is optional. The presence of any other parameters will cause an error. .PP For a subroutine expecting positional parameters, you would do this: .PP .Vb 1 \& validate_pos( @_, 1, 1, 0, 0 ); .Ve .PP This says that you expect at least 2 and no more than 4 parameters. If you have a subroutine that has a minimum number of parameters but can take any maximum number, you can do this: .PP .Vb 1 \& validate_pos( @_, 1, 1, (0) x (@_ \- 2) ); .Ve .PP This will always be valid as long as at least two parameters are given. A similar construct could be used for the more complex validation parameters described further on. .PP Please note that this: .PP .Vb 1 \& validate_pos( @_, 1, 1, 0, 1, 1 ); .Ve .PP makes absolutely no sense, so don't do it. Any zeros must come at the end of the validation specification. .PP In addition, if you specify that a parameter can have a default, then it is considered optional. .SS "Type Validation" .IX Subsection "Type Validation" This module supports the following simple types, which can be exported as constants: .IP "\(bu" 4 \&\s-1SCALAR\s0 .Sp A scalar which is not a reference, such as \f(CW10\fR or \f(CW\*(Aqhello\*(Aq\fR. A parameter that is undefined is \fBnot\fR treated as a scalar. If you want to allow undefined values, you will have to specify \f(CW\*(C`SCALAR | UNDEF\*(C'\fR. .IP "\(bu" 4 \&\s-1ARRAYREF\s0 .Sp An array reference such as \f(CW\*(C`[1, 2, 3]\*(C'\fR or \f(CW\*(C`\e@foo\*(C'\fR. .IP "\(bu" 4 \&\s-1HASHREF\s0 .Sp A hash reference such as \f(CW\*(C`{ a => 1, b => 2 }\*(C'\fR or \f(CW\*(C`\e%bar\*(C'\fR. .IP "\(bu" 4 \&\s-1CODEREF\s0 .Sp A subroutine reference such as \f(CW\*(C`\e&foo_sub\*(C'\fR or \f(CW\*(C`sub { print "hello" }\*(C'\fR. .IP "\(bu" 4 \&\s-1GLOB\s0 .Sp This one is a bit tricky. A glob would be something like \f(CW*FOO\fR, but not \f(CW\*(C`\e*FOO\*(C'\fR, which is a glob reference. It should be noted that this trick: .Sp .Vb 1 \& my $fh = do { local *FH; }; .Ve .Sp makes \f(CW$fh\fR a glob, not a glob reference. On the other hand, the return value from \f(CW\*(C`Symbol::gensym\*(C'\fR is a glob reference. Either can be used as a file or directory handle. .IP "\(bu" 4 \&\s-1GLOBREF\s0 .Sp A glob reference such as \f(CW\*(C`\e*FOO\*(C'\fR. See the \s-1GLOB\s0 entry above for more details. .IP "\(bu" 4 \&\s-1SCALARREF\s0 .Sp A reference to a scalar such as \f(CW\*(C`\e$x\*(C'\fR. .IP "\(bu" 4 \&\s-1UNDEF\s0 .Sp An undefined value .IP "\(bu" 4 \&\s-1OBJECT\s0 .Sp A blessed reference. .IP "\(bu" 4 \&\s-1BOOLEAN\s0 .Sp This is a special option, and is just a shortcut for \f(CW\*(C`UNDEF | SCALAR\*(C'\fR. .IP "\(bu" 4 \&\s-1HANDLE\s0 .Sp This option is also special, and is just a shortcut for \f(CW\*(C`GLOB | GLOBREF\*(C'\fR. However, it seems likely that most people interested in either globs or glob references are likely to really be interested in whether the parameter in question could be a valid file or directory handle. .PP To specify that a parameter must be of a given type when using named parameters, do this: .PP .Vb 6 \& validate( \& @_, { \& foo => { type => SCALAR }, \& bar => { type => HASHREF } \& } \& ); .Ve .PP If a parameter can be of more than one type, just use the bitwise or (\f(CW\*(C`|\*(C'\fR) operator to combine them. .PP .Vb 1 \& validate( @_, { foo => { type => GLOB | GLOBREF } ); .Ve .PP For positional parameters, this can be specified as follows: .PP .Vb 1 \& validate_pos( @_, { type => SCALAR | ARRAYREF }, { type => CODEREF } ); .Ve .SS "Interface Validation" .IX Subsection "Interface Validation" To specify that a parameter is expected to have a certain set of methods, we can do the following: .PP .Vb 7 \& validate( \& @_, { \& foo => \& # just has to be able to \->bar \& { can => \*(Aqbar\*(Aq } \& } \& ); \& \& ... or ... \& \& validate( \& @_, { \& foo => \& # must be able to \->bar and \->print \& { can => [qw( bar print )] } \& } \& ); .Ve .SS "Class Validation" .IX Subsection "Class Validation" A word of warning. When constructing your external interfaces, it is probably better to specify what methods you expect an object to have rather than what class it should be of (or a child of). This will make your \s-1API\s0 much more flexible. .PP With that said, if you want to validate that an incoming parameter belongs to a class (or child class) or classes, do: .PP .Vb 4 \& validate( \& @_, \& { foo => { isa => \*(AqMy::Frobnicator\*(Aq } } \& ); \& \& ... or ... \& \& validate( \& @_, \& # must be both, not either! \& { foo => { isa => [qw( My::Frobnicator IO::Handle )] } } \& ); .Ve .SS "Regex Validation" .IX Subsection "Regex Validation" If you want to specify that a given parameter must match a specific regular expression, this can be done with \*(L"regex\*(R" spec key. For example: .PP .Vb 4 \& validate( \& @_, \& { foo => { regex => qr/^\ed+$/ } } \& ); .Ve .PP The value of the \*(L"regex\*(R" key may be either a string or a pre-compiled regex created via \f(CW\*(C`qr\*(C'\fR. .PP If the value being checked against a regex is undefined, the regex is explicitly checked against the empty string ('') instead, in order to avoid \*(L"Use of uninitialized value\*(R" warnings. .PP The \f(CW\*(C`Regexp::Common\*(C'\fR module on \s-1CPAN\s0 is an excellent source of regular expressions suitable for validating input. .SS "Callback Validation" .IX Subsection "Callback Validation" If none of the above are enough, it is possible to pass in one or more callbacks to validate the parameter. The callback will be given the \&\fBvalue\fR of the parameter as its first argument. Its second argument will be all the parameters, as a reference to either a hash or array. Callbacks are specified as hash reference. The key is an id for the callback (used in error messages) and the value is a subroutine reference, such as: .PP .Vb 10 \& validate( \& @_, \& { \& foo => { \& callbacks => { \& \*(Aqsmaller than a breadbox\*(Aq => sub { shift() < $breadbox }, \& \*(Aqgreen or blue\*(Aq => sub { \& return 1 if $_[0] eq \*(Aqgreen\*(Aq || $_[0] eq \*(Aqblue\*(Aq; \& die "$_[0] is not green or blue!"; \& } \& } \& } \& } \& ); \& \& validate( \& @_, { \& foo => { \& callbacks => { \& \*(Aqbigger than baz\*(Aq => sub { $_[0] > $_[1]\->{baz} } \& } \& } \& } \& ); .Ve .PP The callback should return a true value if the value is valid. If not, it can return false or die. If you return false, a generic error message will be thrown by \f(CW\*(C`Params::Validate\*(C'\fR. .PP If your callback dies instead you can provide a custom error message. If the callback dies with a plain string, this string will be appended to an exception message generated by \f(CW\*(C`Params::Validate\*(C'\fR. If the callback dies with a reference (blessed or not), then this will be rethrown as-is by \&\f(CW\*(C`Params::Validate\*(C'\fR. .SS "Untainting" .IX Subsection "Untainting" If you want values untainted, set the \*(L"untaint\*(R" key in a spec hashref to a true value, like this: .PP .Vb 6 \& my %p = validate( \& @_, { \& foo => { type => SCALAR, untaint => 1 }, \& bar => { type => ARRAYREF } \& } \& ); .Ve .PP This will untaint the \*(L"foo\*(R" parameter if the parameters are valid. .PP Note that untainting is only done if \fIall parameters\fR are valid. Also, only the return values are untainted, not the original values passed into the validation function. .PP Asking for untainting of a reference value will not do anything, as \&\f(CW\*(C`Params::Validate\*(C'\fR will only attempt to untaint the reference itself. .SS "Mandatory/Optional Revisited" .IX Subsection "Mandatory/Optional Revisited" If you want to specify something such as type or interface, plus the fact that a parameter can be optional, do this: .PP .Vb 6 \& validate( \& @_, { \& foo => { type => SCALAR }, \& bar => { type => ARRAYREF, optional => 1 } \& } \& ); .Ve .PP or this for positional parameters: .PP .Vb 5 \& validate_pos( \& @_, \& { type => SCALAR }, \& { type => ARRAYREF, optional => 1 } \& ); .Ve .PP By default, parameters are assumed to be mandatory unless specified as optional. .SS "Dependencies" .IX Subsection "Dependencies" It also possible to specify that a given optional parameter depends on the presence of one or more other optional parameters. .PP .Vb 11 \& validate( \& @_, { \& cc_number => { \& type => SCALAR, \& optional => 1, \& depends => [ \*(Aqcc_expiration\*(Aq, \*(Aqcc_holder_name\*(Aq ], \& }, \& cc_expiration => { type => SCALAR, optional => 1 }, \& cc_holder_name => { type => SCALAR, optional => 1 }, \& } \& ); .Ve .PP In this case, \*(L"cc_number\*(R", \*(L"cc_expiration\*(R", and \*(L"cc_holder_name\*(R" are all optional. However, if \*(L"cc_number\*(R" is provided, then \&\*(L"cc_expiration\*(R" and \*(L"cc_holder_name\*(R" must be provided as well. .PP This allows you to group together sets of parameters that all must be provided together. .PP The \f(CW\*(C`validate_pos()\*(C'\fR version of dependencies is slightly different, in that you can only depend on one other parameter. Also, if for example, the second parameter 2 depends on the fourth parameter, then it implies a dependency on the third parameter as well. This is because if the fourth parameter is required, then the user must also provide a third parameter so that there can be four parameters in total. .PP \&\f(CW\*(C`Params::Validate\*(C'\fR will die if you try to depend on a parameter not declared as part of your parameter specification. .SS "Specifying defaults" .IX Subsection "Specifying defaults" If the \f(CW\*(C`validate()\*(C'\fR or \f(CW\*(C`validate_pos()\*(C'\fR functions are called in a list context, they will return a hash or containing the original parameters plus defaults as indicated by the validation spec. .PP If the function is not called in a list context, providing a default in the validation spec still indicates that the parameter is optional. .PP The hash or array returned from the function will always be a copy of the original parameters, in order to leave \f(CW@_\fR untouched for the calling function. .PP Simple examples of defaults would be: .PP .Vb 1 \& my %p = validate( @_, { foo => 1, bar => { default => 99 } } ); \& \& my @p = validate_pos( @_, 1, { default => 99 } ); .Ve .PP In scalar context, a hash reference or array reference will be returned, as appropriate. .SH "USAGE NOTES" .IX Header "USAGE NOTES" .SS "Validation failure" .IX Subsection "Validation failure" By default, when validation fails \f(CW\*(C`Params::Validate\*(C'\fR calls \&\f(CW\*(C`Carp::confess()\*(C'\fR. This can be overridden by setting the \f(CW\*(C`on_fail\*(C'\fR option, which is described in the \*(L"\s-1GLOBAL\*(R" OPTIONS\s0 section. .SS "Method calls" .IX Subsection "Method calls" When using this module to validate the parameters passed to a method call, you will probably want to remove the class/object from the parameter list \fBbefore\fR calling \f(CW\*(C`validate()\*(C'\fR or \f(CW\*(C`validate_pos()\*(C'\fR. If your method expects named parameters, then this is necessary for the \f(CW\*(C`validate()\*(C'\fR function to actually work, otherwise \f(CW@_\fR will not be usable as a hash, because it will first have your object (or class) \fBfollowed\fR by a set of keys and values. .PP Thus the idiomatic usage of \f(CW\*(C`validate()\*(C'\fR in a method call will look something like this: .PP .Vb 2 \& sub method { \& my $self = shift; \& \& my %params = validate( \& @_, { \& foo => 1, \& bar => { type => ARRAYREF }, \& } \& ); \& } .Ve .SS "Speeding Up Validation" .IX Subsection "Speeding Up Validation" In most cases, the validation spec will remain the same for each call to a subroutine. In that case, you can speed up validation by defining the validation spec just once, rather than on each call to the subroutine: .PP .Vb 4 \& my %spec = ( ... ); \& sub foo { \& my %params = validate( @_, \e%spec ); \& } .Ve .PP You can also use the \f(CW\*(C`state\*(C'\fR feature to do this: .PP .Vb 1 \& use feature \*(Aqstate\*(Aq; \& \& sub foo { \& state $spec = { ... }; \& my %params = validate( @_, $spec ); \& } .Ve .ie n .SH """GLOBAL"" OPTIONS" .el .SH "``GLOBAL'' OPTIONS" .IX Header "GLOBAL OPTIONS" Because the \s-1API\s0 for the \f(CW\*(C`validate()\*(C'\fR and \f(CW\*(C`validate_pos()\*(C'\fR functions does not make it possible to specify any options other than the validation spec, it is possible to set some options as pseudo\-'globals'. These allow you to specify such things as whether or not the validation of named parameters should be case sensitive, for one example. .PP These options are called pseudo\-'globals' because these settings are \&\fBonly applied to calls originating from the package that set the options\fR. .PP In other words, if I am in package \f(CW\*(C`Foo\*(C'\fR and I call \&\f(CW\*(C`validation_options()\*(C'\fR, those options are only in effect when I call \&\f(CW\*(C`validate()\*(C'\fR from package \f(CW\*(C`Foo\*(C'\fR. .PP While this is quite different from how most other modules operate, I feel that this is necessary in able to make it possible for one module/application to use Params::Validate while still using other modules that also use Params::Validate, perhaps with different options set. .PP The downside to this is that if you are writing an app with a standard calling style for all functions, and your app has ten modules, \fBeach module must include a call to \f(CB\*(C`validation_options()\*(C'\fB\fR. You could of course write a module that all your modules use which uses various trickery to do this when imported. .SS "Options" .IX Subsection "Options" .IP "\(bu" 4 normalize_keys => \f(CW$callback\fR .Sp This option is only relevant when dealing with named parameters. .Sp This callback will be used to transform the hash keys of both the parameters and the parameter spec when \f(CW\*(C`validate()\*(C'\fR or \&\f(CW\*(C`validate_with()\*(C'\fR are called. .Sp Any alterations made by this callback will be reflected in the parameter hash that is returned by the validation function. For example: .Sp .Vb 7 \& sub foo { \& return validate_with( \& params => \e@_, \& spec => { foo => { type => SCALAR } }, \& normalize_keys => \& sub { my $k = shift; $k =~ s/^\-//; return uc $k }, \& ); \& \& } \& \& %p = foo( foo => 20 ); \& \& # $p{FOO} is now 20 \& \& %p = foo( \-fOo => 50 ); \& \& # $p{FOO} is now 50 .Ve .Sp The callback must return a defined value. .Sp If a callback is given then the deprecated \*(L"ignore_case\*(R" and \&\*(L"strip_leading\*(R" options are ignored. .IP "\(bu" 4 allow_extra => \f(CW$boolean\fR .Sp If true, then the validation routine will allow extra parameters not named in the validation specification. In the case of positional parameters, this allows an unlimited number of maximum parameters (though a minimum may still be set). Defaults to false. .IP "\(bu" 4 on_fail => \f(CW$callback\fR .Sp If given, this callback will be called whenever a validation check fails. It will be called with a single parameter, which will be a string describing the failure. This is useful if you wish to have this module throw exceptions as objects rather than as strings, for example. .Sp This callback is expected to \f(CW\*(C`die()\*(C'\fR internally. If it does not, the validation will proceed onwards, with unpredictable results. .Sp The default is to simply use the Carp module's \f(CW\*(C`confess()\*(C'\fR function. .IP "\(bu" 4 stack_skip => \f(CW$number\fR .Sp This tells Params::Validate how many stack frames to skip when finding a subroutine name to use in error messages. By default, it looks one frame back, at the immediate caller to \f(CW\*(C`validate()\*(C'\fR or \&\f(CW\*(C`validate_pos()\*(C'\fR. If this option is set, then the given number of frames are skipped instead. .IP "\(bu" 4 ignore_case => \f(CW$boolean\fR .Sp \&\s-1DEPRECATED\s0 .Sp This is only relevant when dealing with named parameters. If it is true, then the validation code will ignore the case of parameter names. Defaults to false. .IP "\(bu" 4 strip_leading => \f(CW$characters\fR .Sp \&\s-1DEPRECATED\s0 .Sp This too is only relevant when dealing with named parameters. If this is given then any parameters starting with these characters will be considered equivalent to parameters without them entirely. For example, if this is specified as '\-', then \f(CW\*(C`\-foo\*(C'\fR and \f(CW\*(C`foo\*(C'\fR would be considered identical. .SH "PER-INVOCATION OPTIONS" .IX Header "PER-INVOCATION OPTIONS" The \f(CW\*(C`validate_with()\*(C'\fR function can be used to set the options listed above on a per-invocation basis. For example: .PP .Vb 8 \& my %p = validate_with( \& params => \e@_, \& spec => { \& foo => { type => SCALAR }, \& bar => { default => 10 } \& }, \& allow_extra => 1, \& ); .Ve .PP In addition to the options listed above, it is also possible to set the option \*(L"called\*(R", which should be a string. This string will be used in any error messages caused by a failure to meet the validation spec. .PP This subroutine will validate named parameters as a hash if the \*(L"spec\*(R" parameter is a hash reference. If it is an array reference, the parameters are assumed to be positional. .PP .Vb 9 \& my %p = validate_with( \& params => \e@_, \& spec => { \& foo => { type => SCALAR }, \& bar => { default => 10 } \& }, \& allow_extra => 1, \& called => \*(AqThe Quux::Baz class constructor\*(Aq, \& ); \& \& my @p = validate_with( \& params => \e@_, \& spec => [ \& { type => SCALAR }, \& { default => 10 } \& ], \& allow_extra => 1, \& called => \*(AqThe Quux::Baz class constructor\*(Aq, \& ); .Ve .SH "DISABLING VALIDATION" .IX Header "DISABLING VALIDATION" If the environment variable \f(CW\*(C`PERL_NO_VALIDATION\*(C'\fR is set to something true, then validation is turned off. This may be useful if you only want to use this module during development but don't want the speed hit during production. .PP The only error that will be caught will be when an odd number of parameters are passed into a function/method that expects a hash. .PP If you want to selectively turn validation on and off at runtime, you can directly set the \f(CW$Params::Validate::NO_VALIDATION\fR global variable. It is \fBstrongly\fR recommended that you \fBlocalize\fR any changes to this variable, because other modules you are using may expect validation to be on when they execute. For example: .PP .Vb 2 \& { \& local $Params::Validate::NO_VALIDATION = 1; \& \& # no error \& foo( bar => 2 ); \& } \& \& # error \& foo( bar => 2 ); \& \& sub foo { \& my %p = validate( @_, { foo => 1 } ); \& ...; \& } .Ve .PP But if you want to shoot yourself in the foot and just turn it off, go ahead! .SH "SPECIFYING AN IMPLEMENTATION" .IX Header "SPECIFYING AN IMPLEMENTATION" This module ships with two equivalent implementations, one in \s-1XS\s0 and one in pure Perl. By default, it will try to load the \s-1XS\s0 version and fall back to the pure Perl implementation as needed. If you want to request a specific version, you can set the \f(CW\*(C`PARAMS_VALIDATE_IMPLEMENTATION\*(C'\fR environment variable to either \f(CW\*(C`XS\*(C'\fR or \f(CW\*(C`PP\*(C'\fR. If the implementation you ask for cannot be loaded, then this module will die when loaded. .SH "TAINT MODE" .IX Header "TAINT MODE" The \s-1XS\s0 implementation of this module has some problems Under taint mode with versions of Perl before 5.14. If validation \fIfails\fR, then instead of getting the expected error message you'll get a message like \*(L"Insecure dependency in eval_sv\*(R". This can be worked around by either untainting the arguments yourself, using the pure Perl implementation, or upgrading your Perl. .SH "LIMITATIONS" .IX Header "LIMITATIONS" Right now there is no way (short of a callback) to specify that something must be of one of a list of classes, or that it must possess one of a list of methods. If this is desired, it can be added in the future. .PP Ideally, there would be only one validation function. If someone figures out how to do this, please let me know. .SH "SUPPORT" .IX Header "SUPPORT" Bugs may be submitted through the \s-1RT\s0 bug tracker <http://rt.cpan.org/Public/Dist/Display.html?Name=Params-Validate> (or bug\-params\-validate@rt.cpan.org <mailto:bug-params-validate@rt.cpan.org>). .PP I am also usually active on \s-1IRC\s0 as 'drolsky' on \f(CW\*(C`irc://irc.perl.org\*(C'\fR. .SH "DONATIONS" .IX Header "DONATIONS" If you'd like to thank me for the work I've done on this module, please consider making a \*(L"donation\*(R" to me via PayPal. I spend a lot of free time creating free software, and would appreciate any support you'd care to offer. .PP Please note that \fBI am not suggesting that you must do this\fR in order for me to continue working on this particular software. I will continue to do so, inasmuch as I have in the past, for as long as it interests me. .PP Similarly, a donation made in this way will probably not make me work on this software much more, unless I get so many donations that I can consider working on free software full time (let's all have a chuckle at that together). .PP To donate, log into PayPal and send money to autarch@urth.org, or use the button at <http://www.urth.org/~autarch/fs\-donation.html>. .SH "AUTHORS" .IX Header "AUTHORS" .IP "\(bu" 4 Dave Rolsky <autarch@urth.org> .IP "\(bu" 4 Ilya Martynov <ilya@martynov.org> .SH "CONTRIBUTORS" .IX Header "CONTRIBUTORS" .IP "\(bu" 4 Ivan Bessarabov <ivan@bessarabov.ru> .IP "\(bu" 4 J.R. Mash <jmash.code@gmail.com> .IP "\(bu" 4 Noel Maddy <zhtwnpanta@gmail.com> .IP "\(bu" 4 Olivier Mengue\*' <dolmen@cpan.org> .IP "\(bu" 4 Tony Cook <tony@develop\-help.com> .IP "\(bu" 4 Vincent Pit <perl@profvince.com> .SH "COPYRIGHT AND LICENCE" .IX Header "COPYRIGHT AND LICENCE" This software is Copyright (c) 2001 \- 2016 by Dave Rolsky and Ilya Martynov. .PP This is free software, licensed under: .PP .Vb 1 \& The Artistic License 2.0 (GPL Compatible) .Ve