Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Check for sub / don't fail on non-existent sub

by SFLEX (Chaplain)
on Aug 29, 2009 at 10:39 UTC ( [id://792067]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://792067]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2025-06-24 09:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.