http://www.perlmonks.org?node_id=742764


in reply to dynamic perl calls

It sounds like you want to do polymorphism. This is a quick example that sounds like a starting point for you.

child1.pm

package child1; sub polyexample { my $class = shift; my $param1 = shift; my $param2 = shift; print "polyexample (child1) called with [$param1] and [$param2]\n" +; } 1;

child2.pm

package child2; sub polyexample { my $class = shift; my $param1 = shift; my $param2 = shift; print "polyexample (child2) called with [$param1] and [$param2]\n" +; } 1;

main.pl

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 ($@)" +) }

Finally, here are the two example invocations:

~/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]

and an example failure

~/src/perl/poly$ perl main.pl child5 polyexample 1 2 Couldn't find package child5 (Can't locate child5.pm in @INC (@INC con +tains: /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.1 +0.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/per +l/5.10 /usr/local/lib/site_perl .) at (eval 1) line 2. BEGIN failed--compilation aborted at (eval 1) line 2.

And many wonderful permutations of this exist.

Still searching; Still seeking.