Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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
    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?

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-06-15 19:32 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.