Does every perl statement has to end with ';'?

P

Peng Yu

It says in the following webpage http://www.comp.leeds.ac.uk/Perl/basic.html
and many other tutorials that every perl statement has to end with
';'. But it seems not to be the case (see the example below). I feel
that ';' in the last statement can be omitted. Could somebody point me
what the rules are?

$ ./semicolon.pl
Hello world!
pengy@morgan:~/test/perl$ cat semicolon.pl
#!/usr/bin/perl

print "Hello world!\n"
 
J

Jens Thoms Toerring

Peng Yu said:
It says in the following webpage http://www.comp.leeds.ac.uk/Perl/basic.html
and many other tutorials that every perl statement has to end with
';'. But it seems not to be the case (see the example below). I feel
that ';' in the last statement can be omitted. Could somebody point me
what the rules are?
$ ./semicolon.pl
Hello world!
pengy@morgan:~/test/perl$ cat semicolon.pl
#!/usr/bin/perl
print "Hello world!\n"

The last line of a block (i.e. also code enclosed in curly braces)
doesn't need a terminating semicolon. On the other hand the website
is a tutorial and in tutorials you often find that not everything
is always spelled out completely at first for pedagogical reasons
- in this case giving a more complex rule at the very start probably
would have just confused some readers with an unimportant detail.

Regards, Jens
 
J

Jürgen Exner

Peng Yu said:
It says in the following webpage http://www.comp.leeds.ac.uk/Perl/basic.html
and many other tutorials that every perl statement has to end with
';'.

Well, that's wrong.
But it seems not to be the case (see the example below). I feel
that ';' in the last statement can be omitted.

In Perl the ';' is a statement separator, not a statement terminator.
Therefore it is only required between two statement. However it is good
style to add it after the concluding statement in a block, too,
basically creating an empty statement, because then it is easier to add
another new final statement later without having to add the ';' in the
line above.
Could somebody point me
what the rules are?

perldoc perlsyn:

Simple Statements
[...] 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. (A semicolon is still encouraged if the
block
takes up more than one line, because you may eventually add another
line.) [...]

jue
 
R

Randal L. Schwartz

Peng> It says in the following webpage http://www.comp.leeds.ac.uk/Perl/basic.html
Peng> and many other tutorials that every perl statement has to end with
Peng> ';'. But it seems not to be the case (see the example below). I feel
Peng> that ';' in the last statement can be omitted. Could somebody point me
Peng> what the rules are?

Yes, semicolons are required at the end of all statements.

However, the last semicolon of a compilation unit (file, block, or eval
string) may be omitted, because it is implied.

I recommend you include the terminating semicolon anyway, unless the beginning
of the compilation unit is also clearly on the same line.

For example:

@a = map { 2 + $_ } @input; # omit semicolon after expression

@a = map {
2 + $_; # include semicolon
} @input;

my $callback = sub { print "$_\n" }; # omit semicolon after print
my $callback = sub {
print "$_\n"; # include semicolon
};

sub foo {
print "hi ";
my $x = 2 + shift;
print "$x\n"; # semicolon here, since scope started 2 lines ago
} # a subroutine definition is not a statement, so no semicolon here

perl -e 'print "hello, no semicolon here\n"'

print "Just another Perl hacker,";
 
J

Jochen Lehmeier

It says in the following webpage [...] and many other tutorials

Just a general comment not related to your question (which has been
answered):

For many languages, it's popular to refer to "tutorials" of differing
quality. This is because many languages do not really come with good
online or interactive documentation. Older languages came with paper books
instead, or only delivered reference manuals in electronic form.

With Perl, this is very different. All features are very well documented
and that documentation comes with Perl itself. On perldoc.perl.org you can
browse that documentation online; on Unix systems, you can use "man perl"
or "perldoc perl" to get the starting page of the documentation, with a
very good description of where to look to get all the answers you really
need (this is the same as http://perldoc.perl.org/perl.html ). There, you
will find the rest of the documentation sorted by topic, for things like
semicolons, it's the "perl syntax". If you click that link, and then use
your browsers search function to look for "semicolon", you immediately get
the relevant section: "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."

So, all in all, I'd rather stay clear from "webpages and other tutorials"
for questions related strictly to Perl itself, and look first and foremost
in the Perl documentation itself. It is extremely complete and a pleasure
to read.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top