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.
| [reply] |
|
So is there a sensible explanation for formats or is that just considered an exception?
| [reply] |
|
| [reply] |
|
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"
| [reply] [d/l] |
|
|
|
Newlines are unnecessary, check out the Obfuscation page here!
| [reply] |
|
Perl has no statement terminator; just a statement separator.
| [reply] |
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!
| [reply] [d/l] [select] |
|
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']});
| [reply] [d/l] [select] |
|
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
| [reply] [d/l] |
|
|
|
|
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 | [reply] |
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. | [reply] |
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...
| [reply] |
Re: Strange finish!!
by TOD (Friar) on Nov 07, 2007 at 12:52 UTC
|
| [reply] [d/l] [select] |