- Perl ›
- Object Oriented ›
- Inheritance mechanism
Inheritance mechanism
I will explain the mechanism of inheritance in Perl.
1. Inheritance mechanism
The inheritance mechanism is that the method is searched for the upper class. Let's take a look at the previous code again.
use strict;
use warnings;
# Component class
package Component;
sub x {return 5}
# Button class
package Button;
# Inheritance description
our @ISA = ('Component');
sub new {bless {}, 'Button'}
package main;
# Use button class
my $button = Button->new;
print $button->x;
The Button class didn't have a method called x, but it was calling a method called x. I explained that this is because it inherits with the description @ISA = ('Component').
But in reality, it's just a method search.
2. Method search algorithm
The following is the method search algorithm.
If your package (Button in this example) has an x method, call it. If not, call the x method of the package (Component in this example) contained in @ISA.
Repeat this until the top superclass. Here is the implementation of inheritance in Perl.
Perl ABC