Parsing configuration files in perl




















I would suggest using yaml for your config file instead of writing your own parser config file and parser. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Parsing a text config file in perl Ask Question. Asked 7 years, 6 months ago. Active 7 years, 6 months ago. If a parameter is specified, the variable is set to that value and the result of the set operation is returned.

The varlist method can be used to extract a number of variables into a hash array. The first parameter should be a regular expression used for matching against the variable names. A second parameter may be specified any true value to indicate that the part of the variable name matching the regex should be removed when copied to the target hash.

The AppConfig module provides a streamlined interface for reading configuration files with the AppConfig::File module. The file method automatically loads the AppConfig::File module and creates an object to process the configuration file or files. Variables stored in the internal AppConfig::State are automatically updated with values specified in the configuration file. Multiple files may be passed to file and should indicate the file name or be a reference to an open file handle or glob.

The file may contain blank lines and comments prefixed by ' ' which are ignored. They will be set with the value 1, with any value explicitly specified except "0" and "off" being ignored.

The variable may also be specified with a "no" prefix to implicitly set the variable to 0. An optional equals sign may be inserted between the variable and value for clarity. Each subsequent definition of the variable adds the value to the list of previously set values for the variable. Each subsequent definition creates a new key and value in the hash array. See AppConfig::State for more information on expanding variable values.

The configuration files may have variables arranged in blocks. A block header, consisting of the block name in square brackets, introduces a configuration block. The block name and an underscore are then prefixed to the names of all variables subsequently referenced in that block. The block continues until the next block definition or to the end of the current file.

There are two methods for processing command line options. The first, args , is a small and efficient implementation which offers basic functionality. The second, getopt , offers a more powerful and complete facility by delegating the task to Johan Vroman's Getopt::Long module. Use as appropriate. Both implement on-demand loading of modules and incur no overhead until used.

The args method is used to parse simple command line options. It automatically loads the AppConfig::Args module and creates an object to process the command line arguments. Variables stored in the internal AppConfig::State are automatically updated with values specified in the arguments. The method should be passed a reference to a list of arguments to parse.

The hash includes a key for every variable declared or assigned to in the document, the key is the name of the variable including its sigil. Note that documents are currently always parsed in list context. I hope to achieve a balance where this module is useful, without becoming too much of a re-implementation of Perl.

I've labeled these items with "wishlist", "maybe", and "no", depending on whether I currently feel that I'd like to support this feature in a later version, I'd consider supporting this feature if the need arises, or I currently don't think the feature should be implemented.

For more information see the Perl Artistic License , which should have been distributed with your copy of Perl. For more information on module installation, please visit the detailed CPAN module installation guide. Interface This module has a simple OO interface.

See the section Things for further information regarding thing order. This option explicitly specifies the namespace the files are parsed in.

See the section Parsing for further information. Unless the Namespace argument is specified, the namespace a file is parsed in is generated by appending a cleaned up version of the filename to this setting. These include scalars, arrays, hashes, subroutines, IO handles, and globs. Anywhere a "things" argument is required you can specify one of two things; a string containing the special "thing" characters, or an array reference of each thing's actual name.

Each argument can take one of several values. The code reference will be called with a single argument, that of the error message. The error message is guaranteed to contain no trailing newlines in case the code reference decides to die or warn. There are various handler arguments. Called if there is a problem with a thing character or thing name in a thing argument thing thing thing. Due to the fact that the scalar slot in a glob is always filled it is not possible to distinguish from a scalar that was never defined e.

For more information on module installation, please visit the detailed CPAN module installation guide. Export The parse function is exportable upon request. Parsing Parsing is not a simple do "filename".



0コメント

  • 1000 / 1000