Use your own configuration file with Dancer

Dancer has a command line option called --environment which allows you to specify the path to an application specific configuration file.

You can put both Dancer specific options and settings related to your own application in this file. For example, you can put:

app_setting: 42

and then use

my $setting = config->{app_setting};

to access the value.

All this is convenient, but sometimes you need to put some of the more transitory settings into their own files, separately from more permanent settings that describe the environment in which the program operates. For example, the multiplier in a public goods experiment is at a higher level of abstraction and is valid for a shorter duration than the port number for the underlying server. Those two simply do not belong in the same file.

Dancer uses Getopt::Long to parse command line options. However, it does not enable the pass_through option which means options unknown to Dancer are flagged as errors.

The solution? Simple. Use Getopt::Long with pass_through before Dancer gets a chance to call it:

use strict; use warnings;
use Getopt::Long;

my $param_file;

    GetOptions("param=s" => \$param_file);

use lib 'lib';

use Config::Std;
use My::Experiment::App;
use Dancer;

load_app 'My::Experiment::App::Dancer';

my %param;

die "Must supply a parameter file using --param\n"
    unless defined $param_file;

read_config $param_file => %param;

This way, experiment specific parameters can be specified generically and independently of the underlying application settings.