now you can either slurp the contents into an array or a scalar, or iterate through the file one line at a time. Small files can use the former technique without a hitch, but the latter technique is better for huge files. Since you are splitting on new lines - let's use a scalar:open(FILE,'../env.conf') or die "not there";
Next, don't use %ENV - use your own hash, how aboutmy $content = do {local $/; <FILE>};
but that doesn't quite work - if it reads '=bar' then you will have an undefined key that points at the value 'bar'. map is fine and dandy for data transformation, but as soon as the logic inside the code block gets tricky, i like to find another way. Let's try a while loop on an open file handle:my %conf = map { /^([^=]+)=(.*)$/; $1 => $2 } split /\n/, $content;
Hope this helps!my %conf; while (<FILE>) { chomp; my ($k,$v) = $_ =~ /^([^=]+)=(.*)$/; $conf{$k} = $v if $k; }
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)
In reply to (jeffa) Re: How to assign a variable with a value read from a config file
by jeffa
in thread How to assign a variable with a value read from a config file
by tariqahsan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |