in reply to How do I test the validity of a CODE reference?
You don't need eval and you can just inspect the main namespace for whether a subroutine of that name is defined:
use strict; use Data::Dumper; sub foo { print "foo\n"; } for my $subname (@ARGV) { my $glob = $::{$subname}; warn defined $glob ? "'*$subname' exists" : "'*$subname' not found"; if ($glob) { my $sub = *{$glob}{CODE}; warn defined $sub ? "'$subname' exists" : "'$subname' not found"; &$sub if $sub }; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How do I test the validity of a CODE reference?
by tinita (Parson) on Aug 10, 2008 at 12:57 UTC | |
by ikegami (Patriarch) on Aug 10, 2008 at 13:27 UTC | |
|
Re^2: How do I test the validity of a CODE reference?
by pilap (Initiate) on Aug 08, 2008 at 17:33 UTC | |
by Corion (Patriarch) on Aug 09, 2008 at 08:45 UTC |