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();
  $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

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:

creates a new Config::Properties object. The optional $defaults parameter can be used to pass another Config::Properties object holding default property values.

$p->getProperty($k, $default, $default2, ...)
return property $k or when not defined, the first defined $default*.

$p->requireProperty($k, $default, $default2, ...)
this method is similar to getProperty but dies if the requested property is not found.

$p->setProperty($k, $v)
set property $k value to $v.

$p->changeProperty($k, $v)
$p->changeProperty($k, $v, $default, $default2, ...)
method similar to 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

$p->deleteProperty($k, $recurse)
deletes property $k from the object.

If $recurse is true, it also deletes any $k property from the default properties object.

returns a flatten hash with all the property key/value pairs, i.e.:
  my %props=$p->properties;

returns a hash reference with all the properties (including those passed as defaults).

returns the names of all the properties (including those passed as defaults).

$p->splitToTree($regexp, $start)
builds a tree from the properties, splitting the keys with the regular expression $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
  open my $fh, '<', \$data;
  my $tree = $cfg->splitToTree();


  $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');


  $tree = { birth => '1958-09-12',
            death => '2004-05-11' };

$p->setFromTree($tree, $separator)
$p->setFromTree($tree, $separator, $start)
This method sets properties from a tree of Perl hashes and arrays. It is the opposite to splitToTree.

$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' },
  # 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

$p->changeFromTree($tree, $separator)
$p->changeFromTree($tree, $separator, $start)
similar to setFromTree but internally uses changeProperty instead of setProperty to set the property values.

loads properties from the open file $file.

Old properties on the object are forgotten.

$p->save($file, $header)
$p->store($file, $header)
save the properties to the open file $file. Default properties are not saved.

similar to save, but instead of saving to a file, it returns a string with the content.

get/set the format string used when saving the object to a file.


Java docs for java.util.Properties at

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 <>, 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