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

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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2016-10-24 03:15 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (302 votes). Check out past polls.