Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Executing a string as a Perl command

by johnpeterdinesh (Initiate)
on May 10, 2011 at 09:22 UTC ( [id://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;

Replies are listed 'Best First'.
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 (Archbishop) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://903932]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-20 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found