in reply to Re: Reading Variables from a File
in thread Reading Variables from a File
I would submit that it is often better to read all the configuration information at once. Here's a snippet that uses this philosophy:
use strict; my(%config) = ("variable1" => "default1", "variable2" => "default2"); &get_settings("myconfig",\%config); sub get_settings{ my($filename,$config_hash) = @_; my($var,$val); return unless ref $config_hash eq "HASH"; open(CFGFILE,"<$filename") or die "Cannot open config file $filename: $!"; while (<CFGFILE>){ chomp; next if /^#/; ($var,$val) = split(/=/,$_,2); $config_hash->{$var} = $val if exists $config_hash->{$var}; } close(CFGFILE); }
Of course, there's always Config::General, but it's probably overkill and you may not be able to count on it being there for you.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Answer: Reading Variables from a File
by #include (Curate) on Jun 02, 2003 at 09:00 UTC |
In Section
Seekers of Perl Wisdom