in reply to Re: How to use eval?
in thread How to use eval?

It didn't work for me. Despite a number of variations I always got this error message:

Scalar found where operator expected at (eval 1) line 1, near "$port_a + $cmp_op" (Missing operator before $cmp_op?) Scalar found where operator expected at (eval 1) line 1, near "$cmp_op + $port_b" (Missing operator before $port_b?)

If I understand correctly, the eval is having trouble with the '>' but so far I haven't figured out how to fix it.

xenchu


That's about all there is to it, except for everything else. -<b>Programming Perl</b> (p.346)

Replies are listed 'Best First'.
Re: Re: Re: How to use eval?
by bmann (Priest) on Apr 09, 2004 at 19:19 UTC
    Comment out the line with 'eval', and print it instead

    #eval $code; print $code;

    You might be surprised at the value of $code.

    I'm sure you already know this, but I'll repeat it anyway - double quotes interpolate contained variables, single quotes do not. perl is trying to eval if ($port_a $cmp_op $port_b)... which is destined to fail - it won't even compile. In order to evaluate it properly, $code needs to to contain if ($port_a < $port_b)....

    I'll leave it for you to construct the string properly ;) Once you do, comment the print and uncomment the eval.

    BTW, what are you trying to accomplish? Could there be a better way?

    Update: BTW, I didn't see your update until after posting this node. I assume most would see it easier if you posted a response instead the update.

    Update #2 Update #1 is directed at the OP, not xenchu