Config::Station - Load configs from files and the environment
version 0.002001
Define your config class:
package MyApp::Config; use Moo; has www_port => ( is => 'ro', required => 1, ); has static_path => ( is => 'ro', default => 'view/static', ); 1;
And elsewhere you load it up:
my $station = Config::Station->new( config_class => 'MyApp::Config', env_key => 'MYAPP', location => '.config.json', ); my $config = $station->load;
This config loader offers a couple of major features that make it compelling for the user:
This is a huge deal. This means that you can trivially set defaults, add validation, and an other number of cool things. On top of that this means that unless you do something silly, your configuration has clearly defined fields, instead of being a shapeless hash.
Presumably many users of this module will be loading their config from a file. That's fine and normal, but baked into the module is and an environment based config solution. This allows the user to change, for example, a port, by just running the application as follows:
MYAPP_WWW_PORT=8080 perl bin/myapp.pl
my $station = Config::Station->new( env_key => 'MYAPP' )
The env_key is a required attribute which affects everything about this module.
env_key
env_key affects two classes of values:
These values use the env_key as a suffix, and are documented further down.
These values use the env_key as a prefix for env vars that override configuration keys. To be clear, if you specify an env_key of FOO, an env var of FOO_BAR=BAZ will pass bar => 'BAZ' to the constructor of "config_class".
FOO
FOO_BAR=BAZ
bar => 'BAZ'
The config_class is a required attribute which determines the class that will be used when loading the configuration. The config class absolutely must have a new method which takes a hash. What it returns is up to you.
config_class
new
If you care to, you can define a serialize method on the object which supports the "store" method, but I suspect that is likely not a typical use case.
serialize
Debugging is critical feature of this module. If you set this attribute directly, or indirectly by setting the env var 'DEBUG_' . $env_key, you will get some handy debugging output warned. It looks like this:
'DEBUG_' . $env_key
warn
CONFIGSTATION FROM FILE: name: herp CONFIGSTATION FROM ENV: id: 1 name: wins
If the file can't be loaded or parsed, for some reason, instead of listing key-value pairs, the output for the file will be:
CONFIGSTATION FROM FILE: $exception
Note that failure to load or deserialize the file is not considered an error. If you want to enforce that data is set do that by making your object constructor more strict.
The location can be set directly, or indirectly by setting the env var 'FILE_' . $env_key. As noted above, it is neither required to be set or parseable at all.
'FILE_' . $env_key
my $station = Config::Station->new( ..., decode_via => sub { \&YAML::Load );
A code reference which can inflate a string into a hash reference. Default uses JSON.
my $station = Config::Station->new( ..., encode_via => sub { \&YAML::Dump );
A code reference which can deflate a hash reference into a string. Default uses JSON.
Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
This software is copyright (c) 2015 by Arthur Axel "fREW" Schmidt.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Config::Station, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Config::Station
CPAN shell
perl -MCPAN -e shell install Config::Station
For more information on module installation, please visit the detailed CPAN module installation guide.