The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Config::FromHash - Read config files containing hashes

Requires Perl 5.10.1 Travis status

VERSION

Version 0.0800, released 2015-10-27.

SYNOPSIS

# in config file
{
    thing => 'something',
    things => ['lots', 'of', 'things'],
    deep => {
        ocean => 'submarine',
    },
}

# somewhere else
use Config::FromHash;

my $config = Config::FromHash->new(filename => 'path/to/theconfig.conf', data => { deep => { ocean => 'thing' });

# prints 'submarine'
print $config->get('deep/ocean');

DESCRIPTION

Config::FromHash is yet another config file handler. This one reads config files that contain a Perl hash.

The following options are available

my $config = Config::FromHash->new(
    filename => 'path/to/config.file',
    filenames => ['path/to/highest_priority_config.file', 'path/to/might_be_overwritten.file'],
    environment => 'production',
    environments => ['production', 'standard'],
    data => { default => { data => ['structure'] } },
    require_all_files => 1,
);

data

Optional. If it exists its value is used as the default settings and will be overwritten if the same setting exists in a config file.

filename or filenames

Optional. filenames is an alias for filename. It reads better to use filenames if you have many config files.

Files are parsed left to right. That is, as soon as a setting is found in a file (while reading left to right) that setting is not overwritten.

environment or environments

Optional. environments is an alias for environment It reads better to use environment if you have many environments.

If this is set its value is inserted into all config file names, just before the final dot.

Environments are read left to right. All files from each environment is read before moving on to the next environment. See Examples below.

An environment can be undef.

require_all_files

Default: 0

If set to a true value Config::FromHash will die if any config file doesn't exist. Otherwise it will silently skip such files.

sep

Default: qr{/}

The separator used to split the argument to get():

my $config = Config::FromHash->new(sep => qr{\.}, data => { some => { nested => { data => { is => 'deep' }}}});

# prints 'deep'
print $config->get('some.nested.data.is');

METHODS

$self->get($path)

Returns the value that exists at $path. $path is translated into hash keys, and is separated by /.

$self->data

Returns the entire hash after all config files have been read.

$self->config_files

Returns a list of parsed config files. Mostly useful as a debuging tool, especially if require_all_files is false, and the contents of $self->data doesn't match expectations.

EXAMPLES

 my $config = Config::FromHash->new(
    filename => '/path/to/config.file',
    data => { some => 'setting' },
};

Will read

/path/to/config.file

And any setting that exists in data that has not yet been set will be set.

my $config = Config::FromHash->new(
    filenames => ['/path/to/highest_priority_config.file', '/path/to/might_be_overwritten.file'],
    environments => ['production', 'standard', undef],
    data => { default => { data => ['structure'] } },
);

The following files are read (with decreasing priority)

/path/to/highest_priority_config.production.file
/path/to/might_be_overwritten.production.file
/path/to/highest_priority_config.standard.file
/path/to/might_be_overwritten.standard.file
/path/to/highest_priority_config.file
/path/to/might_be_overwritten.file

And then any setting that exists in data that has not yet been set will be set.

my $config->new(data => { hello => 'world', can => { find => ['array', 'refs'] });

# $hash becomes { hello => 'world', can => { find => ['array', 'refs'] }
my $hash = $config->data;

# prints 'refs';
print $config->get('can/find')->[1];

SOURCE

https://github.com/Csson/config-fromhash

HOMEPAGE

https://metacpan.org/release/Config-FromHash

AUTHOR

Erik Carlsson <info@code301.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Erik Carlsson <info@code301.com>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.