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


In reply to Re: How to use polymorphism in perl by Athanasius
in thread How to use polymorphism in perl by ramu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.