A Perl namespace associates and encapsulates various named entities within a named category, like your family name or a brand name. Unlike a real-world name, a namespace implies no direct relationship between entities. Such relationships may exist, but do not have to.
A package in Perl 5 is a collection of code in a single namespace. The distinction is subtle: the package represents the source code and the namespace represents the entity created when Perl parses that code.
The package
builtin declares a package and a namespace:
All global variables and functions declared or referred to after the package declaration refer to symbols within the MyCode
namespace. You can refer to the @boxes
variable from the main
namespace only by its fully qualified name of @MyCode::boxes
. A fully qualified name includes a complete package name, so you can call the add_box()
function only by MyCode::add_box()
.
The scope of a package continues until the next package
declaration or the end of the file, whichever comes first. Perl 5.14 enhanced package
so that you may provide a block which explicitly delineates the scope of the declaration:
The default package is the main
package. Without a package declaration, the current package is main
. This rule applies to one-liners, standalone programs, and even .pm files.
Besides a name, a package has a version and three implicit methods, import()
(importing), unimport()
, and VERSION()
. VERSION()
returns the package's version number. This number is a series of numbers contained in a package global named $VERSION
. By rough convention, versions tend to be a series of integers separated by dots, as in 1.23
or 1.1.10
, where each segment is an integer.
Perl 5.12 introduced a new syntax intended to simplify version numbers, as documented in perldoc version::Internals
. These stricter version numbers must have a leading v
character and at least three integer components separated by periods:
With Perl 5.14, the optional block form of a package
declaration is:
In 5.10 and earlier, the simplest way to declare the version of a package is:
Every package inherits a VERSION()
method from the UNIVERSAL
base class. You may override VERSION()
, though there are few reasons to do so. This method returns the value of $VERSION
:
If you provide a version number as an argument, this method will throw an exception unless the version of the module is equal to or greater than the argument:
Every package
declaration creates a new namespace if necessary and causes the parser to put all subsequent package global symbols (global variables and functions) into that namespace.
Perl has open namespaces. You can add functions or variables to a namespace at any point, either with a new package declaration:
... or by fully qualifying function names at the point of declaration:
You can add to a package at any point during compilation or runtime, regardless of the current file, though building up a package from multiple separate declarations can make code difficult to spelunk.
Namespaces can have as many levels as your organizational scheme requires, though namespaces are not hierarchical. The only relationship between packages is semantic, not technical. Many projects and businesses create their own top-level namespaces. This reduces the possibility of global conflicts and helps to organize code on disk. For example:
StrangeMonkey
is the project nameStrangeMonkey::UI
contains top-level user interface codeStrangeMonkey::Persistence
contains top-level data management codeStrangeMonkey::Test
contains top-level testing code for the project
... and so on.
Hey! The above document had some coding errors, which are explained below:
- Around line 3:
-
A non-empty Z<>