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__
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |