Module Privacy and the Exporter
Perl does not automatically patrol private/public borders within its modules—unlike languages such as C++, Java, and Ada, Perl isn’t obsessed with enforced privacy. A Perl module would prefer that you stay out of its living room because you weren’t invited, not because it has a shotgun.
The module and its user have a contract, part of which is common
law and part of which is written. Part of the common law contract is
that a module refrain from changing any namespace it wasn’t asked to
change. The written contract for the module (that is, the documentation)
may make other provisions. But then, having read the contract, you
presumably know that when you say use
RedefineTheWorld you’re redefining the world, and you’re
willing to risk the consequences. The most common way to redefine worlds
is to use the Exporter module. As we’ll see later in this chapter, you can even
redefine built-ins with this module.
When you use a module, the
module typically makes some variables or functions available to your
program or, more specifically, to your program’s current package. This
act of exporting symbols from the module (and thus importing them into
your program) is sometimes called polluting your
namespace. Most modules use Exporter
to do this; that’s why near the top most modules say something like one
of these:
use parent qw(Exporter);
require Exporter;
our @ISA = ("Exporter");These two lines make the module inherit from the Exporter class. Inheritance is described ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access