Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

how to read perl variable from a line read from a file

by Bharath666 (Novice)
on Jan 31, 2013 at 12:25 UTC ( [id://1016308]=perlquestion: print w/replies, xml ) Need Help??

Bharath666 has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am writing a perl script in which I have a variable name say $INST whose value is set to "C". Then I opened a file for reading. This file contains the above variable entries(ex VAR=$INST/dirivebase/program). Now I tried to read this line from the file and assign the value after '=' symbol to another variable,here I am not getting $INST value(i.e "C" which is declared previously in the script),but instead $INST is observed to be assigned as it is. Please tell me how can I assign the $INST value after reading from the file

  • Comment on how to read perl variable from a line read from a file

Replies are listed 'Best First'.
Re: how to read perl variable from a line read from a file
by marto (Cardinal) on Jan 31, 2013 at 12:30 UTC
Re: how to read perl variable from a line read from a file
by Athanasius (Archbishop) on Jan 31, 2013 at 13:18 UTC

    Hello Bharath666, and welcome to the Monastery!

    As parv says, one approach is to use eval:

    #! perl use Modern::Perl; my ($VAR1, $VAR2); my $INST = 'C'; while (<DATA>) { chomp; if (/ ^ ([^=]*?) = (.*) $ /x) { my ($lhs, $rhs) = ($1, $2); eval '$' . $lhs . ' = "' . $rhs . '";'; warn $@ if $@; } } say for $VAR1, $VAR2; __DATA__ VAR1=$INST/dirivebase/program VAR2=$INST/origbase/data

    Output:

    22:54 >perl 513_SoPW.pl C/dirivebase/program C/origbase/data 23:07 >

    The problem with this ‘solution’ is that the code is tightly-bound to the exact form of the input data, making it brittle and inflexible. I have to say, you will be better-off re-examining the broader problems your code is addressing, and then re-thinking your general strategy.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: how to read perl variable from a line read from a file
by Anonymous Monk on Jan 31, 2013 at 15:17 UTC

    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...

      Thanks!! Its working now....

        It's not perfect. It's missing a close $fh and the substitution regexp probably should be s/\$([a-z]+)/ $vars{$1} || "\$$1" /gie; to give a somewhat sane behaviour for undefined variables (instead of substituting them with the empty string. Which is what *nix shells do, so I guess it's not that bad...)

Re: how to read perl variable from a line read from a file
by parv (Parson) on Jan 31, 2013 at 13:05 UTC
    The value is being read as literal string. You could try any of s///ee, eval "", some sort of -- overkill in this case -- template facility on the string read.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1016308]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-19 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found