package
packageNAMESPACEVERSIONBLOCKpackageNAMESPACEVERSIONpackageNAMESPACEBLOCKpackageNAMESPACE
This is not really a function but a declaration that says that the
BLOCK, or the rest of the innermost enclosing
scope, belongs to the indicated symbol table or namespace. (The scope of
a package declaration is thus the
same as the scope of a my, state, or our declaration.) Within its scope, the
declaration causes the compiler to resolve all unqualified global
identifiers by looking them up in the declared package’s symbol
table.
A package declaration affects
only global variables—including those on which you’ve used local—not lexical variables created with
my, state, or our. It only affects unqualified global
variables; global variables that are qualified with a package name of
their own ignore the current declared package. Global variables declared
with our are unqualified and
therefore respect the current package, but only at the point of
declaration, after which they behave like my variables. That is, for the rest of their
lexical scope, our variables are
“nailed” to the package in use at the point of declaration, even if a
subsequent package declaration intervenes.
Typically, you would put a package declaration as the first thing in a
file that is to be included by the require or use operator, but you can put one anywhere a statement would be legal. When creating a traditional or object-oriented module file, it is customary to name the package the same name as the file to ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access