Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

using eval

by Anonymous Monk
on May 25, 2004 at 23:59 UTC ( [id://356417]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Ok, I’m an extreme newb, I don’t use my Perl projects on a server for anyone other then myself and I honestly usually just get by with my "while" and "if" statements

However, I’ve run into a problem that if and while statements would be very messy at.

say I have a variable like $foobar and $foobar holds x^4-3 or even x+3 I’m wondering if there’s a way I can have Perl evaluate this and give me the result... I think it has to do with the eval function but I tried this simple code and it didn’t work...


$question="4+3"; eval {$answer=$question;}; print "$question=$answer";
the result is 4+3=4+3... not exactly what I need...

Any and all help would be greatly appreciated, also I need it to do it using bedmas, I don’t know if it does by default or not

Edit by castaway added code tags

Replies are listed 'Best First'.
Re: using eval
by Somni (Friar) on May 26, 2004 at 02:09 UTC

    The eval you're using, eval { ... }; is called a block eval. It catches exceptions (die statements), it does not evaluate the contents of the variables as Perl code. The eval you want is a string eval, $answer = eval $question;.

    You should also realize that these must be valid Perl expressions, and they use the Perl operators. x^4-3 xors the string "x" with 4, then subtracts 3; you probably meant $x ** 4 - 3, but that requires $x be defined.

    For further details please see eval and perlop. I also have a script that evaluates code entered in on the command line called math.

Re: using eval
by mifflin (Curate) on May 26, 2004 at 00:11 UTC
    this program
    $y = 4; $x = '$z = $y**3+3'; eval $x; print $z;
    will print out 67.
    Is this an example of what you are talking about?
Re: using eval
by eclark (Scribe) on May 26, 2004 at 02:15 UTC

    Without getting into why this is bad, I'll just fix your sample so it works

    $question="4+3"; $answer = eval $question; print "$question=$answer";
Re: using eval
by Trag (Monk) on May 26, 2004 at 01:13 UTC
    I'm not sure how to solve your problem, but = is the assignment operater. You are telling Perl that $answer should be the same as $question. == is the "equal to" operater. Also, your code calls eval but then does nothing with it. You should have $foo = eval {$answer=$question}; print $foo;. BTW, you only need one ; in the second line.


    our @item = reverse (114, 101, 107, 99, 97, 104, 32, 108, 114, 101, 80, 32, 114, 101, 104, 116, 111, 110, 97, 32, 116, 115, 117, + 74); local $my = reverse ")meti@\ ,rhc (pam tnirp";eval $my;
Re: using eval
by sleepingsquirrel (Chaplain) on May 26, 2004 at 15:05 UTC
    $question='$x**4-3'; $s="\$answer=$question"; $x=2; eval $s; print "\$answer=$answer\n";
Re: using eval
by Anonymous Monk on May 28, 2004 at 18:59 UTC
    Try this indeed my $question = "4+3"; eval "\$answer = $question";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://356417]
Approved by DaWolf
Front-paged by DaWolf
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-19 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found