http://www.perlmonks.org?node_id=1038504


in reply to OO Perl Baby Steps

Your usages have subtly different semantic, and in some cases, practical meanings, especially if you're using Perl 5.14 or newer.

{ package somename; ..... }: This creates a lexical scope, and inside that lexical scope you happen to be working with package somename. When the block ends, you will be working back in the previous package again, though this is not the preferred syntax (see below) as of Perl 5.14.

package somename; { ..... }: This declares that you're working with package somename, and then creates a lexical scope. The two are not really related to each other, but it does happen that this lexical scope was created while you're working in package somename.

package somename { ..... }: This is a new Perl 5.14 feature; you've declared that you'll be using package somename for the duration of the following block (I suppose you could change within the block, but haven't tried). Once the block expires, the package you will find yourself working within will revert back to whatever was in effect before this package block.(See http://www.perl.com/pub/2011/05/new-features-of-perl-514-package-block.html).

The next two are just the same as other constructs you demonstrated, but with an extra semicolon at the end, which has no effect. You can put a semicolon just about anywhere you want as long as it doesn't break a statement. print "hello world\n" ;;;;;;;;;;;;; print "Hello again\n";;;;;; # Perfectly legal

The last one is a syntax error because you didn't separate the "package somename" statement from the next statement with a semicolon. It's not a syntax error if that statement is the last statement within the block, since semicolons are optional for the final statement in a block.


Dave