Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by Athanasius (Monsignor)
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

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,


Comment on Re: how to read perl variable from a line read from a file
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016319]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2014-08-30 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls