Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Uer Perl Variable in txt file

by rovf (Priest)
on Aug 06, 2012 at 14:52 UTC ( #985743=note: print w/ replies, xml ) Need Help??


in reply to Uer Perl Variable in txt file

Although choroba already warned against using eval - and he is certainly right with it -, you can consider an eval-solution, if you understand the risks (and, in particular, if you have control over what goes into your file).

In this case, you could do:

use strict; use warnings; while(my $line=<FH>) { print(eval "qq($line)"); }
This solution is much simpler than doing a substitution manually, but never forget: Whoever creates file.txt, can get *any* code being executed by your program; it requires that every variable referenced in the file, exists in the scope of evaluation (and will be replaced); and finally, that, if file.txt contains parentheses, that they are balanced.

-- 
Ronald Fischer <ynnor@mm.st>


Comment on Re: Uer Perl Variable in txt file
Download Code
Re^2: Uer Perl Variable in txt file
by Anonymous Monk on Aug 06, 2012 at 20:06 UTC
    eval stinks in such an application, as of course well you know.   It would only "work perfectly" in a perfect situation, and who knows what it would do at all other times.   Unacceptable.
      eval stinks in such an application
      Admittedly, it at least smells funny. However, I personally don't like to tell people in those situations that it is unacceptable in an absolute sense, because we don't know what's their application is, and what risk they are willing to take (in exchange for easier programming). That's why I outlined every risk I was aware off. With other words: Instead of saying: "Don't shoot yourself in the head", I prefer saying: "You can shoot yourself in the head, but be aware that there will be a hole afterwards". ;-)


      Having said this, I must admit that I would use such an eval solution for work I only use myself (not paid work for the customer), knowing well that I can't blame anyone but myself if I get bitten....
      -- 
      Ronald Fischer <ynnor@mm.st>
Re^2: Uer Perl Variable in txt file
by vinaybond (Novice) on Aug 07, 2012 at 06:13 UTC
    Thank you very much. It solved my problem. I can use eval without worry as i am the one who is creating the file. -Vinay

      Thank you very much. It solved my problem. I can use eval without worry as i am the one who is creating the file. -Vinay

      And you never make mistakes? :)

      I would worry about making mistakes, maybe not today, but a week from now, so I would avoid string eval unless this was a single-use-program, only to be used once

      Otherwise I would use String::Interpolate or String::Interpolate::RE or another similar abstraction instead

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-07-13 05:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (247 votes), past polls