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.


In reply to Re: dynamic perl calls by osunderdog
in thread dynamic perl calls by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.