Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Answer: Reading Variables from a File

by Anonymous Monk
on Jun 01, 2003 at 01:19 UTC ( #262156=note: print w/replies, xml ) Need Help??

in reply to Answer: Reading Variables from a File
in thread input and output

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
    Part of the reason why I like to grab settings "on the fly" is because you can then change those settings at runtime. By grabbing the setting only when I need it, I can tweak the configuration file while the script is running. For settings that can't be changed while running, I load them in at the beginning of the script and store them in scalars.

    A genius writes code an idiot can understand, while an idiot writes code the compiler can't understand.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://262156]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2018-05-26 11:10 GMT
Find Nodes?
    Voting Booth?