in reply to How do I test the validity of a CODE reference?

If you're going to call the sub anyway, call it in an eval and see what happens. If some other error comes up, you can rethrow it with die.

eval { &$sub }; if ( $@ =~ /^Undefined subroutine/ ) { # This is what happens when you call a bogus sub ref. } elsif ( $@ ) { die $@; }

Replies are listed 'Best First'.
Re^2: How do I test the validity of a CODE reference?
by Your Mother (Archbishop) on Aug 08, 2008 at 01:07 UTC

    Probably $sub->() is a better idiom, for the same reason to prefer prefered() to &prefered -- no accidental arg passing.