• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 856
  • Last Modified:

Perl CGI issues "Undefined subroutine" error

My Perl CGI sometimes prints out the expected HTML. Other times, it issues a "500 Internal Server Error". When this happens, the following message is printed to Apache's error.log:

[Fri Sep  3 19:20:00 2004] [error] Undefined subroutine &Apache::ROOT::about_2ecgi::myHeader called at /var/www/about.cgi line 4.\n

Below is the source code of the scipt (about.cgi). You may find a rendered version of it at http://metagloss.gotdns.com/about.cgi. (Or, you may receive an internal server error.)

use common;

&myHeader;  #this is line 4

print <<EOF;
<font size=5 face="Verdana" size=5></font>
<br><br>Under Construction<br><br><br>


-------------common.pm (called by about.cgi)--------------------------

use CGI qw(:standard escapeHTML);

return 1;

sub myHeader
print header();
print start_html("MetaGloss.org");

print <<EOF;
<body bgcolor="#eeeeee" marginwidth="0" marginheight="0" leftmargin="0"
        topmargin="0" text="#000000" link="000066" vlink="000066">
<table width=843px cellpadding=0 border=0>
<tr><td><img src="/images/metagloss5.jpg"></td></tr>
<tr><td><table cellpadding=0 border=0>
<tr><td width=80px bgcolor="#bbbbbb"><br /><br />
&nbsp;&nbsp;<a href=index.cgi>search</a><br /><br />
&nbsp;&nbsp;<a href=about.cgi>about</a><br /><br />
&nbsp;&nbsp;<a href=statistics.cgi>statistics</a><br /><br />
&nbsp;&nbsp;<a href=submit.cgi>submit</a><br /><br />
&nbsp;&nbsp;<a href=help.cgi>help</a><br /><br />
&nbsp;&nbsp;<a href=contact.cgi>contact</a>
</td><td width=763px bgcolor="#dddddd">

sub myFooter

print <<EOF;
<center><br /><br /><br />
<img src="/images/cat1.gif">

What part of my script could cause an "Undefined Subroutine" error to occasionally occur? (It does not always occur.)  I would very much appreciate any advice.
  • 3
1 Solution
Hi bobwood2000,

The reason why it's undefined is that the function's not exported.  You have to declare common.pm to be of Exporter type.  I'll give you a working example later.


Add these lines immediately after use CGI... in your common.pm:

use vars qw(@ISA @EXPORT);
@ISA = ("Exporter");
@EXPORT = qw(myHeader myFooter);

I tested it and everything worked fine.
bobwood2000Author Commented:
Success! Thank you ing123!
Thanks for the points!  I'll leave it up to mod about how to deal with more than 500 points then.  BTW, again, if you want the points for one question back you can ask in community service.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now