Many, many moons ago (this version is from 2006, having evolved from around 2001) I had reason to deal with this problem and this was what I wound up with after a while. It's pretty ugly by today's standards, but I think the principles might still be informative. (Although this version read its config values from a database, the original stored them in a hash.) Of particular interest is how the accessors for each config param are generated on the fly.

On a side note, I was toying with inside-out objects for this since I didn't want people messing with config params other than by setting them via the UI.

package Nortx::Config; use strict; =head1 NAME Nortx::Config - Nortx package configuration =head1 SYSNOPSYS use Nortx::Config; my $cfg = new Nortx::Config; my $plist = $cfg->params; my @plist = $cfg->params; my $base = $cfg->basedir; =head1 DECRIPTION This module provides a standard location to store constant data such a +s directory names, passwords and such. Unlike other objects in the Nortx system, this one is implemented as a reference to an anonymous scalar. None of the values returned by the accessors are stored in the object itself. Instead, the values ar +e stored in hashes private to the module itself. All accessors are READ-ONLY. Use the methods below to get a list of available accessors or read the source for more details. =cut use Carp; use Nortx::Db; # Start package block { my $pkg = __PACKAGE__; my $stdcfg = {}; sub _load { my $dbh = Nortx::Db->connect; # Get the values from the database my $dbparams = $dbh->selectall_hashref('select * from ntcadmin', 'param'); # Now make a local copy so we can release the DBI ram. foreach my $pname (keys(%{$dbparams})) { foreach (keys(%{$dbparams->{$pname}})) { $stdcfg->{$pname}->{$_} = $dbparams->{$pname}->{$_}; } } # and gen the accessors foreach my $attr (keys(%{$stdcfg})) { next if $pkg->can($attr); no strict 'refs'; *{"$pkg\::$attr"} = sub { my $self = shift; return $stdcfg->{$attr}->{value}; }; } } =head1 METHODS =head2 new Returns a reference to a C<Nortx::Config> object. =cut sub new { my $proto = shift; # we really don't care what the proto is in this case, but we' +ll keep it # handy if we want to do some defensive programming later. $pkg->_load; return (bless *foo{SCALAR}, $pkg); } =head2 params Can be called as either a CLass or Instance Method. Returns a list of available standard configuration variable names. # get list as an array my @plist = $cfg->params; # get list as a reference to an array my $plist = $cfg->params; =cut sub params { my $self = shift; my @pnames = keys(%{$stdcfg}); return (wantarray ? @pnames : \@pnames); } sub get_param { my $self = shift; my $pname = shift; croak("Must supply a param name to get\n") unless $pname; my $retval; eval { my $accessor = $pname; $retval = $self->$pname; }; if ($@) { croak($@); } return ($retval); } }; # end package block 1; __END__
It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

In reply to Re: Clean way to export all constants at once by boftx
in thread Clean way to export all constants at once by perl_help26

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.