1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
=pod
=head1 NAME
Config::Record - Configuration file access
=head1 SYNOPSIS
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");
=head1 DESCRIPTION
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
C<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 C<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.
=head1 EXAMPLE
name = Foo
title = "Wizz bang wallop"
eek = (
OOhh
Aahhh
Wizz
)
people = (
{
forename = John
surnamne = Doe
}
{
forename = Some
surname = One
}
)
wizz = {
foo = "Elk"
ooh = "fds"
}
=head1 EXTRA PARSER FEATURES
The syntax described thus far is classed as the base feature set. By
passing the C<features> parameter when creating an instance of the
C<Config::Record> class, it is possible to turn on certain extra
features
=head2 QUOTED NON-ALPHANUMERIC KEYS
The keys for configuration parameters are normally restricted to only
contain the characters 'a-Z', '0-9', '_', '-' and '.'. Sometimes it
is desirable to allow arbitrary characters for keys. If this capability
is required then the C<quotedkeys> parameter can be set.
=head3 EXAMPLE
name = Foo
title = "Wizz bang wallop"
" some parameter " = (
foo
bar
}
"an embeded \" quote" = bar
"an embeded \\ backslash" = wizz
=head2 EXTERNAL INCLUDE FILES
With large configuration files it can be desirable to split them into
a number of smaller files. If this capability is required, then the
C<includes> feature can be requested. Each included file must follow
the syntax rules already described.
=head3 EXAMPLE
In the main file
name = Foo
title = "Wizz bang wallop"
foo = @include(somefile.cfg)
And in somefile.cfg
firstname = Joe
lastname = Blogs
Is equivalent to
name = Foo
title = "Wizz bang wallop"
foo = {
firstname = Joe
lastname = Blogs
}
=head1 METHODS
=over 4
=item my $config = Config::Record->new([file => $file],
[features => \%features]);
Creates a new config object, loading parameters from the file specified
by the C<file> parameter. The C<file> parameter can either be a string
representing a fully qualified filename, or a IO::Handle object. If the
C<file> parameter is a string, this filename will be saved and future
calls to C<load> or C<save> are permitted to omit the filename. If the
C<file> parameter is not supplied then an empty configuration record
is created.
The C<features> parameter allows extra parser features to be enabled.
The two valid keys for the associated hash as C<includes> and
C<quotedkeys> as described earlier in this document.
=item $config->load([$file]);
Loads and parses a configuration record. The C<file> parameter can either
be a string representing a fully qualified filename, or an IO::Handle
object. The C<$file> parameter may be omitted, if a filename was specified
in the constructor, or in previous calls to C<load> or C<save>. Prior to
loading the record, the current contents of this configuration are cleared.
=item $config->save([$file]);
Saves the configuration record to a file. The C<file> parameter can either
be a string representing a fully qualified filename, or an IO::Handle
object opened for writing. The C<$file> parameter may be omitted, if a
filename was specified in the constructor, or in previous calls to C<load>
or C<save>.
=item my $value = $config->get($key[, $default]);
Gets the value of a configuration parameter corresponding to the name
C<key>. If there is no value in the record, then the optional C<default>
is returned.
=item $config->set($key, $value);
Sets the value of a configuration parameter corresponding to the
name C<key>.
=item $config->view($key)
Return a new Config::Record object, rooted at the specified key.
If the key doesn't resolve to a hash reference an error will be
raised.
=item my $record = $config->record();
Retrieves a hash reference for the entire configuration record. Currently
this is the actual internal storage record, so changes will modify the
configuration. In the next release this will be changed to be a deep clone
of the internal storage record.
=back
=head1 BUGS
Config::Record has the following limitations
=over 4
=item *
If you load and then save a configuration file all comments are
removed & whitespace normalized.
=item *
Ordering of elements in hash ref are not preserved across load
and save sequence
=back
These limitations may be fixed in a future release if there is
demand from users...
=head1 AUTHORS
Daniel Berrange <dan@berrange.com>
=head1 COPYRIGHT
Copyright (C) 2000-2007 Daniel P. Berrange <dan@berrange.com>
=head1 SEE ALSO
C<perl(1)>
=cut
|