in reply to Polymorphism in Perl

See overload about overloading.

You get what the OO people call polymorphism by overridng inherited methods in child classes. See perlboot and perltoot for an introduction to OO in Perl.

Replies are listed 'Best First'.
Re^2: Polymorphism in Perl (interface)
by tye (Sage) on Feb 07, 2011 at 14:37 UTC

    You don't need inheritance (much less method overriding) for polymorphism in Perl. You can just use duck typing, where you design an interface that consists of one or more method calls and then any object or class that implements that interface can be passed to code that makes use of that interface.

    Note that Perl itself doesn't provide a way to declare "interfaces" so when I say "design an interface", I'm talking about documentation for humans not coding. Though, languages like C++ and Java that support declaring an interface don't actually express interfaces fully, of course, so designing an interface there should also involve documentation for humans, not just writing a bunch of method signatures.

    - tye        

      sub feedDucks { while (my $object = shift) { if ( $object->can('walk') and $object->can('quack') and $object->can('swim') and $object->can('feed') ) { $object->feed('breadcrumbs'); }else{ warn "Insufficient mana to polymorph $object!"; } } }