Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to use eval?

by iwanthome (Beadle)
on Apr 09, 2004 at 07:21 UTC ( [id://343858]=perlquestion: print w/replies, xml ) Need Help??

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

My test code is like this:

#!/usr/bin/perl $code = 'if($a>$b){'; $code .= '$result = 1;}'; $a =1; $b = 0; $result =0; eval $code; print $@ if ($@); print $result;
There are no information displayed on the screen. What's wrong with the code ?
thanks!

Update

To bmann:

yes,it is right.But I have another question:

#!/usr/bin/perl $code = 'if($port_a $cmp_op $port_b){$result=1;}'; $code .= 'else{$result=0;}'; $port_a =7777; $port_b = 6666; $cmp_op = ">"; eval $code; print $@ if($@); print $result; $perl eval Scalar found where operator expected at (eval 1) line 1, near "$port_a + $cmp_op" (Missing operator before $cmp_op?)

So I can't use eval like that which I must specify operator not scalar,is it true ? Are there any solution to do this ? thanks!

Replies are listed 'Best First'.
Re: How to use eval?
by matija (Priest) on Apr 09, 2004 at 07:30 UTC
    There is nothing wrong with the code.

    When I try it, it prints "1" to the screen, just as it should.

    Now, if you had said print "$result\n"; at the end, that 1 would be on a line of it's own, and more easily visible - which is what I think your problem was.

      $ more eval #!/usr/bin/perl $code = 'if($a>$b){ $result =1} '; $a =1; $b = 0; $result =0; eval $code; print "eval error\n" if($@); print $result; $ eval $ perl -v This is perl, v5.8.2 built for sun4-solaris Copyright 1987-2003, Larry Wall
      You can see what's happened on my computer,there are nothing .
      Which perl version do you use ?Maybe it is a bug?
      thanks!
        eval is a shell builtin. Just rename your script or run it with perl eval.
Re: How to use eval?
by BUU (Prior) on Apr 09, 2004 at 10:26 UTC
    (Addressing the update) You are litterally trying to execute the code if($port_a $cmp_op $port_B) and that isn't valid perl syntax. A quick fix would probably be to just define $port_a,$cmp_op and $port_b before you initialize $code and then just do $code = "if($port_a $cmp_op $port_b)" which will work.

      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)
        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

Re: How to use eval?
by Anonymous Monk on Apr 10, 2004 at 03:42 UTC
    Please don't use updates to ask new questions, you're not carrying on a conversation.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://343858]
Approved by stonecolddevin
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found