NAME
Config::Hash
DESCRIPTION
Handle config files containing Perl hashes
SYNOPSIS
Read, parse and merge config files containing Perl hashes:
my $c = Config::Hash->new( filename => 'MyApp.conf' );
my $user = $c->get('mysql.test.user');
my $pass = $c->get('mysql.test.pass');
# The contents of the config file named MyApp.conf:
# {
# mysql => {
# test => {
# user => 'rick',
# pass => 'james'
# }
# }
# };
Manually initialize the config data:
my $c = Config::Hash->new(
data => {
user => 'james',
pass => 'rick',
ips => {
alpha => '127.0.0.1',
beta => '10.0.0.2'
}
}
);
say "Beta is at: " . $c->get('ips.beta');
Merge data with config files:
my $c = Config::Hash->new(
data => { server => 'localhost' },
filename => 'MyApp.conf'
);
In this case the contents of the file will merge with the data hash, with precedent given to the config file.
DESCRIPTION
Simple yet powerful config module. Why simple? Because it uses Perl hashes. Why powerful? Because it uses Perl hashes.
MERGING
Config::Hash merges two hashes so that the second hash overrides the first one. Let's say we have two hashes, A and B. Merging will proceed as follows:
Each key in B that doesn't contain a hash will be copied to A. Duplicate keys will be overwriten in favor of B.
Each key in B that contains a hash will be merged using the same algorithm described here.
Example:
# Example 1
$a = { a => 1, b => 2 };
$b = { a => 3 };
$merged = { a => 2, b => 2 };
# Example 2
$a = { a => { b => 'foo' } };
$b = { a => { b => 'baz' }, c => 'bar' };
$merged = { a => { b => 'baz', c => 'bar' } }; # Hashes merge
# Example 3:
$a = { a => [ 1, 2, 3 ] };
$b = { a => [] };
$merged = { a => [] }; # Non-hashes overwrite the other key
ATTRIBUTES
filename
Full pathname of the config file.
my $c = Config::Hash->new( filename => 'conf/stuff.pl' );
It does not matter what file extension is used, as long as the file contains a legitimate Perl hash. Example:
# conf/stuff.pl
{
redis => 1,
mongo => {
table => 'stuff',
data => 'general'
}
};
data
Load a Perl hash instead of a file.
my $c = Config::Hash->new(
data => {
redis => 1,
mysql => {
user => 'test',
pass => 'secret'
}
}
);
mode
Application mode or modes. Files that match the modes will be merged into the configuration data. Example:
my $c = Config::Hash->new(
filename => 'app.conf',
mode => 'development'
);
This will look first for a file app.conf
, then for app_development.conf
and both files will be merged. mode
can be a comma separated list of modes, so:
my $c = Config::Hash->new(
filename => 'app.conf',
mode => 'development, local, test'
);
will look for and merge app.conf
, app_development.conf
, app_local.conf
and app_test.conf
.
param
Allows for passing variables to the config hash.
my $c = Config::Hash->new(
filename => 'app.conf',
param => { base_path => '/path/to/stuff' }
);
Each key of the param
hash can be accessed via a function with the same name inside the config file:
# app.conf
{
name => 'Rick James',
path => base_path() . 'rick/james'
};
The evaluation of the config code is isolated from the rest of the code, so it doesn't have access to $self
. If you need to use $self
, you'll have to pass it in the params
hash and then reference it with self()
Note: You will have to add ()
to the function name, otherwise Perl will not recognize it as such and will die with an error.
separator
A regular expression for the value separator used by "get". The default is qr/\./
, i.e. a dot.
SUBROUTINES
get
Get a value from the config hash.
my $value = $c->get('bar.foo.baz');
my $same = $c->get('bar')->{foo}->{baz};
my $again = $c->data->{bar}->{foo}->{baz};
By default the subhash separator is a dot, but this can be changed via the "separator" attribute.
my $c = Config::Hash->new(
filename => 'app.conf',
separator => qr/\-/
);
my $value = $c->get('bar-foo-baz');
AUTHOR
minimalist - minimal@cpan.org
LICENSE
Same as Perl itself.