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

Revered monks,
I am curious about the syntax of the switch module. I have seen two variations and both seem to work. I wanted to know which of these 2 is the correct one.
I am also curious to know why both of them work!! Isn't semicolon a requirement??!
use Switch; switch ($char) { case 1 { print "1" } case 2 { print "2" } }
Note: The semicolons are missing above and are present below
use Switch; switch ($char) { case 1 { print "1"; } case 2 { print "2"; } }
Any light on this behaviour of Perl is greatly appreciated.

Thanks

Replies are listed 'Best First'.
Re: Question about switch module for Perl
by jdhedden (Deacon) on Nov 03, 2006 at 20:41 UTC
    From 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.) ...

    Remember: There's always one more bug.
Re: Question about switch module for Perl
by Fletch (Bishop) on Nov 03, 2006 at 20:57 UTC
Re: Question about switch module for Perl
by ysth (Canon) on Nov 03, 2006 at 20:22 UTC
    No, semicolons aren't necessary at the end of a block, just as in if ($^O) { print "foo"; } vs. if ($^O) { print "foo" }