Current Path : /usr/opt/perl530/man/man3/ |
FreeBSD hs32.drive.ne.jp 9.1-RELEASE FreeBSD 9.1-RELEASE #1: Wed Jan 14 12:18:08 JST 2015 root@hs32.drive.ne.jp:/sys/amd64/compile/hs32 amd64 |
Current File : //usr/opt/perl530/man/man3/Params::Check.3 |
.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35) .\" .\" 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 .\" .\" 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::Check 3" .TH Params::Check 3 "2019-10-24" "perl v5.30.2" "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.