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 () { 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 read_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;