From moritz's reply I think that your problem exposition wasn't entirely clear. My image of your situation is the following:

#!/usr/bin/perl -w package PLCFuncs; sub calcTot { "The total is 10 PLCs" }; package PPDFuncs; sub calcTot { "The total is 42 PPDs" }; package Generic_Funcs; use strict; use Carp qw(croak); =head2 C<calc_totals KIND> Returns the calculated total. The kind is either C<PPD> or C<PLC>. =cut use vars qw(%calcTotals); %calcTotals = ( PLC => \&PLCFuncs::calcTot, PPD => \&PPDFuncs::calcTot, ); sub calculate_totals { my ($kind) = @_; croak "Unknown kind '$kind'" unless exists $calcTotals{ $kind }; my $calcTot = $calcTotals{$kind}; $calcTot->(); }; package main; use strict; for my $kind (qw(PPD PLC)) { print "Totals for $kind\n"; print Generic_Funcs::calculate_totals($kind), "\n" };

If you want to pass arbitrary callbacks around, just use references to subroutines:

sub get_and_print_totals { my ($callback) = @_; my $res = $callback->(); print "The total is >$res<\n"; }; get_and_print_totals( sub { return "hello total world" }); get_and_print_totals( sub { return 42+10 });

In reply to Re: Call external function by Corion
in thread Call external function by rpike

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.