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,
  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 )] );

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 defaultProperties field in the original Config::Properties::new constructor).

noread => 1
mode => "write"

stops properties for being read from a file.

utf8 => 1>

opens the file for reading/writing with the :utf8 layer.

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.

format => $format

equivalent to calling setFormat method.

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 = key1, alias2 =>key2 ... }

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.

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

validate => [ qr/^Foo\.\w+$/, qw(Bar Doz) ],
validate => \%hash

%hash allows to set a condition for every property.

There could be an additional __default entry to be applied to properties that don't have their own entries.

Supported conditions are:

\&subroutine

calls the subroutine as

&subroutine($key, $value, $cfg)

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.

qr/regular expression/

$value has to match the regular expression.

b or boolean

$value has 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.

u or unsigned

unsigned integer.

i or integer

integer

f, float, n or number

float number

s, string, a or 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.

$this->save(%opts)

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.

$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-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:

Around line 449:

Non-ASCII character seen before =encoding in 'Fandiño,'. Assuming CP1252