Config::Properties::Simple - Perl extension to manage configuration files.
use Config::Properties::Simple; my $cfg=Config::Properties::Simple->new(); my $foo=$cfg->getProperty('foo', 'default foo'); $cfg->setProperty(bar => 'my bar') $cfg->save my $cfg2=Config::Properties::Simple->new( name => 'app/file', file => $opt_c, optional => 1, aliases => { Fhoo => 'Foo', Bhar => 'Bar' }, validate => { Foo => 'boolean', MyHexProp => qr/^0x[0-9a-f]+$/i, Odd => sub { my ($key, $value, $cfg)=@_; $value = int $value; $value & 1 or $cfg->fail("$value is not odd"); 1 } }, defaults => { Foo => 1, MyHexProp => '0x45' }, required => [qw( Foo )] );
Wrapper around Config::Properties to simplify its use.
This package mix functionality in Config::Properties and Config::Find packages to provide a simple access to configuration files.
It changes new and save methods of Config::Properties (every other method continues to work as usual):
new
save
creates a new Config::Properties::Simple object and reads on the configuration file determined by the options passed through %opts.
%opts
The supported options are:
defaults => {...}
hash reference containing default values for the configuration keys (similar to defaultProperties field in the original Config::Properties::new constructor).
defaultProperties
Config::Properties::new
noread => 1
mode => "write"
stops properties for being read from a file.
utf8 =
opens the file for reading/writing with the :utf8 layer.
:utf8
optional => 1
by default an exception is thrown when the configuration file can not be found or opened, this option makes the constructor succeed anyway.
If the file option is included and defined the constructor dies unless optional value is greater than 1. This is useful to let the user pass the configuration file name on the script command line when you want the script to fail if it's not found.
file
optional
format => $format
equivalent to calling setFormat method.
setFormat
dups_ok => 1
by default, an error is reported when two similar keys are found on the same file, setting dups_ok causes previous values to be ignored instead.
aliases => { alias1 =
entries on the configuration file whose keys are found on the aliases hash are normalized to the corresponding key. Aliases only affect parsing and are not taken into account for default values or when getting or setting properties.
validate => ...
sets conditions that the properties in the configuration file have to meet.
There are several formats allowed:
validate => \&subroutine
calls the subroutine as
&subroutine($key, $value, $cfg)
subroutine should return a true value if the pair $key $value is valid or false otherwise. For customized error messages $cfg->fail($error) can be called.
$key
$value
$cfg->fail($error)
Both $key and $value can be modified manipulating the @_ array directly. Its sometimes useful to normalize the value, i.e.:
@_
use Date::Manip; sub validate_date { defined($_[1] = Date::Manip::ParseDate($_[1])) } my $cfg = Config::Properties::Simple->new(validate => \&validate_date);
validate => \@array
only properties in @array are allowed. Regexp are also allowed inside de array. i.e.:
@array
validate => [ qr/^Foo\.\w+$/, qw(Bar Doz) ],
validate => \%hash
%hash allows to set a condition for every property.
%hash
There could be an additional __default entry to be applied to properties that don't have their own entries.
__default
Supported conditions are:
\&subroutine
similar to passing a validating subrutine (explained before).
\@array
property value has to be in @array.
\%hash
$hash{$value} has to exist and its value is returned instead of the original $value.
$hash{$value}
qr/regular expression/
$value has to match the regular expression.
b
boolean
$value has to be a boolean value.
Valid true values are y, yes, t, true, 1.
y
yes
t
true
1
Valid false values are n, no, f, false, 0, .
n
no
f
false
0
Case doesn't matter.
u
unsigned
unsigned integer.
i
integer
float
number
float number
s
string
a
any
anything is ok.
required => [...]
properties that have to be included in the configuration file. When someone is missing, an exception is raised telling the user the reason.
Any option accepted by Config::Find can also be used in new method.
creates a new configuration file with the properties defined in the object.
%opts are passed to Config::Find->find() to determine the configuration file name and location.
Config::Find->find()
method to be called from inside validation subs to report an error. It appends the filename and the line number to the error and throws an exception that if uncatched will show the user what went wrong.
None, this package is OO.
Config::Properties, Config::Find.
Salvador Fandiño, <sfandino@yahoo.com>
Copyright 2003-2005 by Salvador Fandiño
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1 POD Error
The following errors were encountered while parsing the POD:
Non-ASCII character seen before =encoding in 'Fandiño,'. Assuming CP1252
To install Config::Properties::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Config::Properties::Simple
CPAN shell
perl -MCPAN -e shell install Config::Properties::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.