Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Checking if a subroutine has been defined in a package...

by dragonchild (Archbishop)
on Oct 12, 2001 at 22:30 UTC ( [id://118545]=note: print w/replies, xml ) Need Help??


in reply to Checking if a subroutine has been defined in a package...

Ok. I've been trying to do
Do something if defined &{"$package::$funcName"};
and it's not working. Here's the situation:
  • In package A, I've got a function foo() which defines the subroutines, supposedly as needed.
  • In package B, which inherits (at some distance) from package A, I've got the definitions of various subroutines and the call to foo().
For some reason, the defined check isn't catching that the functions are supposed to be defined elsewhere. Why is that?

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Replies are listed 'Best First'.
Re: Re: Checking if a subroutine has been defined in a package...
by pjf (Curate) on Oct 13, 2001 at 18:44 UTC
    G'day dragonchild,

    What your code is doing is calling the subroutine "$package::$funcName", and checking if it returns a defined value. This is certainly not what you want to be doing, because if the subroutine does not exist, you end up with a fatal errror.

    If your subroutine does exist, you've just invoked it with no arguments, and it could return undef just to spite you anyway.

    Rather than calling the subroutine, you instead want to use globs to access perl's symbol table. This is described in my reply above. It also happens to be a bit faster than methods using can(), since inheritance is not checked.

    Cheers,
    Paul

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-20 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found