Introducing Class::ConfigHash

A while ago I saw a post by Ovid on boxing hashes for configuration in Dancer. The idea seemed pretty neat, and I found myself doing something similar in some work for Net-A-Porter, so I wrote a generalized implementation (although the underlying mechanism is different). From the synopsis:

my $config = Class::ConfigHash->_new({
   database => {
       user => 'rodion',
       pass => 'bonaparte',
       options => {
           city => 'St Petersburg'
       },
   },
});
 
$config->database->options->city; # St Petersburg
 
# Dies: Can't find 'flags' at [/->database]. Options: [options; pass; user]
$config->database->flags;
 
# Won't die, returns undef
$config->database->flags({ allow_undef => 1 });
 
# Won't die, returns 'foo'
$config->database->flags({ default => 'foo' });
 
# Access the underlying structure
$config->database({ raw => 1 })->{'user'} = 'raskolnikov';

You can see the module itself here: Class::ConfigHash

5 Comments

Similar module: Data::Hive.

It would be cool if you could explain the differences between your new module and Ovid's implementation for Dancer.

Congrats on the module! :)

Peter, thank you for taking the time to explain the differences to me. :)

Leave a comment

About Peter Sergeant

user-pic I blog about Perl.