{
    "mode": "perldoc",
    "parameter": "AppConfig::State",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/AppConfig%3A%3AState/json",
    "generated": "2026-06-10T16:23:38Z",
    "synopsis": "use AppConfig::State;\nmy $state = AppConfig::State->new(\\%cfg);\n$state->define(\"foo\");            # very simple variable definition\n$state->define(\"bar\", \\%varcfg);  # variable specific configuration\n$state->define(\"foo|bar=i@\");     # compact format\n$state->set(\"foo\", 123);          # trivial set/get examples\n$state->get(\"foo\");\n$state->foo();                    # shortcut variable access\n$state->foo(456);                 # shortcut variable update",
    "sections": {
        "NAME": {
            "content": "AppConfig::State - application configuration state\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use AppConfig::State;\n\nmy $state = AppConfig::State->new(\\%cfg);\n\n$state->define(\"foo\");            # very simple variable definition\n$state->define(\"bar\", \\%varcfg);  # variable specific configuration\n$state->define(\"foo|bar=i@\");     # compact format\n\n$state->set(\"foo\", 123);          # trivial set/get examples\n$state->get(\"foo\");\n\n$state->foo();                    # shortcut variable access\n$state->foo(456);                 # shortcut variable update\n",
            "subsections": []
        },
        "OVERVIEW": {
            "content": "AppConfig::State is a Perl5 module to handle global configuration variables for perl programs.\nIt maintains the state of any number of variables, handling default values, aliasing,\nvalidation, update callbacks and option arguments for use by other AppConfig::* modules.\n\nAppConfig::State is distributed as part of the AppConfig bundle.\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "USING THE AppConfig::State MODULE\nTo import and use the AppConfig::State module the following line should appear in your Perl\nscript:\n\nuse AppConfig::State;\n\nThe AppConfig::State module is loaded automatically by the new() constructor of the AppConfig\nmodule.\n\nAppConfig::State is implemented using object-oriented methods. A new AppConfig::State object is\ncreated and initialised using the new() method. This returns a reference to a new\nAppConfig::State object.\n\nmy $state = AppConfig::State->new();\n\nThis will create a reference to a new AppConfig::State with all configuration options set to\ntheir default values. You can initialise the object by passing a reference to a hash array\ncontaining configuration options:\n\n$state = AppConfig::State->new( {\nCASE      => 1,\nERROR     => \\&myerror,\n} );\n\nThe new() constructor of the AppConfig module automatically passes all parameters to the\nAppConfig::State new() constructor. Thus, any global configuration values and variable\ndefinitions for AppConfig::State are also applicable to AppConfig.\n\nThe following configuration options may be specified.\n\nCASE\nDetermines if the variable names are treated case sensitively. Any non-zero value makes case\nsignificant when naming variables. By default, CASE is set to 0 and thus \"Variable\",\n\"VARIABLE\" and \"VaRiAbLe\" are all treated as \"variable\".\n\nCREATE\nBy default, CREATE is turned off meaning that all variables accessed via set() (which\nincludes access via shortcut such as \"$state->variable($value)\" which delegates to set())\nmust previously have been defined via define(). When CREATE is set to 1, calling\nset($variable, $value) on a variable that doesn't exist will cause it to be created\nautomatically.\n\nWhen CREATE is set to any other non-zero value, it is assumed to be a regular expression\npattern. If the variable name matches the regex, the variable is created. This can be used\nto specify configuration file blocks in which variables should be created, for example:\n\n$state = AppConfig::State->new( {\nCREATE => '^define',\n} );\n\nIn a config file:\n\n[define]\nname = fred           # definename gets created automatically\n\n[other]\nname = john           # othername doesn't - warning raised\n\nNote that a regex pattern specified in CREATE is applied to the real variable name rather\nthan any alias by which the variables may be accessed.\n\nPEDANTIC\nThe PEDANTIC option determines what action the configuration file (AppConfig::File) or\nargument parser (AppConfig::Args) should take on encountering a warning condition (typically\ncaused when trying to set an undeclared variable). If PEDANTIC is set to any true value, the\nparsing methods will immediately return a value of 0 on encountering such a condition. If\nPEDANTIC is not set, the method will continue to parse the remainder of the current file(s)\nor arguments, returning 0 when complete.\n\nIf no warnings or errors are encountered, the method returns 1.\n\nIn the case of a system error (e.g. unable to open a file), the method returns undef\nimmediately, regardless of the PEDANTIC option.\n\nERROR\nSpecifies a user-defined error handling routine. When the handler is called, a format string\nis passed as the first parameter, followed by any additional values, as per printf(3C).\n\nDEBUG\nTurns debugging on or off when set to 1 or 0 accordingly. Debugging may also be activated by\ncalling debug() as an object method (\"$state->debug(1)\") or as a package function\n(AppConfig::State::debug(1)), passing in a true/false value to set the debugging state\naccordingly. The package variable $AppConfig::State::DEBUG can also be set directly.\n\nThe debug() method returns the current debug value. If a new value is passed in, the\ninternal value is updated, but the previous value is returned.\n\nNote that any AppConfig::File or App::Config::Args objects that are instantiated with a\nreference to an App::State will inherit the DEBUG (and also PEDANTIC) values of the state at\nthat time. Subsequent changes to the AppConfig::State debug value will not affect them.\n\nGLOBAL\nThe GLOBAL option allows default values to be set for the DEFAULT, ARGCOUNT, EXPAND,\nVALIDATE and ACTION options for any subsequently defined variables.\n\n$state = AppConfig::State->new({\nGLOBAL => {\nDEFAULT  => '<undef>',     # default value for new vars\nARGCOUNT => 1,             # vars expect an argument\nACTION   => \\&mysetvar,  # callback when vars get set\n}\n});\n\nAny attributes specified explicitly when a variable is defined will override any GLOBAL\nvalues.\n\nSee \"DEFINING VARIABLES\" below which describes these options in detail.\n\nDEFINING VARIABLES\nThe \"define()\" function is used to pre-declare a variable and specify its configuration.\n\n$state->define(\"foo\");\n\nIn the simple example above, a new variable called \"foo\" is defined. A reference to a hash array\nmay also be passed to specify configuration information for the variable:\n\n$state->define(\"foo\", {\nDEFAULT   => 99,\nALIAS     => 'metavar1',\n});\n\nAny variable-wide GLOBAL values passed to the new() constructor in the configuration hash will\nalso be applied. Values explicitly specified in a variable's define() configuration will\noverride the respective GLOBAL values.\n\nThe following configuration options may be specified\n\nDEFAULT\nThe DEFAULT value is used to initialise the variable.\n\n$state->define(\"drink\", {\nDEFAULT => 'coffee',\n});\n\nprint $state->drink();        # prints \"coffee\"\n\nALIAS\nThe ALIAS option allows a number of alternative names to be specified for this variable. A\nsingle alias should be specified as a string. Multiple aliases can be specified as a\nreference to an array of alternatives or as a string of names separated by vertical bars,\n'|'. e.g.:\n\n# either\n$state->define(\"name\", {\nALIAS  => 'person',\n});\n\n# or\n$state->define(\"name\", {\nALIAS => [ 'person', 'user', 'uid' ],\n});\n\n# or\n$state->define(\"name\", {\nALIAS => 'person|user|uid',\n});\n\n$state->user('abw');     # equivalent to $state->name('abw');\n\nARGCOUNT\nThe ARGCOUNT option specifies the number of arguments that should be supplied for this\nvariable. By default, no additional arguments are expected for variables (ARGCOUNTNONE).\n\nThe ARGCOUNT* constants can be imported from the AppConfig module:\n\nuse AppConfig ':argcount';\n\n$state->define('foo', { ARGCOUNT => ARGCOUNTONE });\n\nor can be accessed directly from the AppConfig package:\n\nuse AppConfig;\n\n$state->define('foo', { ARGCOUNT => AppConfig::ARGCOUNTONE });\n\nThe following values for ARGCOUNT may be specified.\n\nARGCOUNTNONE (0)\nIndicates that no additional arguments are expected. If the variable is identified in a\nconfirguration file or in the command line arguments, it is set to a value of 1\nregardless of whatever arguments follow it.\n\nARGCOUNTONE (1)\nIndicates that the variable expects a single argument to be provided. The variable value\nwill be overwritten with a new value each time it is encountered.\n\nARGCOUNTLIST (2)\nIndicates that the variable expects multiple arguments. The variable value will be\nappended to the list of previous values each time it is encountered.\n\nARGCOUNTHASH (3)\nIndicates that the variable expects multiple arguments and that each argument is of the\nform \"key=value\". The argument will be split into a key/value pair and inserted into the\nhash of values each time it is encountered.\n\nARGS\nThe ARGS option can also be used to specify advanced command line options for use with\nAppConfig::Getopt, which itself delegates to Getopt::Long. See those two modules for more\ninformation on the format and meaning of these options.\n\n$state->define(\"name\", {\nARGS => \"=i@\",\n});\n\nEXPAND\nThe EXPAND option specifies how the AppConfig::File processor should expand embedded\nvariables in the configuration file values it reads. By default, EXPAND is turned off\n(EXPANDNONE) and no expansion is made.\n\nThe EXPAND* constants can be imported from the AppConfig module:\n\nuse AppConfig ':expand';\n\n$state->define('foo', { EXPAND => EXPANDVAR });\n\nor can be accessed directly from the AppConfig package:\n\nuse AppConfig;\n\n$state->define('foo', { EXPAND => AppConfig::EXPANDVAR });\n\nThe following values for EXPAND may be specified. Multiple values should be combined with\nvertical bars , '|', e.g. \"EXPANDUID | EXPANDVAR\").\n\nEXPANDNONE\nIndicates that no variable expansion should be attempted.\n\nEXPANDVAR\nIndicates that variables embedded as $var or $(var) should be expanded to the values of\nthe relevant AppConfig::State variables.\n\nEXPANDUID\nIndicates that '~' or '~uid' patterns in the string should be expanded to the current\nusers ($<), or specified user's home directory. In the first case, \"~\" is expanded to\nthe value of the \"HOME\" environment variable. In the second case, the \"getpwnam()\"\nmethod is used if it is available on your system (which it isn't on Win32).\n\nEXPANDENV\nInidicates that variables embedded as ${var} should be expanded to the value of the\nrelevant environment variable.\n\nEXPANDALL\nEquivalent to \"EXPANDVARS | EXPANDUIDS | EXPANDENVS\").\n\nEXPANDWARN\nIndicates that embedded variables that are not defined should raise a warning. If\nPEDANTIC is set, this will cause the read() method to return 0 immediately.\n\nVALIDATE\nEach variable may have a sub-routine or regular expression defined which is used to validate\nthe intended value for a variable before it is set.\n\nIf VALIDATE is defined as a regular expression, it is applied to the value and deemed valid\nif the pattern matches. In this case, the variable is then set to the new value. A warning\nmessage is generated if the pattern match fails.\n\nVALIDATE may also be defined as a reference to a sub-routine which takes as its arguments\nthe name of the variable and its intended value. The sub-routine should return 1 or 0 to\nindicate that the value is valid or invalid, respectively. An invalid value will cause a\nwarning error message to be generated.\n\nIf the GLOBAL VALIDATE variable is set (see GLOBAL in DESCRIPTION above) then this value\nwill be used as the default VALIDATE for each variable unless otherwise specified.\n\n$state->define(\"age\", {\nVALIDATE => '\\d+',\n});\n\n$state->define(\"pin\", {\nVALIDATE => \\&checkpin,\n});\n\nACTION\nThe ACTION option allows a sub-routine to be bound to a variable as a callback that is\nexecuted whenever the variable is set. The ACTION is passed a reference to the\nAppConfig::State object, the name of the variable and the value of the variable.\n\nThe ACTION routine may be used, for example, to post-process variable data, update the value\nof some other dependant variable, generate a warning message, etc.\n\nExample:\n\n$state->define(\"foo\", { ACTION => \\&mynotify });\n\nsub mynotify {\nmy $state = shift;\nmy $var   = shift;\nmy $val   = shift;\n\nprint \"$variable set to $value\";\n}\n\n$state->foo(42);        # prints \"foo set to 42\"\n\nBe aware that calling \"$state->set()\" to update the same variable from within the ACTION\nfunction will cause a recursive loop as the ACTION function is repeatedly called.\n\nDEFINING VARIABLES USING THE COMPACT FORMAT\nVariables may be defined in a compact format which allows any ALIAS and ARGS values to be\nspecified as part of the variable name. This is designed to mimic the behaviour of Johan\nVromans' Getopt::Long module.\n\nAliases for a variable should be specified after the variable name, separated by vertical bars,\n'|'. Any ARGS parameter should be appended after the variable name(s) and/or aliases.\n\nThe following examples are equivalent:\n\n$state->define(\"foo\", {\nALIAS => [ 'bar', 'baz' ],\nARGS  => '=i',\n});\n\n$state->define(\"foo|bar|baz=i\");\n\nREADING AND MODIFYING VARIABLE VALUES\nAppConfig::State defines two methods to manipulate variable values:\n\nset($variable, $value);\nget($variable);\n\nBoth functions take the variable name as the first parameter and \"set()\" takes an additional\nparameter which is the new value for the variable. \"set()\" returns 1 or 0 to indicate successful\nor unsuccessful update of the variable value. If there is an ACTION routine associated with the\nnamed variable, the value returned will be passed back from \"set()\". The \"get()\" function\nreturns the current value of the variable.\n\nOnce defined, variables may be accessed directly as object methods where the method name is the\nsame as the variable name. i.e.\n\n$state->set(\"verbose\", 1);\n\nis equivalent to\n\n$state->verbose(1);\n\nWithout parameters, the current value of the variable is returned. If a parameter is specified,\nthe variable is set to that value and the result of the set() operation is returned.\n\n$state->age(29);        # sets 'age' to 29, returns 1 (ok)\n\nVARLIST\nThe varlist() method can be used to extract a number of variables into a hash array. The first\nparameter should be a regular expression used for matching against the variable names.\n\nmy %vars = $state->varlist(\"^file\");   # all \"file*\" variables\n\nA second parameter may be specified (any true value) to indicate that the part of the variable\nname matching the regex should be removed when copied to the target hash.\n\n$state->filename(\"/tmp/file\");\n$state->filepath(\"/foo:/bar:/baz\");\n\nmy %vars = $state->varlist(\"^file\", 1);\n\n# %vars:\n#    name => /tmp/file\n#    path => \"/foo:/bar:/baz\"\n\nINTERNAL METHODS\nThe interal (private) methods of the AppConfig::State class are listed below.\n\nThey aren't intended for regular use and potential users should consider the fact that nothing\nabout the internal implementation is guaranteed to remain the same. Having said that, the\nAppConfig::State class is intended to co-exist and work with a number of other modules and these\nare considered \"friend\" classes. These methods are provided, in part, as services to them. With\nthis acknowledged co-operation in mind, it is safe to assume some stability in this core\ninterface.\n\nThe varname() method can be used to determine the real name of a variable from an alias:\n\n$varname->varname($alias);\n\nNote that all methods that take a variable name, including those listed below, can accept an\nalias and automatically resolve it to the correct variable name. There is no need to call\nvarname() explicitly to do alias expansion. The varname() method will fold all variables names\nto lower case unless CASE sensititvity is set.\n\nThe exists() method can be used to check if a variable has been defined:\n\n$state->exists($varname);\n\nThe default() method can be used to reset a variable to its default value:\n\n$state->default($varname);\n\nThe expand() method can be used to determine the EXPAND value for a variable:\n\nprint \"$varname EXPAND: \", $state->expand($varname), \"\\n\";\n\nThe argcount() method returns the value of the ARGCOUNT attribute for a variable:\n\nprint \"$varname ARGCOUNT: \", $state->argcount($varname), \"\\n\";\n\nThe validate() method can be used to determine if a new value for a variable meets any\nvalidation criteria specified for it. The variable name and intended value should be passed in.\nThe methods returns a true/false value depending on whether or not the validation succeeded:\n\nprint \"OK\\n\" if $state->validate($varname, $value);\n\nThe pedantic() method can be called to determine the current value of the PEDANTIC option.\n\nprint \"pedantic mode is \", $state->pedantic() ? \"on\" ; \"off\", \"\\n\";\n\nThe debug() method can be used to turn debugging on or off (pass 1 or 0 as a parameter). It can\nalso be used to check the debug state, returning the current internal value of\n$AppConfig::State::DEBUG. If a new debug value is provided, the debug state is updated and the\nprevious state is returned.\n\n$state->debug(1);               # debug on, returns previous value\n\nThe dumpvar($varname) and dump() methods may also be called for debugging purposes.\n\n$state->dumpvar($varname);    # show variable state\n$state->dump();                # show internal state and all vars\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Andy Wardley, <abw@wardley.org>\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright (C) 1997-2007 Andy Wardley. All Rights Reserved.\n\nCopyright (C) 1997,1998 Canon Research Centre Europe Ltd.\n\nThis module is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "AppConfig, AppConfig::File, AppConfig::Args, AppConfig::Getopt\n",
            "subsections": []
        }
    },
    "summary": "AppConfig::State - application configuration state",
    "flags": [],
    "examples": [],
    "see_also": []
}