Knowledge Base


All content on this website is © Copyright 2000-2010 - All Rights Reserved
The content on this site may not be reused or republished.
Website template powered by VooWeb.com Website Templates

Authoring/Development - Debugging Perl Scripts

This tutorial is meant as a general outline for easing the strain of debugging Perl CGI scripts. It is more concerned with syntax errors than logical ones. A familiarity with SSH is assumed, although you can use most of the techniques locally on your code, if you have Perl installed on your own machine (there are versions for most operating systems).

"403 Forbidden Errors"

A 403 Forbidden error occurs when the Web server finds itself with insufficient permissions to run your script. Generally speaking, there are two steps to be taken when it occurs:
  1. First, make sure you have given the script execute permissions. Via SSH, this is accomplished with:

    chmod 755 scriptname.cgi

    (See our File Permissions tutorial if you are not familiar with chmod.)

    If you are uploading via FTP, your FTP client probably has some facility for setting file permissions. You want to make sure that all choices (user, group, and other) have execute permission.

  2. If your script does not have a .cgi or .pl extensions, it must be placed in the cgi-bin directory to be considered executable. If your script does not have one of these extensions, either rename it, or move it into the cgi-bin directory.

"500 Internal Server Error"

The "500 Internal Server" error is likely to be the most common error message you will see while debugging your scripts. Unfortunately, it is also a very generalized error message, which reveals little other than that there is some problem with the script. To locate the problem or problems, a systematic approach is needed.

First and foremost, you should take care to always do the following:

  1. If you are uploading the script to your server via FTP, always make sure you have done so in ASCII format. If your FTP client autodetects, make sure it knows that .cgi and .pl files are ASCII files. Uploading in BINARY format will cause problems.
  2. Make sure the first line of your script is the path to Perl, and that it is correct. In the vast majority of cases, it should look like:

    #!/usr/local/bin/perl

    That is the path to Perl 5. In the rare case you require Perl version 4, use #!/usr/bin/perl4 instead.

The next few techniques are all accomplished via SSH. We highly recommend the use of SSH while debugging scripts, as it makes it much easier to make small changes to the script, along with offering the ability to run the script from the command line.

First, use Perl's -c option from the command line to see if there are any syntax errors in the file, like this:

perl5 -c scriptname.cgi

This is the fastest way to find where the errors are in your file, and fix them. If you find errors, keep correcting them and rerunning "perl5 -c" until they are gone. That's usually easier said than done, but generally Perl reports the line number with the error to you. (An annoying exception occurs when you omit a left or right bracket. In such cases, the interpreter can get much farther in the script before the error occurs, reporting a certain line number when the missing bracket might actually be much earlier in the script.)

Next, run the file and examine its first two lines of output. This might be done with:

perl5 scriptname.cgi | head -2 

The first line should be a content-type, most likely one of these:

Content-type: text/html
Content-type: text/plain

The second line, unless you are sending other headers like "Expires" or setting cookies, must be a blank line.

Every CGI script must first output its headers (starting with Content-type) followed by a blank line, or a 500 error will result.

Another good technique to use disabled buffering of output, and write a content header as soon as the program starts. In this way, you can redirect the error to your browser screen.

After your path to Perl, add the following two statements as the second and third lines:

$| = 1;
print "Content-type: text/plain \n\n";

This forces all output to be sent to the browser immediately, and to be considered plain text. It should allow you to see the error Perl is returning directly from your Web browser.

Conclusion

No matter what method you choose, you may need to place print statements in strategic locations, to see how the flow of the program is working. You might add one inside an if/then statement for instance, so that you can see if the statement is being evaluated as true or false.

By practicing all of these techniques, debugging Perl CGI scripts can be made a little easier and less frustrating as well.

Please provide feedback on this article.




 
Related Links
» The Basics
» Frequently Asked Questions
» E-Mail
» E-Commerce
» Promotion
» Advertising on Your Site
» Glossary
» Authoring/Development
» About Your Account
» Our Network and Servers
» Billing

» Contact Tech Support

« Knowledge Base Home


 
 
pair Networks - World Class Web Hosting Collections

All content on this website is © Copyright 2000-2010 - All Rights Reserved
The content on this site may not be reused or republished.
Website template powered by VooWeb.com Website Templates

dhhzp.com v 4_3