|
Config::Record - Configuration file access |
Config::Record - Configuration file access
use Config::Record;
# Create an empty record & then load from file
my $config = Config::Record->new();
$config->load("/etc/myapp.cfg");
# Create & load, then save to filename
my $config = Config::Record->new(file => "/etc/myapp.cfg");
$config->save("/etc/myapp.cfg");
# Load / save from filehandle
my $fh = IO::File->new("/etc/myapp.cfg");
my $config = Config::Record->new(file => $fh);
$config->save($fh);
# Get a config value, throw error if not found
my $value = $config->get("foo");
# Get a config value, return 'eek' if not found
my $value = $config->get("foo", "eek");
# Set a value
$config->set("foobar", "wizz");
# Get a deep config value (ie nested hash)
my $value = $config->get("foo/bar", "eek");
# Get first element of an array param
my $value = $config->get("people/[0]/forename");
# Get the raw hash reference forming the record my $record = $config->record();
# Get a new config object rooted at a sub-hash
my $config = $config->view("foo");
This module provides an API for loading and saving of simple configuration
file records. Entries in the configuration file are essentially key,value
pairs, with the key and values separated by a single equals symbol. The
key consists only of alphanumeric characters. There are three types of
values, scalar values can contain anything except newlines. Trailing
whitespace will be trimmed unless the value is surrounded in double
quotes. eg
foo = Wizz foo = "Wizz.... "
Long lines can be split with a backslash character, without introducing newlines. Without double quotes, whitespace at beginning and end of lines will be trimmed eg
foo = This is a long \
line of text
foo = "This is a long " \
"line of text"
Multi-line strings can be provided as 'HERE' documents, eg
foo = <<EOF This is a multiple paragraph block of text with newlines preserved EOF
Array values consist of a single right round bracket, following by
one value per line, terminated by a single left round bracket. eg
foo = (
Wizz
"Wizz... "
)
Hash values consist of a single right curly bracket, followed by one key,value pair per line, terminated by a single left curly bracket. eg
foo = {
one = Wizz
two = "Wizz.... "
}
Arrays and hashes can be nested to arbitrary depth.
name = Foo
title = "Wizz bang wallop"
eek = (
OOhh
Aahhh
Wizz
)
people = (
{
forename = John
surnamne = Doe
}
{
forename = Some
surname = One
}
)
wizz = {
foo = "Elk"
ooh = "fds"
}
file parameter. The file parameter can either be a string
representing a fully qualified filename, or a IO::Handle object. If the
file parameter is a string, this filename will be saved and future
calls to load or save are permitted to omit the filename. If the
file parameter is not supplied then an empty configuration record
is created.
file parameter can either
be a string representing a fully qualified filename, or an IO::Handle
object. The $file parameter may be omitted, if a filename was specified
in the constructor, or in previous calls to load or save. Prior to
loading the record, the current contents of this configuration are cleared.
file parameter can either
be a string representing a fully qualified filename, or an IO::Handle
object opened for writing. The $file parameter may be omitted, if a
filename was specified in the constructor, or in previous calls to load
or save.
key. If there is no value in the record, then the optional default
is returned.
key.
view($key)
Config::Record has the following limitations
These limitations may be fixed in a future release if there is demand from users...
Daniel Berrange <dan@berrange.com>
Copyright (C) 2000-2004 Daniel P. Berrange <dan@berrange.com>
perl(1)
|
Config::Record - Configuration file access |