Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help:Why does eval add a 1 behind evaluations?

Reply
Thread Tools

Help:Why does eval add a 1 behind evaluations?

 
 
Tay Ray Chuan
Guest
Posts: n/a
 
      08-28-2003
Hi, I tried using eval like this:

print "Enter something to evaluate:\n";
while (defined($s = <>)) {
$result = eval $s;
if ($@) {
print "Invalid string:\n $s";
} else {
print "$result\n";
}
}

this can evaluate anything. but when I type this:

"$a=5;$b=5;$c=$a+$b;print $c;", it prints "101".

Why is there an extra "1"?
 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      08-28-2003
Tay Ray Chuan wrote:
> print "Enter something to evaluate:\n";
> while (defined($s = <>)) {
> $result = eval $s;
> if ($@) {
> print "Invalid string:\n $s";
> } else {
> print "$result\n";
> }
> }
>
> this can evaluate anything. but when I type this:
> "$a=5;$b=5;$c=$a+$b;print $c;", it prints "101".
>
> Why is there an extra "1"?


You really should have read 'perldoc -f eval';

"[...] In both forms, the value returned is the value of the
last expression evaluated inside the mini-program [...]"

In other words: 'print $c;' returns 1.


--
Tore Aursand <>

 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      09-03-2003
In article < >,
Tay Ray Chuan <> wrote:
>Hi, I tried using eval like this:
>
>print "Enter something to evaluate:\n";
>while (defined($s = <>)) {
> $result = eval $s;
> if ($@) {
> print "Invalid string:\n $s";
> } else {
> print "$result\n";
> }
>}
>
>this can evaluate anything. but when I type this:
>
>"$a=5;$b=5;$c=$a+$b;print $c;", it prints "101".
>
>Why is there an extra "1"?


Change
print "$result\n";
to
print "The result from eval() is $result\n";
and it will show what is really happening.
-Joe

--
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-03-2003
Tay Ray Chuan <> wrote:
> Hi, I tried using eval like this:
>
> print "Enter something to evaluate:\n";
> while (defined($s = <>)) {
> $result = eval $s;
> if ($@) {
> print "Invalid string:\n $s";
> } else {
> print "$result\n";
> }
> }
>
> this can evaluate anything. but when I type this:
>
> "$a=5;$b=5;$c=$a+$b;print $c;", it prints "101".
>
> Why is there an extra "1"?



The last expression eval()uated is the value of the eval(), so
$result gets print()'s return value (1).

So, the

print $c

outputs the 10

then

print "$result\n";

outputs the 1 and a newline.



--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to eval or not to eval? Marc Girod Perl Misc 2 04-19-2011 01:13 PM
eval('07') works, eval('08') fails, why? Alex van der Spek Python 6 01-08-2009 08:24 PM
Different behavior between eval "07" and eval "08" Liang Wang Perl Misc 8 02-02-2008 08:31 PM
DataBinder.Eval and Eval. craigkenisston@hotmail.com ASP .Net 1 06-16-2006 05:33 PM
DataBinder.Eval for an object's property property... like Eval(Container.DataItem,"Version.Major") Eric Newton ASP .Net 3 04-04-2005 10:11 PM



Advertisments