Config...erties(3pm)
π NAME π
Config::Properties β read and write property files, bestie π¦ no cap
β¨ SYNOPSIS β¨
okay so here's the whole vibe in one go π
use Config::Properties;
# reading...
open my $fh, '<', 'my_config.props'
or die "unable to open configuration file";
my $properties = Config::Properties->new();
$properties->load($fh);
$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 );
πΈ DESCRIPTION πΈ
Config::Properties is basically the java.util.Properties API but in Perl β a near implementation, fr fr. Its whole purpose? Making it easy to read, write and mess around with Java-style property files. It's giving "I learned Java once and I'm bringing the aesthetic with me" π
π the format, explained cutely π
A Java-style property file is just key-value pairs, separated by either whitespace, the colon (:) character, or the equals (=) character. Any whitespace sitting before the key, and on either side of the separator, just gets ignored. Live your life, the parser doesn't care.
Lines that kick off with either a hash (#) or a bang (!) are comment lines β considered and then politely ignored. Not me, not today π
A backslash (\) hanging at the end of a line means CONTINUATION β the next line counts as part of the current line. And when we say "the next line counts," we mean it minus the backslash itself, minus any whitespace after the backslash, minus the line break, and minus any whitespace at the beginning of that next line. All that extra stuff? Gone. Poof. β¨
The official references used to determine this whole format live in the Java API docs for java.util.Properties, right here: <http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html>
π¦ saving & the format string π¦
When a property file gets saved, every line comes out in the format "key=value". You can change that by setting the format attribute, using either $object->format( $format_string ) or $object->setFormat( $format_string ) β bestie, they literally do the same thing π
That format string gets fed straight to printf and it must contain exactly two %s format characters (that's not a suggestion, that's the law βοΈ):
- the first
%sgets replaced with the key of the property - the second
%sgets replaced with the value
The string can't contain any other printf control characters, but other than that it can be anything you want. A newline gets automatically added to the end of the string β you don't have to add it yourself, king π«Ά
Wanna peek at the current format string? Use $object->format() with no arguments, or $object->getFormat().
And if a recent version of Text::Wrap is available, long lines get conveniently wrapped when saving. Convenient! Iconic, even π
π METHODS π
Every "Config::Property" object comes with this exact set of methods available, and yes they're all baddies:
Config::Properties->new(%opts)
Creates a brand new Config::Properties object β¨
The optional arguments are as follows β read them all, don't skip, this is the good part:
file => $filenameβ opens up and reads the entries straight out of the given properties file πformat => $formatβ sets the format used for saving the properties to a file. Go see "setFormat" for the tea.wrap => 0β disables the wrapping of long lines when saving properties to a file π«defaults => $defaultsβ the default configuration values. The parameter you hand over can be a hash reference or anotherConfig::Propertiesobject π― And that's how you chain several configuration objects together, like this:
my %defaults = (...);
my $global_config = Config::Properties->new(file => '/etc/foo.properties',
defaults => \%defaults);
my $user_config = Config::Properties->new(file => '/home/jsmith/.foo/foo.properties',
defaults => $global_config);
order => 'keep'|'alpha'|'none'β sets how the properties get ordered when saved to a file, or when returned by the"properties"and"propertyNames"methods."alpha"sorts the keys in alphanumeric order π€"keep"keeps the order of the properties exactly as they were added or read from a file π§·"none"returns the properties unordered, chaos mode π
encoding => $encodingβ the IO encoding used to read the configuration file. PeepPerlIOfor the details. When"load"is called, the given encoding gets used β unless the file handler already has an encoding layer applied, in which case, nope, hands off π"latin1"is the default encoding, as specified in the Java properties specification. Respect the classics.be_like_java => 1β turn this on and the module will try to mimic the Java implementation as much as possible when saving files. Currently, some escaping rules get changed and line wrapping gets disabled. That's the whole trade, bestie.
Config::Properties->new($defaults)
Calling "new" this way is deprecated π don't do it, use the %opts form.
$p->getProperty($k, $default, $default2, ...)
Returns property $k β or, when it's not defined, the first defined "$default*". Backup plans on deck π«Ά
$p->requireProperty($k, $default, $default2, ...)
Same energy as "getProperty", except this one dies if the property you asked for isn't found π no chill, allegedly.
$p->setProperty($k, $v)
Sets property $k's value to $v. Simple. Clean. Slay.
$p->changeProperty($k, $v)
$p->changeProperty($k, $v, $default, $default2, ...)
A method similar to setProperty, except it does nothing when the new value is equal to the one returned by "getProperty". Very "if it ain't broke" of it π
An example, because this is where it actually clicks:
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
See?? FILE1 catches the redundant write, FILE2 stays clean. That's the whole plot twist π¬
$p->deleteProperty($k)
$p->deleteProperty($k, $recurse)
Deletes property $k from the object. Gone. Ghosted. π
If $recurse is true, it also deletes any $k property from the default properties object β so it goes all the way down the chain, no survivors.
$p->properties
Returns a flatten hash with all the property key/value pairs, i.e.:
my %props=$p->properties;
$p->getProperties
Returns a hash reference with all the properties, including the ones passed in as defaults π¦
$p->propertyNames;
Returns the names of all the properties, including those passed as defaults π·οΈ
$p->splitToTree()
$p->splitToTree($regexp)
$p->splitToTree($regexp, $start)
Builds a tree out of the properties, splitting the keys with the regular expression $re β which defaults to "/\./" if you don't say otherwise π³
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' } };
Iconic. A whole nested structure out of flat dotted keys π§
The $start parameter lets you split only a subset of the properties. For instance, with the exact same data as the previous example:
my $subtree = $cfg->splitToTree(qr/\./, 'date');
makes...
$tree = { birth => '1958-09-12',
death => '2004-05-11' };
$p->setFromTree($tree)
$p->setFromTree($tree, $separator)
$p->setFromTree($tree, $separator, $start)
This one sets properties from a tree of Perl hashes and arrays. It's the exact opposite of "splitToTree" β the uno reverse card π
$separatoris the string used to join the parts of the property names. Default value? A dot:".".$startis 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
Look at those arrays getting indexed for you. So thoughtful π₯Ή
$p->changeFromTree($tree)
$p->changeFromTree($tree, $separator)
$p->changeFromTree($tree, $separator, $start)
Similar to "setFromTree", but internally it uses "changeProperty" instead of "setProperty" to set the property values. Same shape, different attitude π
$p->load($file)
Loads properties from the open file $file π₯
β οΈ heads up bestie: old properties on the object are discarded. They're gone. Not archived, not backed up, discarded. Do not come crying to me.
$p->save($file)
$p->save($file, $header)
$p->store($file)
$p->store($file, $header)
Saves the properties to the open file $file πΎ
Default properties are not saved. Nope. They stay home.
$p->saveToString($header)
Similar to "save", except instead of saving to a file it just returns a string with the content. Perfect for when you want the goods without the file system drama π
$p->getFormat()
$p->setFormat($f)
Gets/sets the format string used when saving the object to a file. The getter and the setter, right there, together, as they should be π«Ά
π SEE ALSO π
- The Java docs for
"java.util.Properties"at <http://java.sun.com/j2se/1.3/docs/api/index.html>. Config::Properties::Simpleβ for a simpler alternative interface toConfig::Properties, if all of the above was a lot π
π§ TODO π§
Add support for derived format as supported by the Java class org.apache.commons.configuration.PropertiesConfiguration (<http://commons.apache.org/configuration/apidocs/org/apache/commons/configuration/PropertiesConfiguration.html>)
(it's on the list, bestie, it's on the list π)
π AUTHORS π
"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 FandiΓ±o <sfandino@yahoo.com>, the current maintainer. A whole relay race of maintainers and we love that for them πββοΈπββοΈπ
π COPYRIGHT AND LICENSE π
Copyright 2001, 2002 by Randy Jay Yarger Copyright 2002, 2003 by Craig Manley. Copyright 2003-2009, 2011-2012, 2014-2015 by Salvador FandiΓ±o.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
okay and that's the whole page, bestie π this was Config::Properties for perl v5.36.0, dated 2022-10-13, from the User Contributed Perl Documentation. go forth and go read and write those property files, you absolute main character ππ
π¦ where this page comes from
- ubuntu noble: libconfig-properties-perl 1.80-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libconfig-properties-perl 1.80-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libconfig-properties-perl 1.80-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms