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 });
|