Config::Properties - Read and write property files |
Config::Properties - Read and write property files
use Config::Properties;
# reading...
open PROPS, "< my_config.props" or die "unable to open configuration file";
my $properties = new Config::Properties(); $properties->load(*PROPS);
$value = $properties->getProperty( $key );
# saving...
open PROPS, "> my_config.props" or die "unable to open configuration file for writing";
$properties->setProperty( $key, $value );
$properties->format( '%s => %s' ); $properties->store(*PROPS, $header );
Config::Properties is a near implementation of the java.util.Properties API. It is designed to allow easy reading, writing and manipulation of Java-style property files.
The format of a Java-style property file is that of a key-value pair seperated by either whitespace, the colon (:) character, or the equals (=) character. Whitespace before the key and on either side of the seperator is ignored.
Lines that begin with either a hash (#) or a bang (!) are considered comment lines and ignored.
A backslash (\) at the end of a line signifies a continuation and the next line is counted as part of the current line (minus the backslash, any whitespace after the backslash, the line break, and any whitespace at the beginning of the next line).
The official references used to determine this format can be found in the Java API docs for java.util.Properties at http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html.
When a property file is saved it is in the format ``key=value'' for each
line. This can be changed by setting the format attribute using either
$object->format( $format_string ) or $object->setFormat(
$format_string ) (they do the same thing). The format string is fed to
printf and must contain exactly two %s format characters. The first
will be replaced with the key of the property and the second with the
value. The string can contain no other printf control characters, but
can be anything else. A newline will be automatically added to the end
of the string. You an get the current format string either by using
$object->format()
(with no arguments) or $object->getFormat().
If a recent version of module the Text::Wrap manpage is available, long lines are conveniently wrapped when saving.
Config::Property
objects have this set of methods available:
new()
new($defaults)
$defaults
parameter can be used to pass another Config::Properties object
holding default property values.
$k
or when not defined, the first defined
$default*
.
getProperty
but dies if the requested
property is not found.
$k
value to $v
.
setPropery
but that does nothing when the new
value is equal to the one returned by getProperty
.
An example shows why it is useful:
my $defaults=Config::Properties->new(); $defaults->setProperty(foo => 'bar');
my $p1=Config::Properties->new($defaults); $p1->setProperty(foo => 'bar'); # we set here! $p1->store(FILE1); foo gets saved on the file
my $p2=Config::Properties->new($defaults); $p2->changeProperty(foo => 'bar'); # does nothing! $p2->store(FILE2); # foo doesn't get saved on the file
deleteProperty($k)
If $recurse
is true, it also deletes any $k
property from the
default properties object.
my %props=$p->properties;
splitToTree()
splitToTree($regexp)
$re
(or /\./
by default). For instance:
my $data = <<EOD; name = pete date.birth = 1958-09-12 date.death = 2004-05-11 surname = moo surname.length = 3 EOD
open my $fh, '<', \$data; $cfg->load(); my $tree = $cfg->splitToTree();
makes...
$tree = { date => { birth => '1958-09-12', death => '2004-05-11' }, name => 'pete', surname => { '' => 'moo', length => '3' } };
The $start
parameter allows to split only a subset of the
properties. For instance, with the same data as on the previous
example:
my $subtree = $cfg->splitToTree(qr/\./, 'date');
makes...
$tree = { birth => '1958-09-12', death => '2004-05-11' };
setFromTree($tree)
$separator
is the string used to join the parts of the property
names. The default value is a dot (.
).
$start
is a string used as the starting point for the property
names.
For instance:
my $c = Config::Properties->new; $c->setFromTree( { foo => { '' => one, hollo => [2, 3, 4, 1] }, bar => 'doo' }, '->', 'mama')
# sets properties: # mama->bar = doo # mama->foo = one # mama->foo->hollo->0 = 2 # mama->foo->hollo->1 = 3 # mama->foo->hollo->2 = 4 # mama->foo->hollo->3 = 1
changeFromTree($tree)
setFromTree
but internally uses changeProperty
instead of setProperty
to set the property values.
load($file)
$file
.
Old properties on the object are forgotten.
save($file)
store($file)
$file
. Default properties are
not saved.
saveToString($header)
save
, but instead of saving to a file, it returns a
string with the content.
getFormat()
setFormat($f)
Java docs for java.util.Properties
at
http://java.sun.com/j2se/1.3/docs/api/index.html.
the Config::Properties::Simple manpage for a simpler alternative interface to the Config::Properties manpage.
Config::Properties
was originally developed by Randy Jay Yarger. It
was mantained for some time by Craig Manley and finally it passed
hands to Salvador Fandiño <sfandino@yahoo.com>, the current
maintainer.
Copyright 2001, 2002 by Randy Jay Yarger Copyright 2002, 2003 by Craig Manley. Copyright 2003-2006 by Salvador Fandiño.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Config::Properties - Read and write property files |