Web Development Support
    General Info   Cool Tools
    Getting Started   Troubleshooting
    Design & Promotion   Feedback
 

Search this site:


view site map

Diagnosing problems with Perl scripts

  1. Overview
  2. Is the script executable?
  3. Are the permissions on the directory containing the script correct?
  4. Does the script have UNIX line endings?
  5. Which Perl are you using?
  6. Does the script compile error free?
  7. Will the script execute from the UNIX command line?
  8. Is the script using the correct version of Perl?
  9. When the script executes, does it have the correct header, and make reasonable HTML?
  10. Script examples in Perl (with some C code)

Overview

If you are having trouble getting a perl script to run, these 6 basic tests are a good place to start. You'll need a passing familiarity with UNIX to make all this work, and in some cases you'll need admin privs to login as the user who is having problems. We will use the fictitious filename myscript.pl where a Perl script name is required in the following examples.


Is the script executable?

A Perl script needs to have the x bit set for user, group, and other. The commands I enter are in red.

node16 $ ls -l *.pl
-rw-r--r-- 1 mst3k staff 667 Sep 10 12:41 myscript.pl
node16 $ chmod +x myscript.pl
node16 $ ls -l *.pl
-rwxr-xr-x 1 mst3k staff 667 Sep 10 12:41 myscript.pl*
node16 $

The +x is a shorthand for ugo+x


Are the permissions on the directory containing the script correct?

For scripts running out of the public_html directory they must be in a directory which has write permissions for only the user (site owner).

Bad permissions (assuming you have used cd to get to the directory containing the script)
node16 $ ls -ld .
drwxrwxr-x 1 mst3k staff 40 Sep 10 12:41 .
node16 $ chmod og-w .
node16 $ ls -ld .
drwxr-xr-x 1 mst3k staff 40 Sep 10 12:41 .
node16 $

 

Using '.' in ls and chmod command is a quick way to refer to the current directory.


Does the script have UNIX line endings?

Is there a ^M at the ends of the lines? Use jove to check this out. If the first line resembles the following, then the script will not run.

#!/usr/bin/perl^M

In order to fix this incorrect line ending, you will need to use the UNIX flip utility to clean up the problem. Enter the following at the UNIX command line:

flip -u myscript.pl

where myscript.pl is the name of the file. However, I've seen cases there the line ending is only a ^M and then you'll have to use a text editor like Jove or emacs to substitute all the ^M with a ^J. I like to use the query-replace command, and to get either editor to understand that you mean to enter a ^M or ^J, you must first do ^Q. The ^Q "quotes" the following character. In Jove do this:

open the file (^X^S then enter filename)

ESC-Q, ^Q^M, <press enter>, ^Q^J, <press enter>, <press y a couple of times>, <press !>

You press Y to do a couple of substitutions. If it is substituting correctly, enter a ! and it will do the substitutions for the rest of the file without prompting you. Save the file with ^X^S.

If you are using my .emacs file, this works exactly the same. If not, you might not be able to see the ^M characters, and the query-replace is ESC-%.


Which Perl are you using?

Some scripts just assume that the default Perl interpreter is Perl version 5. Unless the documentation specifies which version is required, you'll have to ask a Perl expert to look at the code. There may also be problems about the location of the Perl interpreter.

Either of these UNIX commands will show you the location for the interpreter that is called automatically whenever you use Perl on the command line:

whence perl
whereis perl

U.Va. keeps perl in several places, although there are symbolic links that may disguise this. When you use perl on the command line, you may be getting a different version than you expect. The absolute path may be one of these:

/usr/bin/perl
/uva/bin/perl
/usr/local/bin/perl
/uva/bin/perl5
/usr/local/bin/perl

If you wish to use a simple invocation frequently, you should modify your PATH environment variable. For example, if you want to always get /uva/bin/perl whenever you call Perl, you would need to edit your .variables.ksh file, inserting /uva/bin before other directories in your PATH as follows:

PATH=/uva/bin:$PATH


Does the script compile error free?

To find out if the script compiles error free, enter the following at the UNIX command line:

perl -w myscript.pl

or

perl5 -w myscript.pl

where myscript.pl is the name of your script. Any errors must be fixed. There are some kinds of errors that are not detected by this process. Check the preceding section to make sure you're using the version of Perl that you want.


Will the script execute from the UNIX command line?

Does the Perl script really run? Try running it at the command line by entering the following:

myscript.pl

or

./myscript.pl

The script should run with no errors being reported.


Is the script using the correct version of Perl?

The first line of the script calls the interpreter. It begins with what is called a "shebang", the #! string of characters. Immediately following (no space) is the path to the interpreter called, for example

#!/usr/bin/perl

When used on the command line (as in "perl myscript.pl") the shebang is overruled.

To know for sure what interpreter is being called, use an absolute path, e.g. /uva/bin/perl, in both the command line and the script's shebang line.


When the script executes, does it have the correct header, and make reasonable HTML?

There are two things to look for.

1) http header
2) some reasonable looking HTML output.

The http header is at least one line, and might optionally be a few lines. The http header must contain, as the last line before the HTML output exactly this:

Content-type: text/html

It must be followed by a blank line. Here is the first few lines that come up if you run the test.cgi script, with what I typed in red as usual:

node16 $ test.cgi
Last-Modified: Thu, 01 Jan 1970 00:00:00 GMT
Content-type: text/html

<HTML>
<HEAD><TITLE>Test script</TITLE></HEAD>
<BODY><H2>CGI test script.</H2>
<a href="test.txt">Get the source for this script.</a><p>
<a href="http://www.itc.virginia.edu/desktop/web/ssi_cgi.html">CGI and SSI notes.</a>
<p>
<HR>
Results of the UNIX date command<br>Fri Sep 10 15:32:53 EDT 1999
<BR>
<HR>

...

In this example the http header is two lines plus a blank line. The Content-type line is required, and the blank line following it is required. Then the HTML begins. I don't show all the HTML in this example. If the HTML is bad (contains incorrect tags, unclosed tags, etc.) then the page still won't display correctly in the browser.

There is an alternate possibility to the Content-type, and that is the Location http header which is used to perform a type of redirect:

Location: http://www.virginia.edu/news.html

This must also be followed by a blank line. Incidentally, there are two other types of redirect which are more common: server redirect and client pull (aka http-equiv, or refresh ). Client pull is done in the <head> section of a Web page, so it doesn't require external data or configuration. It has a problem in that the back button tends not to work with this type of redirect.

<META HTTP-EQUIV="Refresh" CONTENT="?; URL=URL">

A client pull after zero seconds to the U.Va. news site is as follows. The format of the CONTENT attribute is unusual.

<META HTTP-EQUIV="Refresh" CONTENT="0; URL=http://www.virginia.edu/news.html">


Script examples in Perl (with some C code)

The best way to get started with CGI scripting is to get the Perl book with the camel, and install some of our scripts for study. Modifying a working script is far, far easier than writing a script from scratch. Most of the following packages include a C program in the package if you install with the -s option. On some systems Perl cannot write files due to it's inability to deal with a certain file permission issue. The C program is pretty simple and exists only to write files for the Perl scripts. The C program is very specific, and can only write certain files an only under certain conditions.

test.cgi - This is a small script which we recommend you put on your site if you have any problems. This script is known to run on U.Va. systems, and prints out valuable diagnostic information. The script is more or less self documenting. Great feature: use this script as the action in a form, and it will print out all your fields and their values - automatically! This script also will print out its own source, and is therefore available (unsupported) outside U.Va.

Easyform/Easymail Documentation - See how we collect data from an HTML form via CGI, send e-mail, write files. The form handling is generalized, elegant, and based on some intimate knowledge of the inner workings of CGI. It's simple, but understanding why it works may take some study. Source includes comments. For use outside U.Va., this is unsupported script is available in an tar file easyform.tar

LSearch Documentation - This small search engine with a site editor shows how to do forms, write and edit data files, and even includes a very rudimentary password check. Uses a template HTML file to simplify coding, and implementation. For use outside U.Va., this is unsupported script is available in an tar file lsearch.tar

VForum Documentation - A simple forum (quasi-chat) program. Multiple form handlers, generates HTML pages from saved pages. Generates links on the fly. Multiple forms interacting. For use outside U.Va., this is unsupported script is available in an tar file vforum.tar

Guestbook Documentation - Data collected from a simple HTML form, written into an HTML file for guests to read. Source comments are sparse, and some of the coding is not really in a recommended style. For use outside U.Va., this is unsupported script is available in an tar file guestbook.tar

 


Web Development Support | General Info | Getting Started | Design & Promotion
Cool Tools | Troubleshooting | Feedback

If you need further assistance with Web applications or questions, send e-mail to web-consult@virginia.edu or call the ITC Help Desk at 924-3731.

© 2009 by the Rector and Visitors of the University of Virginia.

The information contained on the University of Virginia’s Department of Information Technology and Communication (ITC) website is provided as a public service with the understanding that ITC makes no representations or warranties, either expressed or implied, concerning the accuracy, completeness, reliability or suitability of the information, including warrantees of title, non-infringement of copyright or patent rights of others. These pages are expected to represent the University of Virginia community and the State of Virginia in a professional manner in accordance with the University of Virginia’s Computing Policies.