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

Hello monks,

I thought I understood eval, but... I may have been mistaken. I am trying to use eval with DBI, and I ran into an error with statements like:

eval {$sth->execute($value) } if ($@) { print "Error\n"; }

And in fact, I was able to extract this down to the ridiculously simple case:

eval {1} if (1) {}

Something like this can't be a bug, I figure, so it must be my understanding of eval is mistaken. Can anybody shed some light on what's going on?

Incidentally, the error message itself isn't too helpful:

syntax error at testseqio.pl line 2, near ") {"

Replies are listed 'Best First'.
Re: Syntax error with if following eval
by liz (Monsignor) on Sep 08, 2003 at 21:14 UTC
    eval {1}; if (1) {}

    Note the semi-colon!

    Liz

      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