Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages


home | help
Config::Properties(3) User Contributed Perl DocumentationConfig::Properties(3)

       Config::Properties - Read and write property files

	 use Config::Properties;

	 # reading...

	 open my $fh, '<', 'my_config.props'
	   or die "unable to open configuration	file";

	 my $properties	= Config::Properties->new();

	 $value	= $properties->getProperty($key);

	 # saving...

	 open my $fh, '>', 'my_config.props'
	   or die "unable to open configuration	file for writing";

	 $properties->setProperty($key,	$value);

	 $properties->format('%s => %s');
	 $properties->store($fh, $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
       separated by either whitespace, the colon (:) character,	or the equals
       (=) character.  Whitespace before the key and on	either side of the
       separator 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.
       The current format string can be	obtained by using $object->format()
       (with no	arguments) or $object->getFormat().

       If a recent version of Text::Wrap 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	arguments are as follows:

	   file	=> $filename
	       Opens and reads the entries from	the given properties file

	   format => $format
	       Sets the	format using for saving	the properties to a file. See

	   wrap	=> 0
	       Disables	wrapping of long lines when saving the properties to a

	   defaults => $defaults
	       Default configuration values.

	       The given parameter can be a hash reference or another
	       Config::Properties object.

	       In that way several configuration objects can be	chained. For

		 my %defaults =	(...);
		 my $global_config = Config::Properties->new(file => '/etc/',
							     defaults => \%defaults);
		 my $user_config = Config::Properties->new(file	=> '/home/jsmith/.foo/',
							   defaults => $global_config);

	   order => 'keep'|'alpha'|'none'
	       Sets how	to order the properties	when saved to a	file or	when
	       returned	by "properties"	and "propertyNames" methods.

	       "alpha" sorts the keys in alphanumeric order. "keep" keeps the
	       order of	the properties as added	or read	from a file. "none"
	       returns the properties unordered.

	   encoding => $encoding
	       IO encoding used	to read	the configuration file.	See PerlIO.

	       When "load" is called the given encoding	is used	unless the
	       file handler already has	a encoding layer applied.

	       "latin1"	is used	as the default encoding	(as specified in the
	       Java properties specification).

	   be_like_java	=> 1
	       When this feature is enabled, the module	will try to mimic the
	       Java implementation as much as possible when saving files.

	       Currently, some escaping	rules are changed and line wrapping is

	   Calling "new" in this way is	deprecated.

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

       $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

       $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

	      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 of "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

	   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 discarded.

       $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

       Config::Properties::Simple for a	simpler	alternative interface to

       Add support for derived format as supported by Java class

       "Config::Properties" was	originally developed by	Randy Jay Yarger. It
       was maintained for some time by Craig Manley and	finally	it passed
       hands to	Salvador FandiA+-o <>, the current

       Copyright 2001, 2002 by Randy Jay Yarger	Copyright 2002,	2003 by	Craig
       Manley.	Copyright 2003-2009, 2011-2012,	2014-2015 by Salvador

       This library is free software; you can redistribute it and/or modify it
       under the same terms as Perl itself.

perl v5.32.0			  2015-05-14		 Config::Properties(3)


Want to link to this manual page? Use this URL:

home | help