Packages

Packages[378] allow you to compartmentalize namespaces. Imagine you have ten programmers all working on one big project. If you use the global names $fred, @barney, %betty, and &wilma in your part of the project, what happens when I accidentally use one of those same names in my part? Packages keep them separate; I can access your $fred, and you can access mine but not by accident. Packages are needed to make Perl scalable, so we can manage large programs. The Alpaca discusses packages in detail.

[378] The name “package” is perhaps an unfortunate choice in that it makes many people think of a packaged-up chunk of code (in Perl, that’s a module or a library). All that a package does is define a namespace (a collection of global symbol names, like $fred or &wilma). A namespace is not a chunk of code.

Get Learning Perl, Fourth 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.