Packages
Learn to work with Perl packages and namespaces, understanding their role in organizing code and managing global variables and functions. Discover how package versions and fully qualified names help maintain scalable and clear Perl code, enhancing maintainability and reducing namespace conflicts.
We'll cover the following...
Packages
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:
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 ...