📄 params::check.3
字号:
keys that are not allowed to be altered by the user. It pretty muchallows 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 datawill be stored:.Sp.Vb 2\& my $x;\& my $args = check(foo => { default => 1, store => \e$x }, $input);.Ve.SpThis is basically shorthand for saying:.Sp.Vb 2\& my $args = check( { foo => { default => 1 }, $input );\& my $x = $args\->{foo};.Ve.SpYou can alter the global variable \f(CW$Params::Check::NO_DUPLICATES\fR tocontrol whether the \f(CW\*(C`store\*(C'\fR'd key will still be present in yourresult 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 ithas to adhere to particular rules..SpSee the \f(CW\*(C`allow()\*(C'\fR function for details..SH "Functions".IX Header "Functions".Sh "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 itvia:.PP.Vb 1\& use Params::Check qw[check];.Ve.PPor 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 hashreference 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 warnabout what went wrong in a check or not..SpYou can enable this program wide by setting the package variable\&\f(CW$Params::Check::VERBOSE\fR to a true value. For details, see thesection 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 lowercasekeys of parsed arguments when it succeeds..PPSo 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.PPA lot of the behaviour of \f(CW\*(C`check()\*(C'\fR can be altered by settingpackage variables. See the section on \f(CW\*(C`Global Variables\*(C'\fR for detailson this..ie n .Sh "allow( $test_me, \e@criteria );".el .Sh "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 alsoavailable for independent use..PPThe function takes as first argument a key to test against, andas second argument any form of criteria that are also allowed bythe \f(CW\*(C`allow\*(C'\fR key in the template..PPYou 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 validationto pass..IP "regexp" 4.IX Item "regexp"The provided argument \s-1MUST\s0 match the regular expression for thevalidation to pass..IP "subroutine" 4.IX Item "subroutine"The provided subroutine \s-1MUST\s0 return true in order for the validationto 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 arrayref for the validation to pass. An array ref can hold all the abovevalues..PPIt returns true if the key matched the criteria, or false otherwise..Sh "\fIlast_error()\fP".IX Subsection "last_error()"Returns a string containing all warnings and errors reported duringthe last time \f(CW\*(C`check\*(C'\fR was called..PPThis 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..PPIt is exported upon request..SH "Global Variables".IX Header "Global Variables"The behaviour of Params::Check can be altered by changing thefollowing global variables:.ie n .Sh "$Params::Check::VERBOSE".el .Sh "\f(CW$Params::Check::VERBOSE\fP".IX Subsection "$Params::Check::VERBOSE"This controls whether Params::Check will issue warnings andexplanations as to why certain things may have failed.If you set it to 0, Params::Check will not output any warnings..PPThe default is 1 when warnings are enabled, 0 otherwise;.ie n .Sh "$Params::Check::STRICT_TYPE".el .Sh "\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..PPThe default is 0;.ie n .Sh "$Params::Check::ALLOW_UNKNOWN".el .Sh "\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 thereturn value, rather than filtered out. This is useful if yoursubroutine is only interested in a few arguments, and wants to passthe rest on blindly to perhaps another subroutine..PPThe default is 0;.ie n .Sh "$Params::Check::STRIP_LEADING_DASHES".el .Sh "\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.PPwill have their leading dashes stripped..ie n .Sh "$Params::Check::NO_DUPLICATES".el .Sh "\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 bestored in a scalar, will also be removed from the result set..PPDefault is false, meaning that when you use \f(CW\*(C`store\*(C'\fR as a templatekey, \f(CW\*(C`check\*(C'\fR will put it both in the scalar you supplied, as well asin the hashref it returns..ie n .Sh "$Params::Check::PRESERVE_CASE".el .Sh "\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 fromthe user input to lowercase, but instead expect them to be in thecase the template provided. This is useful when you want to usesimilar keys with different casing in your templates..PPUnderstand that this removes the case-insensitivy feature of thismodule..PPDefault is 0;.ie n .Sh "$Params::Check::ONLY_ALLOW_DEFINED".el .Sh "\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 thetemplate option \f(CW\*(C`defined\*(C'\fR instead..PPDefault is 0;.ie n .Sh "$Params::Check::SANITY_CHECK_TEMPLATE".el .Sh "\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, validatingfor errors and unknown keys. Although very useful for debugging, thiscan be somewhat slow in hot-code and large loops..PPTo disable this check, set this variable to \f(CW\*(C`false\*(C'\fR..PPDefault is 1;.ie n .Sh "$Params::Check::WARNINGS_FATAL".el .Sh "\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..PPDefault is 0;.ie n .Sh "$Params::Check::CALLER_DEPTH".el .Sh "\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 wrapperfunction around \f(CW\*(C`Params::Check::check()\*(C'\fR. The value must be aninteger, indicating the number of wrapper functions inserted betweenthe real function call and \f(CW\*(C`Params::Check::check()\*(C'\fR..PPExample 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.PPDefault is 0;.SH "AUTHOR".IX Header "AUTHOR"This module byJos Boumans <kane@cpan.org>..SH "Acknowledgements".IX Header "Acknowledgements"Thanks to Richard Soderberg for his performance improvements..SH "COPYRIGHT".IX Header "COPYRIGHT"This module iscopyright (c) 2003,2004 Jos Boumans <kane@cpan.org>.All rights reserved..PPThis library is free software;you may redistribute and/or modify it under the sameterms as Perl itself.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -