Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if a function exists 1

Status
Not open for further replies.

xaze

Technical User
Dec 2, 2002
8
US
Hello,

I have a database table set up (somewhat like this):

title, value, description, function, function_args

where function would be a function name and args would be constants or variable names

What I need to be able to do is be able to tell if the 'function' exists and then if it does get a reference to it so that I can call it. I would think this is a common problem but I have searched everywhere for the answer and cannot seem to find it. If you could shed light on how to do this it would be greatly appreciated.

-xaze
 
Do not bother answering, I figured it out. In case anyone else also needs this functionality then look over this small peice of code:

if (defined(*{${main::}{'my_func'}}{CODE})==1) {
&{*{${main::}{'my_func'}}{CODE}}
}

'my_func' is the name of the function. It first checks to make sure there is a symbol table entree with that name defined (and also that it is function and not another type of data) and then it calls the function.
 
You could have also done
Code:
sub AUTOLOAD { return undef }

my $myfunc = 'my_func';
if ( $myfunc->() ) {

    # print "Function exists!\n";

} else {

    # print "Function does not exist\n";
}
which is a little less notation-heavy.

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top