Perl v5.14 gets a step closer to a saner way to declare classes with its new package NAME BLOCK
syntax that lets you easily group everything that goes in a package.
Previously, to limit a package you had to declare the package inside the scope:
{ package Some::Class; ... }
That ...
is another v5.12 new feature, the yadda yadda operator. It’s a compilable placeholder for statements to come later.
Many people think that the package
statement changes the scope, but it doesn’t (see Know what creates a scope). It doesn’t.
Now you can put the package
statement outside the block so everything in the block is in that package:
use v5.14; package Some::Class { ... }
You can also add a version too, like the package NAME VERSION syntax that v5.12 added:
use v5.14; package Some::Class 1.23 { ... }
This even works with nested classes:
use v5.14; package Local::foo { say "1. Package is " . __PACKAGE__; package Local::bar { say "2. Package is " . __PACKAGE__; } say "3. Package is " . __PACKAGE__; }
This is handy for small private classes that you don’t want to put in a separate file. That also means another part of the code can’t load it explicitly because there’s no file that maps to its package name. That class is not actually hidden; it’s merely obscure.
And, if your editor supports it, you can also fold the block that contains the package. But then you may be able to fold naked blocks as well.