This is the problem: an application is split over several modules. However, I want to have a single configuration file (with paths, database data and the like) for the whole application; using one of the excellent Config:: modules and providing the full path to the config file in every module that uses it seems quite unnatural. Is there a simple and standard way to do such a thing?

I currently use a special class that keeps the configuration in a class variable.

Here is the code:

use strict; package Config::Once; use Config::Auto; our $Config; sub init { my ($inv, $config_path) = @_; my $class = ref ($inv) || $inv; my $config_varname = $class . '::Config'; no strict 'refs'; $$config_varname = Config::Auto::parse( $config_path ); return $$config_varname; } sub cfg { my ($inv, $param) = @_; my $class = ref ($inv) || $inv; my $config_varname = $class . '::Config'; no strict 'refs'; return $$config_varname->{$param} if (defined $param); return $$config_varname; } 1;

Then every application subclasses it:

use strict; package MyApp::Config; use base qw/ Config::Once /; 1;

and the modules can do MyApp::Config->cfg('smth').

Is this the right way to do it? Is something like that (or a better solution) available from CPAN?


In reply to Application-wide configuration by akho

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.