base
use base qw(Mother Father);
This pragma lets a programmer conveniently declare a derived class based
on the listed parent classes. This pragma has mostly fallen out of favor,
and most people prefer to use the parent pragma.
The declaration above is roughly equivalent to:
BEGIN {
require Mother;
require Father;
push @ISA, qw(Mother Father);
}The base pragma takes care of any
require needed. When the strict "vars" pragma is in scope, use base lets you (in effect) assign to @ISA without first having to declare our @ISA. (Since the base pragma happens at compile time, it’s best
to avoid diddling @ISA on your own at
runtime.)
But beyond this, base has another
property. If any named base class use fields facility under fields (mentioned later in this chapter), then the pragma
initializes the package’s special field attributes from the base class.
(Multiple inheritance of field classes is not
supported. The base pragma raises an
exception if more than one named base class has fields.)
Any base class not yet loaded will be loaded automatically via
require. However, whether to require a base class package is determined not
by the customary inspection of %INC,
but by the absence of a global $VERSION
in the base package. This hack keeps Perl from repeatedly trying (and
failing) to load a base class that isn’t in its own requirable file
(because, for example, it’s loaded as part of some other module’s file).
If $VERSION is not detected after
successfully loading a file, base will
define $VERSION ...
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