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.
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' }
);
Param is initialized as a variable $p
inside the config file, so it could be accessed this way:
# app.conf
{
name => 'Rick James',
path => $p->{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 $p->{self}
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->hash->{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.