Packages

Learn about packages in Perl.

A Perl namespace associates and encapsulates various named entities. It’s like our family name or a brand name. Unlike a real-world name, a namespace implies no direct relationship between entities. Such relationships may exist, but they’re not required to.

A package in Perl is a collection of code in a single namespace. The distinction is subtle: the package represents the source code, and the namespace represents the internal data structure Perl uses to collect and group that code.

Declaring a package

The package built-in declares a package and a namespace:

Press + to interact
package MyCode;
our @boxes;
sub add_box { ... }

Package scope

All global variables and functions declared or referred to after the package declaration refer to symbols within the MyCode namespace. We can refer to the @boxes variable from the main namespace only by its fully qualified name of @MyCode::boxes. A ...