| deltax-modules documentation | view source | Contained in the deltax-modules distribution. |
DeltaX::Config - Perl module for reading configuration files
_____
/ \ _____ ______ ______ ___________
/ \ / \\__ \ / ___// ___// __ \_ __ \
/ Y \/ __ \_\___ \ \___ \\ ___/| | \/
\____|__ (____ /____ >____ >\___ >__|
\/ \/ \/ \/ \/ project
use DeltaX::Config;
my $config_file = new DeltaX::Config(filename=>'my.conf');
my $conf = $config_file->read();
if (!$conf) {
# write some error
}
...
if ($conf->{param1}) { ... }
Constructor. Parameters are in name => value form, ant there is only one and required parameter filename, which is a filename to read. All other parameters are directive definitions in form "directive => sub reference" (see "DIRECTIVES").
This function attemts to read given file. It returns reference to a hash with options and values or undef in case of error.
Returns last error description.
Configuration file consists of these parts:
Comment is everything from # char to end of line.
Setting is a name = value pair, spaces around name and = sign a removed.
Directives are special comments in form #!<directive>.
Long lines can be splitted by using '\' character, it must be the last character on the line (except spaces and comments). Lines are connected using one space.
Example:
name1 = this is long line \ # this is comment 1
which must be splitted. # this is comment 2
which results in:
name1 => "this is long line which must be splitted"
Name can have dots in itself, resulting hash is than:
Config file:
name.one = value1
name.two = value2
other_name = value3
Returned by read():
$conf->{name}{one} = 'value1';
$conf->{name}{two} = 'value2';
$conf->{other_name} = 'value3';
Directives are special form of comments: #!directive [parameters].
DeltaX::Config knows two of them:
It includes given file. Filename of included file is the first and only argument. If it is not absolute path, path is got from actually readed filename.
Works as include, but readed values are put in hash by this way:
Config file:
name1 = value1
#!import another.conf
In another.conf:
name1 = value2
Returned by read():
$conf->{name1} = 'value1';
$conf->{another}->{name1} = 'value2';
By setting parameter to new() you can define other directives and use it in your files. Every definition must be sub reference. This sub will be called with all arguments for this directive.
Program:
sub myspec_func {
my $arg = shift;
# return reference to a hash or undef in case of error
}
my $conf = new DeltaX::Config(filename=>'my.conf',myspec=>\&myspec_func);
Configuration file:
#!myspec something
| deltax-modules documentation | view source | Contained in the deltax-modules distribution. |