Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Program configuration in the OOP world

by Tardis (Pilgrim)
on Apr 14, 2002 at 12:34 UTC ( [id://158919]=note: print w/replies, xml ) Need Help??


in reply to Program configuration in the OOP world

OK, I did it. Rather than my usual rant, lets post some bona-fide code. This is really rough, a lot of it is cut and paste from my other stuff, but at this point it's working. It may not work for you, as I've diked out a lot of the really ugly stuff before pressing submit ;-) Consider this untested.

package Config; use strict; # always be strict use Carp; use vars qw ($AUTOLOAD); my ($config); # our global config object my (%config); # config hash, the actual config +data sub new { my $proto = shift; my $class = ref($proto) || $proto; # setup the new object my $self = { %config, }; bless ($self, $class); return ($self); } sub AUTOLOAD { my $self = $config; my $type = ref($self); my $name = $AUTOLOAD; $name =~ s/.*://; # strip fully-qualified portion unless ( defined $self->{$name} ) { croak "Can't access `$name' field in class $type"; } # just return the attribute return $self->{$name}; } sub read_config { my $conf_file = shift; open (FILE, "$conf_file") || return 0; while (<FILE>) { if ( /^(\w+)\s*=?\s*(.*)$/ ) { $config->{$1} = $2; carp "w00t! set $1 to $2"; } } close (FILE); return 1; } # package constructor BEGIN { my (@config_files); $config = Config->new; # we have an empty configuration object, lets read in # a config file # if we fail, fail silently, give the user code a chance to call rea +d_config # with it's own specifier @config_files = qw ( ../etc/journal.conf /usr/local/etc/journal.conf + ); # try each one until we succeed foreach (@config_files) { if (read_config ($_)) { last; } } # error check if (! $config ) { carp "Failed to get config"; } } # keep stuff happy 1;
With a config file like this:

rootdir = /usr/tmp
My code can do this:

use Config; print "Root Directory is " . Config->rootdir . "\n";
All of my modules can use config;, the config data will only be read once.

The other bit of my criteria, allowing for a user override of the config file I haven't yet nutted out. I have a feeling it's going to cause me problems, given that some of my modules need config data during their class constructers. I'll keep on that.

Replies are listed 'Best First'.
Re: Re: Program configuration in the OOP world
by IlyaM (Parson) on May 29, 2002 at 19:31 UTC
    May be it is late reply but anyway. Do not name your module Config. Perl distro already has core module Config. By giving your module same name you just asking for troubles. It is almost as bad as if you created your own perl module named lib.

    --
    Ilya Martynov (http://martynov.org/)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://158919]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-20 11:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found