Diagnosing problems with Perl scripts
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. A Perl script needs to have the x bit set for user, group, and other. The commands I enter are in red.
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).
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^MIn 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-%. 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 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 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 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 <HTML> ... 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 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.
|
|||||||||||||||||||||||||||||||||