ConfigReader::Simple - Simple configuration file parser
use ConfigReader::Simple; # parse one file $config = ConfigReader::Simple->new("configrc", [qw(Foo Bar Baz Quux)]); # parse multiple files, in order $config = ConfigReader::Simple->new_multiple( Files => [ "global", "configrc" ], Keys => [qw(Foo Bar Baz Quux)] ); my @directives = $config->directives; $config->get( "Foo" ); if( $config->exists( "Bar" ) ) { print "Bar was in the config file\n"; }
ConfigReader::Simple reads and parses simple configuration files. It is designed to be smaller and simpler than the ConfigReader module and is more suited to simple configuration files.
ConfigReader::Simple
ConfigReader
Creates a ConfigReader::Simple object.
FILENAME tells the instance where to look for the configuration file. If FILENAME cannot be found, an error message for the file is added to the %ERROR hash with the FILENAME as a key, and a combined error message appears in $ERROR.
FILENAME
DIRECTIVES is an optional argument and is a reference to an array. Each member of the array should contain one valid directive. A directive is the name of a key that must occur in the configuration file. If it is not found, the method croaks. The directive list may contain all the keys in the configuration file, a sub set of keys or no keys at all.
DIRECTIVES
The new method is really a wrapper around new_multiple.
new
new_multiple
Create a configuration object from several files listed in the anonymous array value for the Files key. The module reads the files in the same order that they appear in the array. Later values override earlier ones. This allows you to specify global configurations which you may override with more specific ones:
Files
ConfigReader::Simple->new_multiple( Files => [ qw( /etc/config /usr/local/etc/config /home/usr/config ) ], );
This function croaks if the values are not array references.
If this method cannot read a file, an error message for that file is added to the %ERROR hash with the filename as a key, and a combined error message appears in $ERROR. Processing the list of filenames continues if a file cannot be found, which may produced undesired results. You can disable this feature by setting the $ConfigReader::Simple::Die variable to a true value.
Create a configuration object from several strings listed in the anonymous array value for the Strings key. The module reads the strings in the same order that they appear in the array. Later values override earlier ones. This allows you to specify global configurations which you may override with more specific ones:
Strings
ConfigReader::Simple->new_strings( Strings => [ \$global, \$local ], );
Parse another configuration file and add its directives to the current configuration object. Any directives already defined will be replaced with the new values found in FILENAME.
This does the actual work.
This is automatically called from new(), although you can reparse the configuration file by calling parse() again.
new()
parse()
Parses the string inside the reference SCALAR_REF just as if it found it in a file.
Returns the parsed value for that directive. For directives which did not have a value in the configuration file, get returns the empty string.
get
Sets the value for DIRECTIVE to VALUE. The DIRECTIVE need not already exist. This overwrites previous values.
Remove the value from DIRECTIVE, which will still exist. It's value is undef. If the DIRECTIVE does not exist, it will not be created. Returns FALSE if the DIRECTIVE does not already exist, and TRUE otherwise.
Remove the DIRECTIVE. Returns TRUE is DIRECTIVE existed and FALSE otherwise.
Returns a list of all of the directive names found in the configuration file. The keys are sorted ASCII-betically.
Return TRUE if the specified directive exists, and FALSE otherwise.
Return a copy of the object. The new object is distinct from the original.
If set to a true value, all errors are fatal.
The last error message.
The error messages from unreadable files. The key is the filename and the value is the error message.
If set to a true value, methods may output warnings.
Directives are case-sensitive.
If a directive is repeated, the first instance will silently be ignored.
Bek Oberin <gossamer@tertius.net.au> wote the original module
Kim Ryan <kimaryan@ozemail.com.au> adapted the module to make declaring keys optional. Thanks Kim.
Alan W. Jurgensen <jurgensen@berbee.com> added a change to allow the NAME=VALUE format in the configuration file.
Andy Lester, <petdance@cpan.org>, for maintaining the module while brian was on active duty.
This source is part of a SourceForge project which always has the latest sources in CVS, as well as all of the previous releases.
https://sourceforge.net/projects/brian-d-foy/
If, for some reason, I disappear from the world, one of the other members of the project can shepherd this module appropriately.
brian d foy, <bdfoy@cpan.org>, currently maintained by Andy Lester <petdance@cpan.org>
Copyright (c) 2002-2003 brian d foy. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install ConfigReader::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm ConfigReader::Simple
CPAN shell
perl -MCPAN -e shell install ConfigReader::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.