Name

package

Synopsis

package namespace
                  

Declares that the rest of the innermost enclosing block, subroutine, eval, or file belongs to the indicated namespace. (The scope of a package declaration is thus the same as the scope of a local or my declaration.) All subsequent references to unqualified global identifiers will be resolved by looking them up in the declared packages symbol table. A package declaration affects only global variables—including those you’ve used local on—but not lexical variables created with my.

Using package without an argument is possible, but since its semantics are unclear, package; has been depracated in Perl 5.8. If you intend to disallow variables that aren’t fully qualified, use strict; instead.

Typically, you put a package declaration as the first thing in a file that will be included by the require or use operator, but you can put one anywhere that a statement would be legal. When defining a class or a module file, it is customary to name the package the same name as the file, to avoid confusion. (It’s also customary to name such packages beginning with a capital letter, because lowercase modules are, by convention, interpreted as pragmas.)

Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.