[sf-perl] Undefined Soubroutine

Quinn Weaver quinn at pgexperts.com
Mon Oct 17 12:15:41 PDT 2011


On Oct 17, 2011, at 11:42 AM, Richard Reina wrote:

> I have a module called Fax.pm that submits faxes to a local fax server. It is called by another program called User_utilities.pl.  Everything has worked great for years until recently when I added the following lines to Fax.pm
> 
> require "/usr/local/src/rushclient/rush_opps/get_hist.pl";
> my $hist = get_hist();
 ^^^^^^^^^^^^^^^^^^^^^^
Is this line 130?


> The problem is that sometimes I *SOMETIMES* I get:
> 
> Undefined subroutine &Fax::get_hist called at Fax.pm line 130.
> 


> After getting this error if I restart User_utilities.pl and try again it works. However, if User_utilities.pm is left running and I try to use Fax.pm again, I get the error again.
> 
> Does anyone know what might be causing this error and what I might do to fix it?

In general, the problem is that 'require' happens at runtime, and can be conditional, so you have to be really careful. If any other code in your program calls get_hist() before Fax.pm hits that 'require' call, then get_hist will not be defined yet and you'll get this error.

The best solution is to convert get_hist.pl to a module and 'use' it instead of 'require'ing it. Then the subroutine will be defined at compile time and ready for code anywhere in your program to call it.

--
Quinn Weaver
PostgreSQL Experts, Inc.
http://pgexperts.com/
1-888-743-9778 (my extension: 510)



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/sanfrancisco-pm/attachments/20111017/0dce24a4/attachment.html>


More information about the SanFrancisco-pm mailing list