adspace


How does polymorphism work in perl? Give an example.

Answer Posted / Avinash Pandey

Polymorphism in Perl works by using references and object-oriented programming. An example is using a method on a base class reference that has been initialized with an instance of a derived class. Here's an example:
```perl
# Base class
sub speak {
my ($self) = @_;
print "$self speaks the truthn";
}

# Derived class
package DerivedClass;
use base 'Examples'; # inherits from Examples
sub new {
my $class = shift;
return bless {}, $class;
}

my $obj = DerivedClass->new();
$obj->speak($obj); # prints "DerivedClass object speaks the truth"
```

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

There is no strict data types in perl unlike in other high level languages like Java so wouldn't that be a problem if a code in perl is to be a written by a big team of 20+ members ?"

2460


How do find the length of an array?

1216


What is perl? What is the basic command to print a string in perl?

944


How to connect to SQL server through Perl?

1148