in reply to How to use polymorphism in perl
Hello ramu, and welcome to the Monastery!
Perl provides two three mechanisms to facilitate OO polymorphism: inheritance, method overloading as illustrated by tobyink above, and operator overloading, via the overload pragma detailed in the perldoc article overload.
A useful example of operator overloading in action is provided by the standard module Math::Complex. The documentation for this module notes that:
Thanks to overloading, the handling of arithmetics with complex numbers is simple and almost transparent.
For example, when complex numbers created using this module are added using the + operator, operator overloading ensures that Perl performs an addition operation suitable for complex numbers. But when ordinary floating-point numbers are added, Perl applies the standard (non-overloaded) addition operation. In other words, the same syntax is used for adding different types of numbers, but the additions actually performed are tailored to the particular types of the numbers being added — which is the essence of polymorphism.
Hope that helps,
Update: Amended to take account of the post by Anonymous Monk, below. Apparently, I’m still thinking C++ instead of Perl. :-(
Athanasius <°(((>< contra mundum
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to use polymorphism in perl
by Anonymous Monk on Sep 30, 2012 at 09:40 UTC |