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__

In reply to Local::Config - an extension to App::Config by rob_au

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.