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
Polymorphism is a foundational concept in object-oriented programming that allows objects of different classes to be treated as objects of a common super class. The most common use of polymorphism is when a parent class reference is used to refer to a child class object. In Perl, polymorphism is implemented through its inheritance mechanism and method overriding.
To grasp the idea of polymorphism in Perl, let's begin with a simple example.
Let's start with a base class called Animal
with a method named speak
:
package Animal; sub new { my $class = shift; return bless {}, $class; } sub speak { print "An animal speaks!\n"; } 1; # Return true for the module to be successfully loaded.
Now, let's define two derived classes, Dog
and Cat
, which inherit from the Animal
class and override the speak
method:
package Dog; use base 'Animal'; sub speak { print "Woof!\n"; } 1;
package Cat; use base 'Animal'; sub speak { print "Meow!\n"; } 1;
With the classes defined, we can now demonstrate polymorphism. Even though we're using a reference to the base class Animal
, we can still call the speak
method of the child classes:
use Animal; use Dog; use Cat; my $dog = Dog->new(); my $cat = Cat->new(); make_animal_speak($dog); # Prints "Woof!" make_animal_speak($cat); # Prints "Meow!" sub make_animal_speak { my $animal = shift; $animal->speak(); }
In the above example, make_animal_speak
expects a reference to an object of type Animal
. But since both Dog
and Cat
are derived from Animal
, we can pass them to this function. This is polymorphism in action: the same function can operate on objects of different classes, and the correct speak
method is called based on the actual type of object.
Polymorphism in Perl enables you to write more generic and reusable code. It's an essential concept in object-oriented programming and is integral in designing scalable and maintainable Perl applications.
Object-oriented programming and polymorphism in Perl: Object-oriented programming (OOP) in Perl involves creating classes and objects, encapsulating data and behavior. Polymorphism, a key OOP concept, allows objects of different classes to be treated as objects of a common base class.
Perl polymorphic methods:
package Animal; sub speak { die "Subclass must implement 'speak' method"; } package Dog; use parent -norequire, 'Animal'; sub speak { print "Woof!\n"; } package Cat; use parent -norequire, 'Animal'; sub speak { print "Meow!\n"; } # Usage my $dog = Dog->new(); $dog->speak(); # Outputs: Woof! my $cat = Cat->new(); $cat->speak(); # Outputs: Meow!
package Shape; sub draw { die "Subclass must implement 'draw' method"; } package Circle; use parent -norequire, 'Shape'; sub draw { print "Drawing a circle\n"; } package Square; use parent -norequire, 'Shape'; sub draw { print "Drawing a square\n"; } # Usage my $circle = Circle->new(); $circle->draw(); # Outputs: Drawing a circle my $square = Square->new(); $square->draw(); # Outputs: Drawing a square
sub perform_draw { my $shape = shift; $shape->draw(); } # Usage perform_draw($circle); # Outputs: Drawing a circle perform_draw($square); # Outputs: Drawing a square
package Employee; sub calculate_salary { die "Subclass must implement 'calculate_salary' method"; } package Manager; use parent -norequire, 'Employee'; sub calculate_salary { my $self = shift; # Manager-specific salary calculation } package Programmer; use parent -norequire, 'Employee'; sub calculate_salary { my $self = shift; # Programmer-specific salary calculation }
package Shape; sub draw { die "Subclass must implement 'draw' method"; } sub move { die "Subclass must implement 'move' method"; } package Circle; use parent -norequire, 'Shape'; sub draw { print "Drawing a circle\n"; } sub move { print "Moving a circle\n"; } # Usage my $circle = Circle->new(); $circle->draw(); # Outputs: Drawing a circle $circle->move(); # Outputs: Moving a circle
Perl polymorphism examples: Examples throughout this discussion showcase polymorphism in different scenarios.
Overloading methods for polymorphism in Perl: Overloading allows a method to behave differently based on the number or types of arguments.
package MyClass; use overload '+' => \&add; sub new { my ($class, $value) = @_; return bless { value => $value }, $class; } sub add { my ($self, $other) = @_; return MyClass->new($self->{value} + $other->{value}); } # Usage my $obj1 = MyClass->new(5); my $obj2 = MyClass->new(10); my $result = $obj1 + $obj2; print $result->{value}; # Outputs: 15