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

Discussion in 'Perl Misc' started by Peng Yu, Dec 31, 2009.

  1. Peng Yu

    Peng Yu Guest

    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"
    Peng Yu, Dec 31, 2009
    #1
    1. Advertising

  2. Peng Yu <> 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://toerring.de
    Jens Thoms Toerring, Dec 31, 2009
    #2
    1. Advertising

  3. Peng Yu <> 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
    Jürgen Exner, Dec 31, 2009
    #3
  4. >>>>> "Peng" == Peng Yu <> 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
    <> <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
    Randal L. Schwartz, Dec 31, 2009
    #4
  5. On Thu, 31 Dec 2009 21:16:27 +0100, Peng Yu <> 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
    Jochen Lehmeier, Jan 1, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alejandro Pombo
    Replies:
    0
    Views:
    453
    Alejandro Pombo
    Jan 25, 2006
  2. Neo Geshel
    Replies:
    2
    Views:
    3,568
    Versteijn
    Aug 18, 2004
  3. Roula
    Replies:
    0
    Views:
    385
    Roula
    Feb 26, 2006
  4. =?Utf-8?B?SXJ3YW5zeWFo?=
    Replies:
    4
    Views:
    2,434
    =?Utf-8?B?SXJ3YW5zeWFo?=
    Oct 30, 2007
  5. Ashliey Elx
    Replies:
    3
    Views:
    73
    Ike Naar
    Apr 5, 2014
Loading...

Share This Page