{
    "mode": "perldoc",
    "parameter": "Config::Simple",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Config%3A%3ASimple/json",
    "generated": "2026-06-09T13:39:19Z",
    "synopsis": "use Config::Simple;\n# --- Simple usage. Loads the config. file into a hash:\nConfig::Simple->importfrom('app.ini', \\%Config);\n# --- OO interface:\n$cfg = new Config::Simple('app.ini');\n# accessing values:\n$user = $cfg->param('User');\n# getting the values as a hash:\n%Config = $cfg->vars();\n# updating value with a string\n$cfg->param('User', 'sherzodR');\n# updating a value with an array:\n$cfg->param('Users', ['sherzodR', 'geek', 'merlyn']);\n# adding a new block to an ini-file:\n$cfg->param(-block=>'last-access', -values=>{'time'=>time()});\n# accessing a block of an ini-file;\n$mysql = $cfg->param(-block=>'mysql');\n# saving the changes back to file:\n$cfg->save();\n# --- tie() interface\ntie %Config, \"Config::Simple\", 'app.ini';",
    "sections": {
        "NAME": {
            "content": "Config::Simple - simple configuration file class\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Config::Simple;\n\n# --- Simple usage. Loads the config. file into a hash:\nConfig::Simple->importfrom('app.ini', \\%Config);\n\n\n# --- OO interface:\n$cfg = new Config::Simple('app.ini');\n\n# accessing values:\n$user = $cfg->param('User');\n\n# getting the values as a hash:\n%Config = $cfg->vars();\n\n# updating value with a string\n$cfg->param('User', 'sherzodR');\n\n# updating a value with an array:\n$cfg->param('Users', ['sherzodR', 'geek', 'merlyn']);\n\n# adding a new block to an ini-file:\n$cfg->param(-block=>'last-access', -values=>{'time'=>time()});\n\n# accessing a block of an ini-file;\n$mysql = $cfg->param(-block=>'mysql');\n\n# saving the changes back to file:\n$cfg->save();\n\n\n# --- tie() interface\ntie %Config, \"Config::Simple\", 'app.ini';\n",
            "subsections": []
        },
        "ABSTRACT": {
            "content": "Reading and writing configuration files is one of the most frequent tasks of any software\ndesign. Config::Simple is the library that helps you with it.\n\nConfig::Simple is a class representing configuration file object. It supports several\nconfiguration file syntax and tries to identify the file syntax automatically. Library supports\nparsing, updating and creating configuration files.\n",
            "subsections": []
        },
        "ABOUT CONFIGURATION FILES": {
            "content": "Keeping configurable variables in your program source code is ugly, really. And for people\nwithout much of a programming experience, configuring your programs is like performing black\nmagic. Besides, if you need to access these values from within multiple files, want your\nprograms to be able to update configuration files or want to provide a friendlier user interface\nfor your configuration files, you just have to store them in an external file. That's where\nConfig::Simple comes into play, making it very easy to read and write configuration files.\n\nIf you have never used configuration files before, here is a brief overview of various syntax to\nchoose from. Otherwise you can jump to \"PROGRAMMING STYLE\".\n\nSIMPLE CONFIGURATION FILE\nSimple syntax is what you need for most of your projects. These are, as the name asserts, the\nsimplest. File consists of key/value pairs, delimited by nothing but white space. Keys\n(variables) should be strictly alpha-numeric with possible dashes (-). Values can hold any\narbitrary text. Here is an example of such a configuration file:\n\nAlias     /exec\nTempFile  /usr/tmp\n\nComments start with a pound ('#') sign and cannot share the same line with other configuration\ndata.\n\nHTTP-LIKE SYNTAX\nThis format of separating key/value pairs is used by HTTP messages. Each key/value is separated\nby semi-colon (:). Keys are alphanumeric strings with possible '-'. Values can be any arbitrary\ntext:\n\nExample:\n\nAlias: /exec\nTempFile: /usr/tmp\n\nIt is OK to have spaces around ':'. Comments start with '#' and cannot share the same line with\nother configuration data.\n\nINI-FILE\nThese configuration files are more native to Win32 systems. Data is organized in blocks. Each\nkey/value pair is delimited with an equal (=) sign. Blocks are declared on their own lines\nenclosed in '[' and ']':\n\n[BLOCK1]\nKEY1=VALUE1\nKEY2=VALUE2\n\n\n[BLOCK2]\nKEY1=VALUE1\nKEY2=VALUE2\n\nYour Winamp 2.x play list is an example of such a configuration file.\n\nThis is the perfect choice if you need to organize your configuration file into categories:\n\n[site]\nurl=\"http://www.handalak.com\"\ntitle=\"Web site of a \\\"Geek\\\"\"\nauthor=sherzodr\n\n[mysql]\ndsn=\"dbi:mysql:dbname;host=handalak.com\"\nuser=sherzodr\npassword=marley01\n\nSIMPLIFIED INI-FILE\nThese files are pretty much similar to traditional ini-files, except they don't have any block\ndeclarations. This style is handy if you do not want any categorization in your configuration\nfile, but still want to use '=' delimited key/value pairs. While working with such files,\nConfig::Simple assigns them to a default block, called 'default' by default :-).\n\nurl = \"http://www.handalak.com\"\n\nComments can begin with either pound ('#') or semi-colon (';'). Each comment should reside on\nits own line\n",
            "subsections": []
        },
        "PROGRAMMING STYLE": {
            "content": "Most of the programs simply need to be able to read settings from a configuration file and\nassign them to a hash. If that's all you need, you can simply use its importfrom() - class\nmethod with the name of the configuration file and a reference to an existing (possibly empty)\nhash:\n\nConfig::Simple->importfrom('myconf.cfg', \\%Config);\n\nNow your hash %Config holds all the configuration file's key/value pairs. Keys of a hash are\nvariable names inside your configuration file, and values are their respective values. If\n\"myconf.cfg\" was a traditional ini-file, keys of the hash consist of block name and variable\ndelimited with a dot, such as \"block.var\".\n\nIf that's all you need, you can stop right here. Otherwise, read on. There is much more\nConfig::Simple offers.\n\nREADING THE CONFIGURATION FILE\nTo be able to use more features of the library, you will need to use its object interface:\n\n$cfg = new Config::Simple('app.cfg');\n\nThe above line reads and parses the configuration file accordingly. It tries to guess which\nsyntax is used by passing the file to guesssyntax() method. Alternatively, you can create an\nempty object, and only then read the configuration file in:\n\n$cfg = new Config::Simple();\n$cfg->read('app.cfg');\n\nAs in the first example, read() also calls guesssyntax() method on the file.\n\nIf, for any reason, it fails to guess the syntax correctly (which is less likely), you can try\nto debug by using its guesssyntax() method. It expects file handle for a configuration file and\nreturns the name of a syntax. Return value is one of \"ini\", \"simple\" or \"http\".\n\nopen(FH, \"app.cfg\");\nprintf(\"This file uses '%s' syntax\\n\", $cfg->guesssyntax(\\*FH));\n\nACCESSING VALUES\nAfter you read the configuration file in successfully, you can use param() method to access the\nconfiguration values. For example:\n\n$user = $cfg->param(\"User\");\n\nwill return the value of \"User\" from either simple configuration file, or http-styled\nconfiguration as well as simplified ini-files. To access the value from a traditional ini-file,\nconsider the following syntax:\n\n$user = $cfg->param(\"mysql.user\");\n\nThe above returns the value of \"user\" from within \"[mysql]\" block. Notice the use of dot \".\" to\ndelimit block and key names.\n\nConfig::Simple also supports vars() method, which, depending on the context used, returns all\nthe values either as hashref or hash:\n\nmy %Config = $cfg->vars();\nprint \"Username: $Config{User}\";\n\n# If it was a traditional ini-file:\nprint \"Username: $Config{'mysql.user'}\";\n\nIf you call vars() in scalar context, you will end up with a reference to a hash:\n\nmy $Config = $cfg->vars();\nprint \"Username: $Config->{User}\";\n\nIf you know what you're doing, you can also have an option of importing all the names from the\nconfiguration file into your current name space as global variables. All the block/key names\nwill be uppercased and will be converted to Perl's valid variable names; that is, all the dots\n(block-key separator) and other '\\W' characters will be substituted with underscore '':\n\n$cfg = new Config::Simple('app.cfg');\n$cfg->importnames();\n\n# or, with a single line:\nConfig::Simple->new('app.cfg')->importnames();\n\nprint STDERR \"Debugging mode is on\" if $DEBUGMODE;\n\nIn the above example, if there was a variable 'mode' under '[debug]' block, it will be now\naccessible via $DEBUGMODE, as opposed to $cfg->param('debug.mode');\n\n\"importnames()\" by default imports the values to its caller's name space. Optionally, you can\nspecify where to import the values by passing the name of the name space as the first argument.\nIt also prevents potential name collisions:\n\nConfig::Simple->new('app.cfg')->importnames('CFG');\nprint STDERR \"Debugging mode is on\" if $CFG::DEBUGMODE;\n\nIf all you want is to import values from a configuration file, the above syntax may still seem\nlonger than necessary. That's why Config::Simple supports importfrom() - class method, which is\ncalled with the name of the configuration file. It will call importnames() for you:\n\nConfig::Simple->importfrom('app.cfg');\n\nThe above line imports all the variables into the caller's name space. It's similar to calling",
            "subsections": [
                {
                    "name": "import_names",
                    "content": "the alternative name space to import the names into. As we already showed in the very first\nexample, you can also pass a reference to an existing hash as the second argument. In this case,\nthat hash will be modified with the values of the configuration file.\n\n# import into $CFG name space:\nConfig::Simple->importfrom('app.cfg', 'CFG');\n\n# import into %Config hash:\nConfig::Simple->importfrom('app.cfg', \\%Config);\n\nThe above line imports all the values to 'CFG' name space. importfrom() returns underlying\nConfig::Simple object (which you may not even need anymore):\n\n$cfg = Config::Simple->importfrom('app.cfg', \\my %Config);\n$cfg->write('app.cfg.bak');\n\nUPDATING THE VALUES\nConfiguration values, once read into Config::Simple, can be updated from within your program by\nusing the same param() method used for accessing them. For example:\n\n$cfg->param(\"User\", \"sherzodR\");\n\nThe above line changes the value of \"User\" to \"sherzodR\". Similar syntax is applicable for\nini-files as well:\n\n$cfg->param(\"mysql.user\", \"sherzodR\");\n\nIf the key you're trying to update does not exist, it will be created. For example, to add a new\n\"[session]\" block to your ini-file, assuming this block doesn't already exist:\n\n$cfg->param(\"session.life\", \"+1M\");\n\nYou can also delete values calling delete() method with the name of the variable:\n\n$cfg->delete('mysql.user'); # deletes 'user' under [mysql] block\n\nSAVING/WRITING CONFIGURATION FILES\nThe above updates to the configuration values are in-memory operations. They do not reflect in\nthe file itself. To modify the files accordingly, you need to call either \"write()\" or \"save()\"\nmethods on the object:\n\n$cfg->write();\n\nThe above line writes the modifications to the configuration file. Alternatively, you can pass a\nname to either write() or save() to indicate the name of the file to create instead of modifying\nexisting configuration file:\n\n$cfg->write(\"app.cfg.bak\");\n\nIf you want the changes saved at all times, you can turn \"autosave\" mode on by passing true\nvalue to $cfg->autosave(). It will make sure before your program is terminated, all the\nconfiguration values are written back to its file:\n\n$cfg = new Config::Simple('aff.cfg');\n$cfg->autosave(1);\n\nCREATING CONFIGURATION FILES\nOccasionally, your programs may want to create their own configuration files on the fly,\npossibly from a user input. To create a configuration file from scratch using Config::Simple,\nsimply create an empty configuration file object and define your syntax. You can do it by either\npassing \"syntax\" option to new(), or by calling syntax() method. Then play with param() method\nas you normally would. When you're done, call write() method with the name of the configuration\nfile:\n\n$cfg = new Config::Simple(syntax=>'ini');\n# or you could also do:\n# $cfg->autosave('ini')\n\n$cfg->param(\"mysql.dsn\", \"DBI:mysql:db;host=handalak.com\");\n$cfg->param(\"mysql.user\", \"sherzodr\");\n$cfg->param(\"mysql.pass\", 'marley01');\n$cfg->param(\"site.title\", 'sherzodR \"The Geek\"');\n$cfg->write(\"new.cfg\");\n\nThis creates a file \"new.cfg\" with the following content:\n\n; Config::Simple 4.43\n; Sat Mar  8 00:32:49 2003\n\n[site]\ntitle=sherzodR \"The Geek\"\n\n[mysql]\npass=marley01\ndsn=DBI:mysql:db;host=handalak.com\nuser=sherzodr\n\nNeat, huh? Supported syntax keywords are \"ini\", \"simple\" or \"http\". Currently there is no\nsupport for creating simplified ini-files.\n\nMULTIPLE VALUES\nEver wanted to define array of values in your single configuration variable? I have! That's why\nConfig::Simple supports this fancy feature as well. Simply separate your values with a comma:\n\nFiles hp.cgi, template.html, styles.css\n\nNow param() method returns an array of values:\n\n@files = $cfg->param(\"Files\");\nunlink $ for @files;\n\nIf you want a comma as part of a value, enclose the value(s) in double quotes:\n\nCVSFiles \"hp.cgi,v\", \"template.html,v\", \"styles.css,v\"\n\nIn case you want either of the values to hold literal quote (\"), you can escape it with a\nbacklash:\n\nSiteTitle \"sherzod \\\"The Geek\\\"\"\n\nTIE INTERFACE\nIf OO style intimidates you, and \"importfrom()\" is too simple for you, Config::Simple also\nsupports tie() interface. This interface allows you to tie() an ordinary Perl hash to the\nconfiguration file. From that point on, you can use the variable as an ordinary Perl hash.\n\ntie %Config, \"Config::Simple\", 'app.cfg';\n\n# Using %Config as an ordinary hash\nprint \"Username is '$Config{User}'\\n\";\n$Config{User} = 'sherzodR';\n\nThe difference between \"importfrom($file, \\%Hash)\" is, all the changes you make to the hash\nafter tie()ing it, will also reflect in the configuration file object. If autosave() was turned\non, they will also be written back to file:\n\ntie %Config, \"Config::Simple\", \"app.cfg\";\ntied(%Config)->autosave(1);\n\nTo access the method provided in OO syntax, you need to get underlying Config::Simple object.\nYou can do so with tied() function:\n\ntied(%Config)->write();\n\nWARNING: tie interface is experimental and not well tested yet. Let me know if you encounter a\nproblem.\n"
                }
            ]
        },
        "MISCELLANEOUS": {
            "content": "CASE SENSITIVITY\nBy default, configuration file keys and values are case sensitive. Which means,\n$cfg->param(\"User\") and $cfg->param(\"user\") are referring to two different values. But it is\npossible to force Config::Simple to ignore cases all together by enabling \"-lc\" switch while\nloading the library:\n\nuse Config::Simple ('-lc');\n\nWARNING: If you call write() or save(), while working on \"-lc\" mode, all the case information of\nthe original file will be lost. So use it if you know what you're doing.\n\nUSING QUOTES\nSome people suggest if values consist of none alpha-numeric strings, they should be enclosed in\ndouble quotes. Well, says them! Although Config::Simple supports parsing such configuration\nfiles already, it doesn't follow this rule while writing them. If you really need it to generate\nsuch compatible configuration files, \"-strict\" switch is what you need:\n\nuse Config::Simple '-strict';\n\nNow, when you write the configuration data back to files, if values hold any none alpha-numeric\nstrings, they will be quoted accordingly. All the double quotes that are part of the value will\nbe escaped with a backslash.\n\nEXCEPTION HANDLING\nConfig::Simple doesn't believe in dying that easily (unless you insult it using wrong syntax).\nIt leaves the decision to the programmer implementing the library. You can use its error() -\nclass method to access underlying error message. Methods that require you to check for their\nreturn values are read() and write(). If you pass filename to new(), you will need to check its\nreturn value as well. They return any true value indicating success, undef otherwise:\n\n# following new() always returns true:\n$cfg = new Config::Simple();\n\n# read() can fail:\n$cfg->read('app.cfg') or die $cfg->error();\n\n# following new() can fail:\n$cfg = new Config::Simple('app.cfg') or die Config::Simple->error();\n\n# importfrom() calls read(), so it can fail:\nConfig::Simple->importfrom('app.cfg', \\%Config) or die Config::Simple->error();\n\n# write() may fail:\n$cfg->write() or die $cfg->error();\n\n# tie() may fail, since it calls new() with a filename\ntie %Config, \"Config::Simple\", 'app.cfg' or die Config::Simple->error();\n",
            "subsections": []
        },
        "METHODS": {
            "content": "",
            "subsections": [
                {
                    "name": "new",
                    "content": "- constructor. Optionally accepts several arguments. Returns Config::Simple object.\nSupported arguments are filename, syntax, autosave. If there is a single argument, will be\ntreated as the name of the configuration file.\n"
                },
                {
                    "name": "autosave",
                    "content": "- turns 'autosave' mode on if passed true argument. Returns current autosave mode if used\nwithout arguments. In 'autosave' mode Config::Simple writes all the changes back to its file\nwithout you having to call write() or save()\n"
                },
                {
                    "name": "read",
                    "content": "- accepts name of the configuration file to parse. Before that, it tries to guess the syntax\nof the file by calling guesssyntax() method. Then calls either of parseinifile(),\nparsecfgfile() or parsehttpfile() accordingly. If the name of the file is provided to\nthe constructor - new(), there is no need to call read().\n"
                },
                {
                    "name": "param",
                    "content": "- used for accessing and updating configuration variables. If used with no arguments returns\nall the available names from the configuration file.\n"
                },
                {
                    "name": "delete",
                    "content": "- deletes a variable from a configuration file. $name has the same meaning and syntax as it\ndoes in param($name)\n"
                },
                {
                    "name": "clear",
                    "content": "- clears all the data from the object. Calling save() or turning autosave() on results in an\nempty configuration file as well.\n"
                },
                {
                    "name": "vars",
                    "content": "- depending on the context used, returns all the values available in the configuration file\neither as a hash or a reference to a hash\n"
                },
                {
                    "name": "import_names",
                    "content": "- imports all the names from the configuration file to the caller's name space. Optional\nargument, if passed, will be treated as the name space variables to be imported into. All\nthe names will be uppercased. Non-alphanumeric strings in the values will be underscored\n"
                },
                {
                    "name": "import_from",
                    "content": "- class method. If the second argument is a reference to an existing hash, it will load all\nthe configuration contents into that hash. If the second argument is a string, it will be\ntreated as the name space variables should be imported into, just like importnames() does.\n"
                },
                {
                    "name": "get_block",
                    "content": "is mostly used for accessing blocks in ini-styled configuration files. Returns a hashref of\nall the key/value pairs of a given block. Also supported by param() method with the help of\n\"-block\" option:\n\n$hash = $cfg->getblock('Project');\n# is the same as saying:\n$hash = $cfg->param(-block=>'Project');\n"
                },
                {
                    "name": "set_block",
                    "content": "used in assigning contents to a block in ini-styled configuration files. $name should be the\nname of a [block], and $values is assumed to be a hashref mapping key/value pairs. Also\nsupported by param() method with the help of \"-block\" and \"-value\" (or \"-values\") options:\n\n$cfg->setblock('Project', {Count=>3, 'Multiple Column' => 20});\n# is the same as:\n$cfg->param(-block=>'Project', -value=>{Count=>3, 'Multiple Column' => 20});\n\nWarning: all the contents of a block, if previously existed will be wiped out. If you want\nto set specific key/value pairs, use explicit method:\n\n$cfg->param('Project.Count', 3);\n"
                },
                {
                    "name": "as_string",
                    "content": "- returns the configuration file as a chunk of text. It is the same text used by write() and\nsave() to store the new configuration file back to file.\n"
                },
                {
                    "name": "write",
                    "content": "- writes the configuration file into disk. Argument, if passed, will be treated as the name\nof the file configuration variables should be saved in.\n"
                },
                {
                    "name": "save",
                    "content": "- same as write().\n"
                },
                {
                    "name": "dump",
                    "content": "- for debugging only. Dumps the whole Config::Simple object using Data::Dumper. Argument, if\npassed, will be treated as the name of the file object should be dumped in. The second\nargument specifies amount of indentation as documented in Data::Dumper manual. Default\nindent size is 2.\n"
                },
                {
                    "name": "error",
                    "content": "- returns the last error message from read/write or import* operations.\n"
                }
            ]
        },
        "TODO": {
            "content": "*   Support for lines with continuation character, '\\'. Currently its support is restricted and\nquite possibly buggy.\n\n*   Retaining comments while writing the configuration files back and/or methods for\nmanipulating comments. Everyone loves comments!\n\n*   Retain the order of the blocks and other variables in the configuration files.\n",
            "subsections": []
        },
        "BUGS": {
            "content": "Submit bugs and possibly patches to Sherzod B. Ruzmetov <sherzodr@cpan.org>.\n",
            "subsections": []
        },
        "CREDITS": {
            "content": "Michael Caldwell (mjc@mjcnet.com)\nwhitespace support, \"-lc\" switch and for various bug fixes\n\nScott Weinstein (Scott.Weinstein@lazard.com)\nbug fix in TIEHASH\n\nRuslan U. Zakirov <cubic@wr.miee.ru>\ndefault name space suggestion and patch\n\nHirosi Taguti\nimportnames() and importfrom() idea.\n\nVitaly Kushneriuk\nfor bug fixes and suggestions\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright (C) 2002-2003 Sherzod B. Ruzmetov.\n\nThis software is free library. You can modify and/or distribute it\nunder the same terms as Perl itself\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Sherzod B. Ruzmetov E<lt>sherzodr@cpan.orgE<gt>\nURI: http://author.handalak.com\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Config::General, Config::Simple, Config::Tiny\n",
            "subsections": []
        }
    },
    "summary": "Config::Simple - simple configuration file class",
    "flags": [],
    "examples": [],
    "see_also": []
}