Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Perl polymorphism work?

Tags:

perl

I am unable to figure out an explanation on how polymorphism works in Perl. I understand what polymorphism means but I am trying to figure out how it internally works within perl. Can someone point me to some documentation that explains it. All the google searches I have done gives me examples of what polymorphism is in perl but not how perl makes it work.

like image 965
user238021 Avatar asked Jun 13 '12 17:06

user238021


People also ask

How does polymorphism work?

Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface. The beauty of polymorphism is that the code working with the different classes does not need to know which class it is using since they're all used the same way.

What is the concept of polymorphism?

Polymorphism is the ability of different objects to respond in a unique way to the same message.

Does Perl support object-oriented programming?

Summary: in this tutorial, you will learn about Perl Object-Oriented Programming or Perl OOP. You will learn how to create a simple Perl class and use it in other programs. Besides procedural programming, Perl also provides you with object-orient programming paradigm.

How polymorphism is achieved in inheritance?

Inheritance supports the concept of reusability and reduces code length in object-oriented programming. Polymorphism allows the object to decide which form of the function to implement at compile-time (overloading) as well as run-time (overriding).


1 Answers

When a method is invoked on an object or class, Perl looks to see if that method is provided directly by the class itself.

Because classes are simply Perl packages, it is simply a matter of looking for the existence of a subroutine &Class::method.

If no such subroutine is found, Perl examines the @ISA array in the same package (i.e. @Class::ISA) which contains a list of base classes for the class, and does the same check for every package/class that appears in there.

Each of those classes in turn may also have an @ISA array, so the search is recursive.

Finally, if the method is found nowhere by this method, Perl looks in a special package UNIVERSAL for a subroutine &UNIVERSAL::method.

A failure at this point goes on to invoke the AUTOLOAD system, but that is really beyond the principle of polymorphism.

A failure to find a suitable matching method anywhere raises an exception.

like image 121
Borodin Avatar answered Sep 30 '22 14:09

Borodin