Config Loader

Recent site activity

Translating dir/file names to keys

Consider the following tree:

    config/
      db/
        main/
          creds_production.yml
          creds_development.yml

When we are running in production, we want to load the creds_production file. When on a development machine, we want to run load the  creds_development file.

Whichever file we load, we want it to be inserted into the config tree with the key 'creds'. This assumes we're using

    load_level => 'path'.

So, we could say:

    my $role = $ENV{ROLE} || 'development';

   
my $load_if = $role eq 'development'
        ? qr/_development$/
        : qr/_production$/;

   
my $load_unless = $role eq 'development'
        ? qr/_production$/
        : qr/_development$/;

    my $c = Config::Loader->new({
        path         => '/path/to/config',
        load_if      => $load_if,
        load_unless  => $load_unless
    });


But that doesn't help us with the naming of the key - it would be loaded as 'creds_production' or 'creds_development'.

So we have two choices:
  • provide another parameter, eg. load_filename_as
  • use the return value of load_if* as the name to use
The first is more explicit, but requires regexes to be repeated. The second is slightly less obvious, but more compact.

So we would rewrite the above as:


    my $role = $ENV{ROLE} || 'development';

   
my $load_if = $role eq 'development'
        ? qr/(.+)_development$/
        : qr/(.+)_production$/;

   
my $load_unless = $role eq 'development'
        ? qr/_production$/
        : qr/_development$/;

    my $c = Config::Loader->new({
        path         => '/path/to/config',
        load_if      => $load_if,
        load_unless  => $load_unless
    });


The same issue does not apply to load_unless as, obviously, we're not going to load the file.

We could use the same mechanism for merge_if.

Checking for "false" / do_not_load


If we're not just expecting a boolean return value from load_if, then a false value may be an allowable 'true' value.  We could check for defined, but probably a more 'friendly' solution would be just to check for a positive length,