Hopefully a less hackish (safer) solution than what has been offered so far. We use a hash called %vars to contain your variables because it's simply a good idea.
use Data::Dumper; my %vars; # set initial variables $vars{'INST'} = 'C'; open my $fh, "<", 'file' or die $!; while (<$fh>) { chomp; m/^(\w+)=(.*)$/ or next; my ($var, $value) = ($1, $2); $value =~ s/\$([a-z]+)/ $vars{$1} /gie; $vars{$var} = $value; } print Dumper(\%vars);
# source file: VAR1=$INST/dirivebase/program VAR2=$INST/origbase/data # output: $VAR1 = { 'VAR1' => 'C/dirivebase/program', 'INST' => 'C', 'VAR2' => 'C/origbase/data' };
You might want to adjust the regexp to be more lenient towards spacing, though...
In reply to Re: how to read perl variable from a line read from a file
by Anonymous Monk
in thread how to read perl variable from a line read from a file
by Bharath666
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |