Inheritance Through @ISA
If @ISA contains more than one
package name, the packages are all searched in left-to-right order by
default. The search is depth-first, so if you have a Mule class set up for inheritance this
way:
package Mule;
our @ISA = ("Horse", "Donkey");Perl looks for any methods missing from Mule first in Horse (and any of its ancestors, like Critter) before going on to search through
Donkey and its ancestors.
If a missing method is found in a base class, Perl internally
caches that location in the current class for efficiency, so the next
time it has to find the method, it doesn’t have to look as far. Changing
@ISA or defining new methods
invalidates the cache and causes Perl to perform the lookup
again.
When Perl searches for a method, it makes sure that you haven’t created a circular inheritance hierarchy. This could happen if two classes inherit from one another, even indirectly through other classes. Trying to be your own great-grandfather is too paradoxical even for Perl, so the attempt raises an exception. However, Perl does not consider it an error to inherit from more than one class sharing a common ancestry, which is rather like cousins marrying. Your inheritance hierarchy just stops looking like a tree and starts to look like a directed acyclic graph. This doesn’t bother Perl—so long as the graph really is acyclic.
When you set @ISA, the
assignment normally happens at runtime, so unless you take precautions,
code in BEGIN, CHECK, UNITCHECK, or INIT blocks won’t ...
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