NAME
Config::Properties::Simple - Perl extension to manage configuration files.
SYNOPSIS
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,
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");
$value } },
defaults => { Foo => 1,
MyHexProp => '0x45' },
required => [qw( Foo )] );
ABSTRACT
Wrapper around Config::Properties to simplify its use.
DESCRIPTION
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):
- Config::Properties::Simple->new(%opts)
-
creates a new Config::Properties::Simple object and reads on the configuration file determined by the options passed through
%opts.The supported options are:
defaults => {...}-
hash reference containing default values for the configuration keys (similar to
defaultPropertiesfield in the originalConfig::Properties::newconstructor). noread => 1mode => "write"-
stops properties for being read from a file.
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
fileoption is included and defined the constructor dies even withoptionalset. format => $format-
equivalent to calling
setFormatmethod. 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 call
$cfg->fail($error)if the pair$key/$valueis not valid or return a normalized value for$value(usually, just$value) if it is good. validate => \@array-
only properties in
@arrayare allowed. Regexp are also allowed inside de array. i.e.:validate => [ qr/^Foo\.\w+$/, qw(Bar Doz) ], validate => \%hash-
%hashallows to set a condition for every property.There could be an additional
__defaultentry to be applied to properties that don't have their own entries.Supported conditions are:
\&subroutine-
calls the subroutine as
&subroutine($key, $value, $cfg)subroutine should call
$cfg->fail($error)if the pair$key/$valueis not valid or return a normalized value for$value(usually, just$value) if it is good. \@array-
property value has to be in
@array. \%hash-
$hash{$value}has to exist and its value is returned instead of the original$value. qr/regular expression/-
$valuehas to match the regular expression. borboolean-
$valuehas to be a boolean value.Valid true values are
y,yes,t,true,1.Valid false values are
n,no,f,false,0,.Case doesn't matter.
uorunsigned-
unsigned integer.
iorinteger-
integer
f,float,nornumber-
float number
s,string,aorany-
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
newmethod. - $this->save(%opts)
-
creates a new configuration file with the properties defined in the object.
%optsare passed to Config::Find to determine the configuration file name and location. - $this->fail($error)
-
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.
EXPORT
None, this package is OO.
SEE ALSO
Config::Properties, Config::Find.
AUTHOR
Salvador Fandiño, <sfandino@yahoo.com>
COPYRIGHT AND LICENSE
Copyright 2003 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:
- Around line 381:
Non-ASCII character seen before =encoding in 'Fandiño,'. Assuming CP1252