in reply to Re: what difference between eval and do ?
in thread what difference between eval and do ?

Understood! eval is actually a anonymous sub call, but do is just a block! Thank you very much ikegami!
  • Comment on Re^2: what difference between eval and do ?

Replies are listed 'Best First'.
Re^3: what difference between eval and do ?
by locked_user sundialsvc4 (Abbot) on Apr 19, 2018 at 13:12 UTC
    eval is also Perl's mechanism for exception-trapping.

      I didn't take the title literally. The OP appears to asking why do BLOCK and eval BLOCK behave differently in the provided code. I did not set to list all the difference between eval BLOCK and do BLOCK, at least one of which hasn't been mentioned yet (do BLOCK's interaction with the while statement modifier).

        But your code already perfectly show the difference and the underlining reason between eval and do: they both return the value of the last statement in the block, eval is sub-call, so it copy the last value and return it; and do is a bit like a named block. that's it.