How does polymorphism work in perl? Give an example.
Answer / 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 |
How do I replace every TAB character in a file with a comma?
How many types of primary data structures in Perl and what do they mean?
Write syntax to use grep function?
What are the various perl data types based on the context?
Why is it hard to call this function: sub y { "because" } ?
what is the procedure to define a user define module in your perl application?
Explain the difference between use and require?
You want to add two arrays together. How would you do that?
How do you find the length of an array?
What purpose does each of the following serve: -w, strict, - T ?
Explain join function in perl?
How to create a package?