Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
Inheritance is a fundamental concept in object-oriented programming, allowing one class (the subclass or child class) to inherit properties and methods from another class (the superclass or parent class). In Perl, inheritance is implemented using packages and modules. This tutorial will guide you through the process of implementing inheritance in Perl.
Let's start with a simple example. We'll create a base class Animal
and a derived class Dog
which will inherit from Animal
.
Animal.pm (Base class)
package Animal; sub new { my $class = shift; my $self = { name => shift, age => shift, }; bless $self, $class; return $self; } sub speak { print "Some generic animal sound\n"; } 1; # Returning a true value
Dog.pm (Derived class)
package Dog; use Animal; # Import the Animal package our @ISA = qw(Animal); # Inherit from Animal sub speak { print "Woof, woof!\n"; } 1;
In the above code, the @ISA
array tells Perl that Dog
is inheriting from Animal
. The derived class Dog
has overridden the speak
method from its parent class Animal
.
use Dog; my $dog = Dog->new("Buddy", 3); $dog->speak(); # Outputs: Woof, woof!
When you call the speak
method on a Dog
object, the overridden method in Dog
is invoked instead of the one in Animal
.
If you still need to access the method in the parent class that has been overridden in the child class, you can use the SUPER
keyword.
Inside the Dog
package:
sub parent_speak { my $self = shift; $self->SUPER::speak(); }
Now, when you call the parent_speak
method on a Dog
object, it will invoke the speak
method from the Animal
class.
Perl supports multiple inheritance, meaning a class can inherit from multiple parent classes. This is done by adding multiple classes to the @ISA
array:
our @ISA = qw(ParentClass1 ParentClass2);
However, multiple inheritance can lead to complexities and is generally discouraged in favor of other design patterns, like composition.
Inheritance allows classes to reuse and extend code from other classes, promoting the DRY (Don't Repeat Yourself) principle. While it's a powerful tool, it's essential to use it judiciously, keeping the design clean and understandable.
Remember that in Perl, the real power of object-oriented programming shines when combined with other features like modules, encapsulation, and polymorphism. As always, it's a good practice to keep classes and their responsibilities clear and distinct.
Object-oriented programming in Perl:
# Class definition package Animal; sub new { my ($class, $name) = @_; my $self = { name => $name }; bless $self, $class; return $self; } sub speak { my ($self) = @_; print $self->{name}, " makes a sound\n"; } # Creating an object my $cat = Animal->new('Whiskers'); $cat->speak();
Using base and parent in Perl inheritance:
base
and parent
modules in Perl are used for specifying base classes in an inheritance hierarchy.# Base class package Animal; sub new { my ($class, $name) = @_; my $self = { name => $name }; bless $self, $class; return $self; } # Derived class package Cat; use parent -norequire, 'Animal'; sub meow { my ($self) = @_; print $self->{name}, " meows\n"; } # Creating objects my $cat = Cat->new('Whiskers'); $cat->speak(); # Inherited from Animal $cat->meow();
Perl SUPER keyword usage:
SUPER
keyword in Perl is used to call methods from the parent class in an overriding method of the subclass.package Animal; sub speak { my ($self) = @_; print $self->{name}, " makes a sound\n"; } package Cat; use parent -norequire, 'Animal'; sub speak { my ($self) = @_; print "Meow!\n"; $self->SUPER::speak(); # Calling the parent method } my $cat = Cat->new('Whiskers'); $cat->speak();