Phoenix.pm: Reason for finding defined sub

Beaves at aol.com Beaves at aol.com
Thu Jan 27 23:48:20 CST 2000


Here is the situation I had in which I wanted to know if a sub was defined or 
not.  Just in case anyone else runs into anything similar...

In this program, I have allowed two techniques for building a 'stored' 
FormVar object:  You can either define a sub by the name of 
'load_<formvarname>' or put the object in a file named <formvarname>.fv which 
will return the desired object.  The 'load...' way would take priority.

The flow would be:
    Use a sub by the name of 'load_<formvarname>' if it exists,
    otherwise, require the file <formvarname>.fv,
    otherwise, fatal error, "object not found".

Obviously, you can't just call the sub, or you'll croak if it doesn't exist.  
You could put it in an eval, and check the $@ for errors.  But it turns out 
that it isn't really all that difficult to check the symbol table.  The 
simple line below will suffice.

    &load_<formvarname> if defined &load_<formvarname>;

In fact, this example was there for me all the while in the explanation of 
the function 'defined' in the Camel Book.

That's it really.
    
Tim



More information about the Phoenix-pm mailing list