package child1;
sub polyexample
{
my $class = shift;
my $param1 = shift;
my $param2 = shift;
print "polyexample (child1) called with [$param1] and [$param2]\n";
}
1;
####
package child2;
sub polyexample
{
my $class = shift;
my $param1 = shift;
my $param2 = shift;
print "polyexample (child2) called with [$param1] and [$param2]\n";
}
1;
####
use strict;
use Carp;
my $class = shift;
my $method = shift;
my $param1 = shift;
my $param2 = shift;
eval("use $class");
if($@)
{
croak("Couldn't find package $class ($@) ");
}
eval($class->$method($param1, $param2));
if($@)
{
croak ("couldn't invoke method #method on $class with params ($@)")
}
####
~/src/perl/poly$ perl main.pl child1 polyexample 1 2
polyexample (child1) called with [1] and [2]
####
~/src/perl/poly$ perl main.pl child2 polyexample 1 2
polyexample (child2) called with [1] and [2]
####
~/src/perl/poly$ perl main.pl child5 polyexample 1 2
Couldn't find package child5 (Can't locate child5.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at (eval 1) line 2.
BEGIN failed--compilation aborted at (eval 1) line 2.