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

Re^2: Executing a string as a Perl command

by johnpeterdinesh (Initiate)
on May 10, 2011 at 09:22 UTC ( #903932=note: print w/ replies, xml ) Need Help??


in reply to Re: Executing a string as a Perl command
in thread Executing a string as a Perl command

Please try below code

my $cmd = '$var1 = 5'; eval "$cmd"; print $var1;


Comment on Re^2: Executing a string as a Perl command
Download Code
Re^3: Executing a string as a Perl command
by JavaFan (Canon) on May 10, 2011 at 11:14 UTC
    Why the double quotes? eval $cmd; works fine, no need for an additional stringification.
Re^3: Executing a string as a Perl command
by ww (Bishop) on May 10, 2011 at 12:29 UTC
    1. ...and your point (aside from the fact that your code prints "5") is...?
    2. ... and this is better than

      my $var1 = 5; print $var1;

      how?
      This short illustrative example has $cmd set inline, but in a real program, it could be constructed from other variables, it could be read from a user, it could be read from a file, etc.

      The question was simply how to get it to execute once it was set. The answer is eval.

        eval is the wrong answer since it can run arbitrary code

        If the user gives you some form of # rm -rf * ~ / , a lot of your files get deleted

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2014-11-22 11:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (121 votes), past polls