CGI Wysardry
Installing Perl CGI Scripts

How to find and trap errors in your Perl scripts.



Home / CGI / Error Trapping

Back | Next

Overview

If you don't have any form of error trapping in your scripts any errors will redirect you to the Spaceports main page.  This can be very confusing when trying to find the cause(s) of the problem.

Fortunately, there is something you can do to make bug tracking a little easier - tell Perl to print error messages.  I will show you two possible ways of doing that here.


Errors to browser

By inserting the following code in the second line of your scripts any internal errors will be printed to the browser window in the form of an error message.  Do not put anything before the line defining the path to Perl.


use CGI;
use CGI::Carp qw/fatalsToBrowser/;


If you still get redirected to the Spaceports page, then the problem can be (at least) one of four things:-

  • Incorrect path to Perl
  • Incorrect file permissions
  • Incorrect format when uploading
  • Incorrect filename or path when calling the script

Errors to a log

This one is a little more complicated, but it will allow you to have a text error log file for reference.  The main advantage is that it will log any errors that occur when your visitors try to access your scripts if you leave it in there after debugging.

  1. Connect to the CGI-BIN server via FTP
  2. Go to the public_html directory
  3. Create a directory called errors
  4. CHMOD errors to 777
  5. Create an empty text file called errorlog.txt
  6. Upload it to the errors directory
  7. CHMOD errorlog.txt to 777

Now, start all your scripts with the following code (replacing username with your username):-


#!/usr/bin/perl

BEGIN
{
use CGI::Carp qw(carpout);
my $errorlog = "/home/username/public_html/errors/errorlog.txt";
open(LOG, ">$errorlog") or die("Unable to open $errorlog: $!\n");
print LOG "Errors:\n";
carpout(*LOG);
}


Users who signed up for their CGI-BIN account after end July 2000 but before the middle of February 2002 will need to replace home with home2 in the code above.

Users who received their CGI-BIN account after the middle of February 2002 will need to replace home with home3 instead.

You will then be able to see your stored error messages by going to:-

http://cgi-bin.spaceports.com/~username/errors/errorlog.txt

If you are feeling a little more adventurous, you could use a separate log file for each script you use.  Remember there is a 1000 file limit on the CGI-BIN server though.

Site Menu

Sell t-shirts with your design ... for free!

Section Menu


Copyright © 2000-2003 Wysardry

Most recently updated on 11 May, 2003

Home / CGI / Error Trapping

Back | Next


Please feel free to contact us with any comments or suggestions
Installing Perl CGI Scripts
CGI Wysardry

This site is hosted by Spaceports