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 as 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 are 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 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__