I recently found and started to use App::Config and have been suitably impressed by its power and flexibility in integrating command line and configuration file parameters. There was one little urksome feature I found however - the requirement to define configuration parameters one line at a time. eg.

my $cfg = App::Config->new({ 'CASE' => 0 }); $cfg->define("effective_group", { 'DEFAULT' => "nogroup" }); $cfg->define("effective_user", { 'DEFAULT' => "nobody" }); $cfg->define("http_port", { 'DEFAULT' => "localhost:8080", 'CMDARG' => [ "-p" ], 'ARGCOUNT' => 1 });

I ideally wanted a different model of behaviour which would allow me to pass all configuration definitions in a single hash for both clarity and ease of maintainability. The result was the small 'local' module which I wrote below - It is certainly not worthy of CPAN or alike, but others may find it useful. The methods define, get, load (equivalent of cfg_file from App::Config) and set are included in this module.

The configuration definition code snippet from above can be rewritten as thus:

my $cfg = Local::Config->new({ 'CASE' => 0 }, { 'effective_group' => { 'DEFAULT' => "nogroup" }, 'effective_user' => { 'DEFAULT' => "nobody" }, 'http_port' => { 'DEFAULT' => "localhost:8080", 'CMDARG' => [ "-p" ], 'ARGCOUNT' => 1 } });
package Local::Config; use strict; use vars qw/$VERSION @ISA @EXPORT @EXPORT_OK/; require Exporter; require AutoLoader; @ISA = qw/Exporter AutoLoader/; @EXPORT_OK = qw//; $VERSION = '0.24'; use App::Config; sub define { my $self = shift; return undef unless defined $self->{config}; my $arg = @_; if (ref($arg) eq 'HASH') { $self->{config}->define($_, ${$arg}{$_}) foreach keys %{$arg}; }; return; }; sub get { my $self = shift; return $self->{config}->get(@_); }; sub load ($) { my $self = shift; return undef unless -e $_[0]; return $self->{config}->cfg_file($_[0]); }; sub new { my $param = shift; my $class = ref($param) || $param; my $self = {}; bless ($self, $class); my $args = (ref($_[0]) eq 'HASH') ? $_[0] : {}; $self->{config} = App::Config->new(\%{$args}); if (ref($_[1]) eq 'HASH') { $self->{config}->define($_, ${$_[1]}{$_}) foreach keys %{$_[1] +}; }; return $self; }; sub set { my $self = shift; return $self->{config}->set(@_); }; 1; __END__