in reply to Check for sub / don't fail on non-existent sub

I use this:
my $sub_x = 'Some::Sub'; my $Result = 'Bad'; if (exists &{$sub_x} && (ref $sub_x eq 'CODE' || ref $sub_x eq '')) { $Result = 'Good'; } print $Result . "\n"

What did the Pro-Perl programmer say to the Perl noob?
You owe me some hair.

Replies are listed 'Best First'.
Re^2: Check for sub / don't fail on non-existent sub
by ikegami (Patriarch) on Aug 29, 2009 at 12:41 UTC
    Simpler:
    my $sub_x = 'Some::Sub'; if (defined(&$sub_x)) { print("It's there\n"); } else { print("It's not there\n"); }
      Wow, I expected that defined() would be applyed to the result of $sub_x, but a test showed that it doesn't.
      Thank you all!