I am trying to write some generic code which will reference some subroutines that may or may not exist in the code itself. I know how to identify if the subroutine exists given a reference to it. But in the cases where the reference is to a sub that does not exist, I want to print that sub's name. Here's what I have tried so far:
use strict;
use warnings;
my @q = (\&abc, \&def);
my $msg;
my $param = "hhhhhh";
foreach my $q (@q) {
if (defined(&$q)) {
$msg .= $q->($param);
} else {
$msg .= "cannot call $q\n";
}
}
print $msg;
sub abc {
my $q = shift;
return "hello from ABC($q)\n";
}
This works correctly for the abc() routine which does exist. Moreover, this code correctly identifies that def() routine does not exist. However, when I print that out, all I get is cannot call CODE(0xac1a40).Is there a way for me to instead print out cannot call def?
Thanks as always!