Book Home Programming PerlSearch this book

31.3. use base

use base qw(Mother Father);

This pragma lets a programmer conveniently declare a derived class based upon the listed parent classes. The declaration above is roughly equivalent to:

BEGIN {
    require Mother;
    require Father;
    push @ISA, qw(Mother Father);
}
The use 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 use base pragma happens at compile time, it's best to avoid diddling @ISA on your own at run time.)

But beyond this, use base has another property. If any named base class makes use of the fields facility described under use fields 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 use 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, use base will define $VERSION in the base package, setting it to the string "-1, defined by base.pm".



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.