Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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

by Athanasius (Chancellor)
on Jan 31, 2013 at 13:18 UTC ( #1016319=note: print w/replies, xml ) Need Help??

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

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


22:54 >perl 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,

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2018-04-20 05:46 GMT
Find Nodes?
    Voting Booth?