Cold Hard Code

Moose Goodness.

A lot of times I come across something that is really clever and fantastic (sometimes I even come up with it, but not usually), and most of the time it has to do with Moose.  

Today I solved a silly problem, that was spawned by the MooseX::SimpleConfig role being so clever and fantastic.

I have a list of locations that I want to search for configuration files, and was mucking with them and then came up with this wonderful construct:

package MyDist::App;

use Moose;

use FindBin;

with 'MooseX::SimpleConfig';
with 'MooseX::Getopt';

has +configfile => (
    default => ( grep { defined $_ and -f $_ } (
            $ENV{APP_CONFIG},
            "$FindBin::Bin/../conf/config.yml",
            "$FindBin::Bin/conf/config.yml",
            "$FindBin::Bin/config.yml",
    ) )[0] || ''
);

So, it just descends the list at runtime and picks the proper config file out and away I go.  I am very happy that this works so well.  It also merges in well with any command-line options.




jshirley

Written by Jay Shirley

Jay Shirley combines technical fundamentals with modern, practical savvy. An open source veteran with plenty of notches in his personal and professional belt, the combination of his work and his field vision (soccer metaphor!) has few rivals.

Comments