How does polymorphism work in perl? Give an example.



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

Post New Answer

More CGI Perl Interview Questions

How do I replace every TAB character in a file with a comma?

1 Answers  


How many types of primary data structures in Perl and what do they mean?

1 Answers  


Write syntax to use grep function?

1 Answers  


What are the various perl data types based on the context?

1 Answers  


Why is it hard to call this function: sub y { "because" } ?

1 Answers  


what is the procedure to define a user define module in your perl application?

2 Answers   IBM,


Explain the difference between use and require?

5 Answers   Aspire,


You want to add two arrays together. How would you do that?

1 Answers  


How do you find the length of an array?

1 Answers  


What purpose does each of the following serve: -w, strict, - T ?

2 Answers  


Explain join function in perl?

1 Answers  


How to create a package?

1 Answers  


Categories