in reply to Getting the name of sub from sub reference
Define a AUTOLOAD-sub and then call the code-ref that does not exist.
The call will then end up in the AUTOLOAD-sub where the (fully qualified) name of the sub is then available in the $AUTOLOAD-variable.
e.g.:
This will produce "cannot call main::def".use strict; use warnings; use vars qw($AUTOLOAD); my @q = (\&abc, \&def); my $msg; my $param = "hhhhhh"; foreach my $q (@q) { if (defined(&$q)) { $msg .= $q->($param); } else { my $name = $q->(); $msg .= "cannot call $name\n"; } } print $msg; sub abc { my $q = shift; return "hello from ABC($q)\n"; } sub AUTOLOAD { return $AUTOLOAD; }
Getting rid of the package-part is an easy exercise left for the reader.
|
|---|