Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Does every perl statement has to end with ';'?

Reply
Thread Tools

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

 
 
Peng Yu
Guest
Posts: n/a
 
      12-31-2009
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"


 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      12-31-2009
Peng Yu <(E-Mail Removed)> wrote:
> 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
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      12-31-2009
Peng Yu <(E-Mail Removed)> wrote:
>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
 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      12-31-2009
>>>>> "Peng" == Peng Yu <(E-Mail Removed)> writes:

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,";

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
 
Reply With Quote
 
Jochen Lehmeier
Guest
Posts: n/a
 
      01-01-2010
On Thu, 31 Dec 2009 21:16:27 +0100, Peng Yu <(E-Mail Removed)> wrote:

> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 03-09-2011 11:00 PM
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 02-11-2011 05:00 AM
Explorer.exe error report every time access every type of video me =?Utf-8?B?SXJ3YW5zeWFo?= ASP .Net 4 10-30-2007 07:49 AM
"'If' must end with a matching 'End If'." <-- BUT IT DOES!! WHY DOESN'TTHE DAMN SERVER SEE IT??? Neo Geshel ASP .Net 2 08-18-2004 01:37 PM
Any one know how to set an alias in the perl, and use it after the perl script end Lim kiang Leng Perl Misc 3 07-17-2004 11:11 PM



Advertisments