IniConf.pm
IniConf - A Module for reading .ini-style configuration files
use IniConf;
IniConf provides a way to have readable configuration files outside
your Perl script. The configuration can be safely reloaded upon
receipt of a signal.
Get a new IniConf object with the new method:
$cfg = IniConf->new( -file => "/path/configfile.ini" );
$cfg = new IniConf -file => "/path/configfile.ini";
Optional named parameters may be specified after the configuration
file name. See the new in the
METHODS
section, below.
INI files consist of a number of sections, each preceeded with the
section name in square brackets. Parameters are specified in each
section as Name=Value. Any spaces around the equals sign will be
ignored, and the value extends to the end of the line
[section]
Parameter=Value
Both the hash mark (#) and the semicolon (;) are comment characters.
Lines that begin with either of these characters will be ignored. Any
amount of whitespace may preceed the comment character.
Multiline or multivalued fields may also be defined ala UNIX "here
document" syntax:
Parameter=<<EOT
value/line 1
value/line 2
EOT
You may use any string you want in place of ``EOT''. Note that what
follows the ``<<'' and what appears at the end of the text MUST match
exactly, including any trailing whitespace.
See the
METHODS
section, below, for settable options.
Values from the config file are fetched with the val method:
$value = $cfg->val('Section', 'Parameter');
If you want a multi-line/value field returned as an array, just
specify an array as the receiver:
@values = $cfg->val('Section', 'Parameter');
new (-file=>$filename, [-option=>value ...] )
Returns a new configuration object (or ``undef'' if the configuration
file has an error). One IniConf object is required per configuration
file. The following named parameters are available:
-
-default section
-
Specifies a section is used for default values. For example, if you
look up the ``permissions'' parameter in the ``users'' section, but there
is none, IniConf will look to your default section for a ``permissions''
value before returning undef.
-
-reloadsig signame
-
You may specify a signal (such as SIGHUP) that will cause the
configuration file to be read. This is useful for static daemons
where a full restart in order to realize a configuration change would
be undesirable. Note that your application must be tolerant of the
signal you choose. If a signal handler was already in place before
the IniConf object is created, it will be called after the
configuration file is reread. The signal handler will not be
re-enabled until after the configuration file is reread any the
previous signal handler returns.
-
-reloadwarn 0|1
-
Set -reloadwarn => 1 to enable a warning message (output to STDERR)
whenever the config file is reloaded. The reload message is of the
form:
PID reloading config file at YYYY.MM.DD HH:MM:SS
See your system documentation for information on valid signals.
-
-nocase 0|1
-
Set -nocase => 1 to handle the config file in a case-insensitive
manner (case in values is preserved, however). By default, config
files are case-sensitive (i.e., a section named 'Test' is not the same
as a section named 'test'). Note that there is an added overhead for
turning off case sensitivity.
.
val ($section, $parameter)
Returns the value of the specified parameter in section $section.
setval ($section, $parameter, $value, [ $value2, ... ])
Sets the value of parameter $section in section $section to $value (or
to a set of values). See below for methods to write the new
configuration back out to a file.
You may not set a parameter that didn't exist in the original
configuration file. setval will return undef if this is
attempted. Otherwise, it returns 1.
Forces the config file to be re-read. Also see the -reloadsig
option to the new method for a way to connect this method to a
signal (such as SIGHUP).
Returns an array containing section names in the configuration file.
If the nocase option was turned on when the config object was
created, the section names will be returned in lowercase.
Parameters ($sectionname)
Returns an array containing the parameters contained in the specified
section.
GroupMembers ($group)
Returns an array containing the members of specified $group. Groups
are specified in the config file as new sections of the form
[GroupName MemberName]
This is useful for building up lists. Note that parameters within a
``member'' section are referenced normally (i.e., the section name is
still ``Groupname Membername'', including the space).
WriteConfig ($filename)
Writes out a new copy of the configuration file. A temporary file
(ending in .new) is written out and then renamed to the specified
filename. Also see
BUGS
below.
Same as WriteConfig, but specifies that the original configuration
file should be rewritten.
@IniConf::errors
Contains a list of errors encountered while parsing the configuration
file. If the new method returns undef, check the value of this
to find out what's wrong. This value is reset each time a config file
is read.
- IniConf won't know if you change the signal handler that it's using
for config reloads.
- The signal handling stuff is almost guaranteed not to work on non-UNIX
systems.
- The output from [Re]WriteConfig/OutputConfig might not be as pretty as
it can be. Comments are tied to whatever was immediately below them.
- No locking is done by [Re]WriteConfig. When writing servers, take
care that only the parent ever calls this, and consider making your
own backup.
- The Windows INI specification (if there is one) probably isn't
followed exactly. First and foremost, IniConf is for making
easy-to-maintain (and read) configuration files.
.
Version 0.9 (beta)
Scott Hutton
E-Mail: shutton@pobox.com
WWW Home Page: http://www.pobox.com/~shutton/
Copyright (c) 1996,1997 Scott Hutton. All rights reserved. This program
is free software; you can redistribute it and/or modify it under the
same terms as Perl itself.