Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Strange finish!!

by narainhere (Monk)
on Nov 07, 2007 at 07:16 UTC ( [id://649417]=perlquestion: print w/replies, xml ) Need Help??

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

I was reviewing an old code written years back, and I was just about the blame the Author for syntax errors until I executed it for myself.Here is the replica
use strict; use warnings; print "Before check\n"; if (1) { die("Abnormal quit") } print "Normal finish\n";
The above code executes(note the missing semicolon at the end of die() statement) successfully.What I deduce from this is that the last statement (Technically die() is the last statement in the above code) in the script need not end with a semicolon.
Here is a straight forward example to bolster what I am saying
use strict; use warnings; $_=12; chomp $_
is my conviction that "Last statement in script necessarily doesn't have to end with a semicolon" right????Or am I missing something??

The world is so big for any individual to conquer

Replies are listed 'Best First'.
Re: Strange finish!!
by duff (Parson) on Nov 07, 2007 at 07:27 UTC

    In Perl, semicolons are statement separators, not statement terminators. Since there is no statement after the last one in a file, a semicolon is not necessary. Same thing for the last statement in a block. Which is why your die doesn't need one. Add a statement after your die with no semicolon in between and perl will give you an error.

      So is there a sensible explanation for formats or is that just considered an exception?
      But duff what's the statement terminator in perl "Newline" is it???

      The world is so big for any individual to conquer

        Following on what duff said, the "statement terminator" is either a semi-colon, end-of-block, or end-of-file. The semi-colon is actually a "statement separator", so when you want two or more statements in a perl script or block (which is just about always), you need to put semicolons between them.

        Spaces, tabs and newlines (when they are not enclosed in any sort of quotation marks) are only useful as token separators (they are often "optional" in this role). Of course, they also serve as aids to human readability, but they only have a functional syntactic role when they are required as token separators (e.g. you must have whitespace between "use" and the name of a module or pragma), and in those cases, spaces, tabs and newlines are interchangeable -- perl doesn't care what kind of whitespace you provide, so long as you put it where it's needed:

        #!/usr/bin/perl # (shebang line syntax is dictated by unix shell) use strict;use warnings;my @a = qw (1 2 3);print "@a\n"
        Newlines are unnecessary, check out the Obfuscation page here!
        Perl has no statement terminator; just a statement separator.
Re: Strange finish!!
by cdarke (Prior) on Nov 07, 2007 at 09:29 UTC
    I think most people would agree that omitting the semi-colon at the end of a block causes grief. Just because you can does not mean you should. For example:
    if ($x == 42) { print "I have the answer\n" }
    Then later add a line:
    if ($x == 42) { print "I have the answer\n" print "to Life, the Universe, and Everything\n"; }
    and it fails with a syntax error. Doh!
      One should also make it a habit to leave a comma in hash definitions.
      %fortytwo = ( "I have" => "the answer\n", );
      You don't have to take care of fixing the previous line, when adding a new one.
      %fortytwo = ( "I have" => "the answer\n", "to" => "Life, the Universe, and Everything\n", );

      print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});
        One should also make it a habit to leave a comma in hash definitions...
        And then you get bitten by exactly that habit when dealing with JavaScript:
        var foo = {a:'A', b:'B',}; //syntax error


        holli, /regexed monk/
Re: Strange finish!!
by lupey (Monk) on Nov 07, 2007 at 22:09 UTC
    I think it is important to point out that the answer to this question is simply found in perlsyn. Here is a snippet from 'perlsyn':

    Every simple statement must be terminated with a semicolon, unless it is the final statement in a block, in which case the semicolon is optional...Note that there are some operators like eval {} and do {} that look like compound statements, but aren't (they're just TERMs in an expression), and thus need an explicit termination if used as the last item in a statement.

    lupey

Re: Strange finish!!
by girarde (Hermit) on Nov 07, 2007 at 14:45 UTC
    Perl requires that statements be separated. Statements do not require termination; they only require separation from following statements, either though a semicolon, or the end of the block.

    No following statement, no need for a separator.

Re: Strange finish!!
by blazar (Canon) on Nov 08, 2007 at 16:22 UTC
    The above code executes(note the missing semicolon at the end of die() statement) successfully.What I deduce from this is that the last statement (Technically die() is the last statement in the above code) in the script need not end with a semicolon.

    I personally believe that people already explained to you pretty much everything that needed to be explained. I would only like to underline a severe thinko on your part: your guess that the semicolon could be omitted on a statement because that statement is the last one to be executed is inconsistent. In fact it's a syntactical issue: whether a semicolon is a statement separator or terminator is something that affects the way a program is parsed, and that happens even before the program itself becomes something that can be executed. To do what you say, an interpreter (or compiler) should run a program before parsing it! Or more realistically, but not much, do a simulation in advance: anyway, something very awkward...

Re: Strange finish!!
by TOD (Friar) on Nov 07, 2007 at 12:52 UTC
    1 is - according to perl, which is what we're presumably talking about here - a true value. hence the condition if (1) will always be fulfilled. from this follows that in your code, which is probably worth being ages old, the last line print "Normal finish\n"; will never be executed.
    --------------------------------
    masses are the opiate for religion.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://649417]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-23 12:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found