Why create your own dispatch table when perl has one built in?
Or - violating specifications for fun? and profit?
#!/usr/bin/perl
# http://perlmonks.org/?node_id=1203952
use strict;
use warnings;
# Callback functions ---------------------------------------
sub first::callback {
my $z = pop;
print "in first_callback, z=$z\n";
return 1;
}
sub last::callback {
my $z = pop;
print "in last_callback, z=$z\n";
return 2;
}
sub UNIVERSAL::callback { -1 }
# Implementation of dispatch table -------------------------
# (You need to write this code)
#my %op_table = ( first => \&first_callback,
# last => \&last_callback,
# );
sub invoker
{
my ($name, $z) = @_;
$name->callback($z);
}
# Main program for testing ---------------------------------
for my $name ( "first", "last", "fred" ) {
my $rc = invoker( $name, $name . '-arg' );
print "$name: rc=$rc\n";
}
Have I once again managed to fail an interview?