Help:Why does eval add a 1 behind evaluations?

Discussion in 'Perl Misc' started by Tay Ray Chuan, Aug 28, 2003.

  1. 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"?
    Tay Ray Chuan, Aug 28, 2003
    #1
    1. Advertising

  2. Tay Ray Chuan

    Tore Aursand Guest

    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 <>
    Tore Aursand, Aug 28, 2003
    #2
    1. Advertising

  3. Tay Ray Chuan

    Joe Smith Guest

    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.
    Joe Smith, Sep 3, 2003
    #3
  4. 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
    Tad McClellan, Sep 3, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JS#
    Replies:
    5
    Views:
    3,530
    Roedy Green
    Nov 21, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,804
    Smokey Grindel
    Dec 2, 2006
  3. Josh Dukes
    Replies:
    0
    Views:
    292
    Josh Dukes
    Feb 10, 2009
  4. Josh Dukes
    Replies:
    3
    Views:
    320
    Josh Dukes
    Feb 10, 2009
  5. nil in evaluations

    , Apr 18, 2007, in forum: Ruby
    Replies:
    4
    Views:
    141
    Robert Dober
    Apr 18, 2007
Loading...

Share This Page