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

Dear Monks,

I was wonder why do I need to locate semicolon at the end of eval function ?

For instance: ...
eval
{
...
};

TIA, Mosh

Replies are listed 'Best First'.
Re: eval with semicolon
by Tanktalus (Canon) on Oct 02, 2005 at 15:20 UTC

    You don't. Well, not any more than you need it after, say, $x++.

    eval is not a flow-control construct (like "if" or "unless" or "foreach", etc.), but a function. A function that takes a block. Like any function that takes parameters, you must separate it from the next command with a semicolon. If it's the last statement in a block, perl makes that semicolon optional.

Re: eval with semicolon
by techcode (Hermit) on Oct 02, 2005 at 15:24 UTC
    Because "eval" is function - you could write it as :
    eval ({ ... });
    But as with many more Perl functions parenthesis () are optional. Other way of calling eval is with string as parameter.

    PS. You should use <code> ... </code> for writing perl code ...


    Have you tried freelancing? Check out Scriptlance - I work there.
Re: eval with semicolon
by xdg (Monsignor) on Oct 02, 2005 at 15:24 UTC

    Because it's a simple statement like any other, not a special compiler directive compound statement that introduces a block. You need to put a semicolon after it whenever you'd need to put a semicolon after any simple statement. See perlsyn:

    Simple Statements The only kind of simple statement is an expression evaluated for its side effects. Every simple statement must be terminated with a semicolon, unless it is the final statement in a block, in which cas +e the semicolon is optional. (A semicolon is still encouraged if the b +lock takes up more than one line, because you may eventually add another line.) Note that there are some operators like "eval {}" and "do {}" that look like compound statements, but aren't (they're just TERMs i +n an expression), and thus need an explicit termination if used as the la +st item in a statement.

    Update: Fixed up terminology as per replies, below

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      "not a special compiler directive"

      "preprocessing directive", or as you said "compiler directive", has its own specific meaning, and things like if, for, while etc. are not preprocessing directives. They are really flow control statement.

      They can often be further classified:

      • selection: for example, if;
      • iteration: for example, for;
      • loop control: for example, last;
      • jumping: for example: goto.

        Good point. Sloppy terminology on my part -- I hadn't meant pre-processing. From perlsyn, a "compound statement that introduces a block" is a better description of what eval is not.

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: eval with semicolon
by davidrw (Prior) on Oct 02, 2005 at 15:34 UTC
    For the same reason that you need a semicolon after the end of any other perl statement (e.g. my @array = split /foo/, $string;). eval (despite what it can do) is just a function, and is applied the same as any other. Also note that perldoc -f eval explicitly states that:
    "eval BLOCK" does *not* count as a loop, so the loop control statements "next", "last", or "redo" cannot be used to leave or restart the block.
    Which emphasizes that eval is just a function and not a condition or looping construct that does not need the semicolon at the end.
    Also note that eval can be embedded (just like any function) in a statement as well, just as if($x == eval $y){ ... }.
Re: eval with semicolon
by tomazos (Deacon) on Oct 02, 2005 at 21:19 UTC
    For historical reasons, to make it easier for perl to parse your Perl, eval is classified as an expression.

    As an expression it needs to be seperated from the next expression so that perl doesn't get confused.

    The advantage of having it as an expression is that it can have a return value. (In this case, the result of the evaled code)

    By adding a ; to the end of an expression it turns it into a statement.

    The ; is not added automatically because this would make the job of parsing harder for perl. Different languages have different approaches to dealing with this, but as a member of the C family - Perl requires certain statements to be delimited explictly with a ;.

    -Andrew.


    Andrew Tomazos  |  andrew@tomazos.com  |  www.tomazos.com
Re: eval with semicolon
by holli (Abbot) on Oct 02, 2005 at 15:21 UTC
    Otherwise it's a syntax error. =[


    holli, /regexed monk/
Re: eval with semicolon
by nothingmuch (Priest) on Oct 03, 2005 at 07:11 UTC