Perl Programming/Keywords/package

Previous: pack Keywords Next: pipe

The package keyword

edit

The package keyword declares the BLOCK or the rest of the compilation unit within the given namespace. The package scope is given by either the supplied BLOCK or from the declaration through the end of the current scope, which may be the enclosing block, eval, or file. In other words, without BLOCK, the scopes are just like with my, our, and state operators. Only overridden or special identifiers such as ARGV, ENV, and STDOUT that qualify into main:: are exempted.

Only dynamic variables are affected by the package keyword. Lexically-scoped created with my, our, and state are exempted. It is allowed to switch into a package at more than one place, as this only determines which default symbol table the compiler uses for the rest of that block. An identifier prefixed by it's package name can access another package, like $SomePack::var or ThatPack::INPUT_HANDLE. Without the package name, the main package is assumed: $::sail is understood as $main::sail.

With VERSION, the package sets the $VERSION variable in the given namespace to a version object with the provided VERSION. VERSION must be a strict-style version number with at least three components, as defined by the version module, $VERSION should be set only once in a package.

Syntax

edit
  package NAMESPACE
  package NAMESPACE VERSION
  package NAMESPACE BLOCK
  package NAMESPACE VERSION BLOCK

See also

edit
Previous: pack Keywords Next: pipe