in reply to Syntax error with if following eval

eval {1}; if (1) {}

Note the semi-colon!

Liz

Replies are listed 'Best First'.
Re: Re: Syntax error with if following eval
by Anonymous Monk on Sep 08, 2003 at 21:28 UTC

    Ahhh, I see: thank you. ~sneaks off to hide shame~

      Actually, I have been looking up why what you did was an error.

      If you think about a string eval, the problem would have been clearer, I think:

      eval '1' if (1) {}

      but if you would write the block eval as:

      eval { 1 } if (1) { }

      then it becomes very confusing and unclear why that would be a problem. I guess it's just one of the Perl's syntaxical quirks that you need to get used to.

      Liz

        For what it's worth, I believe that the language design for Perl 6 specifies that the trailing semicolon after an eval block will become optional.
        I think the problem is that the if is seen as a modifier to the eval statement, after which point the {} mucks up the parsing.

        You'd get the same error from: print "hahaha\n" if (1) {};

        So, in the eval case, perl thinks you want: eval { 1 } if ($@); but finds an extra block.

        Thank you: I was trying to figure out why it was wrong myself. It didn't seem so very wrong, because I always associate {} with "code blocks" that don't require trailing ;'s. When you put it in terms of the string eval that makes it a lot clearer.