{
    "mode": "perldoc",
    "parameter": "Params::Check",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Params%3A%3ACheck/json",
    "generated": "2026-06-03T10:52:45Z",
    "synopsis": "use Params::Check qw[check allow lasterror];\nsub fillpersonalinfo {\nmy %hash = @;\nmy $x;\nmy $tmpl = {\nfirstname   => { required   => 1, defined => 1 },\nlastname    => { required   => 1, store => \\$x },\ngender      => { required   => 1,\nallow      => [qr/M/i, qr/F/i],\n},\nmarried     => { allow      => [0,1] },\nage         => { default    => 21,\nallow      => qr/^\\d+$/,\n},\nphone       => { allow => [ sub { return 1 if /$validre/ },\n'1-800-PERL' ]\n},\nidlist     => { default        => [],\nstricttype    => 1\n},\nemployer    => { default => 'NSA', nooverride => 1 },\n};\n### check() returns a hashref of parsed args on success ###\nmy $parsedargs = check( $tmpl, \\%hash, $VERBOSE )\nor die qw[Could not parse arguments!];\n... other code here ...\n}\nmy $ok = allow( $colour, [qw|blue green yellow|] );\nmy $error = Params::Check::lasterror();",
    "sections": {
        "NAME": {
            "content": "Params::Check - A generic input parsing/checking mechanism.\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Params::Check qw[check allow lasterror];\n\nsub fillpersonalinfo {\nmy %hash = @;\nmy $x;\n\nmy $tmpl = {\nfirstname   => { required   => 1, defined => 1 },\nlastname    => { required   => 1, store => \\$x },\ngender      => { required   => 1,\nallow      => [qr/M/i, qr/F/i],\n},\nmarried     => { allow      => [0,1] },\nage         => { default    => 21,\nallow      => qr/^\\d+$/,\n},\n\nphone       => { allow => [ sub { return 1 if /$validre/ },\n'1-800-PERL' ]\n},\nidlist     => { default        => [],\nstricttype    => 1\n},\nemployer    => { default => 'NSA', nooverride => 1 },\n};\n\n### check() returns a hashref of parsed args on success ###\nmy $parsedargs = check( $tmpl, \\%hash, $VERBOSE )\nor die qw[Could not parse arguments!];\n\n... other code here ...\n}\n\nmy $ok = allow( $colour, [qw|blue green yellow|] );\n\nmy $error = Params::Check::lasterror();\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Params::Check is a generic input parsing/checking mechanism.\n\nIt allows you to validate input via a template. The only requirement is that the arguments must\nbe named.\n\nParams::Check can do the following things for you:\n\n*   Convert all keys to lowercase\n\n*   Check if all required arguments have been provided\n\n*   Set arguments that have not been provided to the default\n\n*   Weed out arguments that are not supported and warn about them to the user\n\n*   Validate the arguments given by the user based on strings, regexes, lists or even\nsubroutines\n\n*   Enforce type integrity if required\n\nMost of Params::Check's power comes from its template, which we'll discuss below:\n",
            "subsections": []
        },
        "Template": {
            "content": "As you can see in the synopsis, based on your template, the arguments provided will be\nvalidated.\n\nThe template can take a different set of rules per key that is used.\n\nThe following rules are available:\n\ndefault\nThis is the default value if none was provided by the user. This is also the type\n\"stricttype\" will look at when checking type integrity (see below).\n\nrequired\nA boolean flag that indicates if this argument was a required argument. If marked as\nrequired and not provided, check() will fail.\n\nstricttype\nThis does a \"ref()\" check on the argument provided. The \"ref\" of the argument must be the\nsame as the \"ref\" of the default value for this check to pass.\n\nThis is very useful if you insist on taking an array reference as argument for example.\n\ndefined\nIf this template key is true, enforces that if this key is provided by user input, its value\nis \"defined\". This just means that the user is not allowed to pass \"undef\" as a value for\nthis key and is equivalent to: allow => sub { defined $[0] && OTHER TESTS }\n\nnooverride\nThis allows you to specify \"constants\" in your template. ie, they keys that are not allowed\nto be altered by the user. It pretty much allows you to keep all your \"configurable\" data in\none place; the \"Params::Check\" template.\n\nstore\nThis allows you to pass a reference to a scalar, in which the data will be stored:\n\nmy $x;\nmy $args = check(foo => { default => 1, store => \\$x }, $input);\n\nThis is basically shorthand for saying:\n\nmy $args = check( { foo => { default => 1 }, $input );\nmy $x    = $args->{foo};\n\nYou can alter the global variable $Params::Check::NODUPLICATES to control whether the\n\"store\"'d key will still be present in your result set. See the \"Global Variables\" section\nbelow.\n\nallow\nA set of criteria used to validate a particular piece of data if it has to adhere to\nparticular rules.\n\nSee the \"allow()\" function for details.\n",
            "subsections": []
        },
        "Functions": {
            "content": "check( \\%tmpl, \\%args, [$verbose] );\nThis function is not exported by default, so you'll have to ask for it via:\n\nuse Params::Check qw[check];\n\nor use its fully qualified name instead.\n\n\"check\" takes a list of arguments, as follows:\n\nTemplate\nThis is a hash reference which contains a template as explained in the \"SYNOPSIS\" and\n\"Template\" section.\n\nArguments\nThis is a reference to a hash of named arguments which need checking.\n\nVerbose\nA boolean to indicate whether \"check\" should be verbose and warn about what went wrong in a\ncheck or not.\n\nYou can enable this program wide by setting the package variable $Params::Check::VERBOSE to\na true value. For details, see the section on \"Global Variables\" below.\n\n\"check\" will return when it fails, or a hashref with lowercase keys of parsed arguments when it\nsucceeds.\n\nSo a typical call to check would look like this:\n\nmy $parsed = check( \\%template, \\%arguments, $VERBOSE )\nor warn q[Arguments could not be parsed!];\n\nA lot of the behaviour of \"check()\" can be altered by setting package variables. See the section\non \"Global Variables\" for details on this.\n\nallow( $testme, \\@criteria );\nThe function that handles the \"allow\" key in the template is also available for independent use.\n\nThe function takes as first argument a key to test against, and as second argument any form of\ncriteria that are also allowed by the \"allow\" key in the template.\n\nYou can use the following types of values for allow:\n\nstring\nThe provided argument MUST be equal to the string for the validation to pass.\n\nregexp\nThe provided argument MUST match the regular expression for the validation to pass.\n\nsubroutine\nThe provided subroutine MUST return true in order for the validation to pass and the\nargument accepted.\n\n(This is particularly useful for more complicated data).\n\narray ref\nThe provided argument MUST equal one of the elements of the array ref for the validation to\npass. An array ref can hold all the above values.\n\nIt returns true if the key matched the criteria, or false otherwise.\n\nlasterror()\nReturns a string containing all warnings and errors reported during the last time \"check\" was\ncalled.\n\nThis is useful if you want to report then some other way than \"carp\"'ing when the verbose flag\nis on.\n\nIt is exported upon request.\n",
            "subsections": []
        },
        "Global Variables": {
            "content": "The behaviour of Params::Check can be altered by changing the following global variables:\n\n$Params::Check::VERBOSE\nThis controls whether Params::Check will issue warnings and explanations as to why certain\nthings may have failed. If you set it to 0, Params::Check will not output any warnings.\n\nThe default is 1 when warnings are enabled, 0 otherwise;\n\n$Params::Check::STRICTTYPE\nThis works like the \"stricttype\" option you can pass to \"check\", which will turn on\n\"stricttype\" globally for all calls to \"check\".\n\nThe default is 0;\n\n$Params::Check::ALLOWUNKNOWN\nIf you set this flag, unknown options will still be present in the return value, rather than\nfiltered out. This is useful if your subroutine is only interested in a few arguments, and wants\nto pass the rest on blindly to perhaps another subroutine.\n\nThe default is 0;\n\n$Params::Check::STRIPLEADINGDASHES\nIf you set this flag, all keys passed in the following manner:\n\nfunction( -key => 'val' );\n\nwill have their leading dashes stripped.\n\n$Params::Check::NODUPLICATES\nIf set to true, all keys in the template that are marked as to be stored in a scalar, will also\nbe removed from the result set.\n\nDefault is false, meaning that when you use \"store\" as a template key, \"check\" will put it both\nin the scalar you supplied, as well as in the hashref it returns.\n\n$Params::Check::PRESERVECASE\nIf set to true, Params::Check will no longer convert all keys from the user input to lowercase,\nbut instead expect them to be in the case the template provided. This is useful when you want to\nuse similar keys with different casing in your templates.\n\nUnderstand that this removes the case-insensitivity feature of this module.\n\nDefault is 0;\n\n$Params::Check::ONLYALLOWDEFINED\nIf set to true, Params::Check will require all values passed to be \"defined\". If you wish to\nenable this on a 'per key' basis, use the template option \"defined\" instead.\n\nDefault is 0;\n\n$Params::Check::SANITYCHECKTEMPLATE\nIf set to true, Params::Check will sanity check templates, validating for errors and unknown\nkeys. Although very useful for debugging, this can be somewhat slow in hot-code and large loops.\n\nTo disable this check, set this variable to \"false\".\n\nDefault is 1;\n\n$Params::Check::WARNINGSFATAL\nIf set to true, Params::Check will \"croak\" when an error during template validation occurs,\nrather than return \"false\".\n\nDefault is 0;\n\n$Params::Check::CALLERDEPTH\nThis global modifies the argument given to \"caller()\" by \"Params::Check::check()\" and is useful\nif you have a custom wrapper function around \"Params::Check::check()\". The value must be an\ninteger, indicating the number of wrapper functions inserted between the real function call and\n\"Params::Check::check()\".\n\nExample wrapper function, using a custom stacktrace:\n\nsub check {\nmy ($template, $argsin) = @;\n\nlocal $Params::Check::WARNINGSFATAL = 1;\nlocal $Params::Check::CALLERDEPTH = $Params::Check::CALLERDEPTH + 1;\nmy $argsout = Params::Check::check($template, $argsin);\n\nmystacktrace(Params::Check::lasterror) unless $argsout;\n\nreturn $argsout;\n}\n\nDefault is 0;\n",
            "subsections": []
        },
        "Acknowledgements": {
            "content": "Thanks to Richard Soderberg for his performance improvements.\n",
            "subsections": []
        },
        "BUG REPORTS": {
            "content": "Please report bugs or other issues to <bug-params-check@rt.cpan.org>.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "This module by Jos Boumans <kane@cpan.org>.\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "This library is free software; you may redistribute and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        }
    },
    "summary": "Params::Check - A generic input parsing/checking mechanism.",
    "flags": [],
    "examples": [],
    "see_also": []
}