Home : Internet : Perl : Error 500

Internal Server Error in Perl (Error 500)

When a perl script fails to execute correctly you will usually see the standard error message:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

There are numerous potential causes of this error, the most common being:

Assuming you have set the correct permissions (usually 755 in *nix) and uploaded the file in ASII mode, you should suspect an error in the script. The problem is, the generic error message doesn't give you any clues. To find out what's wrong you need a specific error message. These are two common ways to do this:

  1. Look at the server error logs. You need to have access to these logs - most good hosting plans allow you to do this.
  2. Force the script to output the specific error message to the browser (instead of the generic error message).

(1) Viewing the Server Error Logs

The way you access your error logs will depend on your situation. If you have shell access, you may be able to view the logs through a command prompt. Otherwise, if you have some sort of control panel for your website you might be able to view the logs that way. Consult your hosting provider or server administrator for details.

The server error log contains a list of errors which have occurred recently, like the example below. This particular error tells you that the permissions have not been set correctly.

[2004-08-07 20:44:42]: error: file has no execute permission: (/home/username/public_html/cgi-bin/helloworld.pl)

(2) Output Error Messages to the Browser

An easier way to see the error message is to read it in the browser. To achieve this, insert the following line of code in the script:

use CGI::Carp qw( fatalsToBrowser );

The example script below, which contains an error, shows how to insert this line:

#!/usr/bin/perl
use CGI::Carp qw( fatalsToBrowser );
 
print "Content-type: text/html\n\n";
print "<html><head><title>Test Page</title></head><body>";
print "Hello World!"
print "</body></html>";

Now when you point your browser at the script, you see the following error message:

Software error:
syntax error at helloworld.pl line 7, near "print"
Execution of helloworld.pl aborted due to compilation errors.

This tells you that a syntax error occurred somewhere near the word "print" on line 7. If you look at the script you'll see that line 7 is okay, but the end of line 6 is missing the semi-colon. Fix this error and you're in business!