Perl: Practical Extraction and Report Language


Perl is a very powerful and popular language for writing Web Server-side CGI scripts and for system administration.  It is free and available in UNIX, PC, and Mac platforms.

We will use  Robert Pepper's  tutorial on Basic Perl programming for Perl and Lincoln Stein's CGI module for using Perl in Common Gateway Interface (CGI) programming.  The following is the set of related links.

Setup your cgi-bin directory on CS Unix Machines

Quick Summary of Perl

Setup: Editing and Running Perl

Literals

Variables

Operators

Selection Structures

Iterative Structures

Associate Array and DBM

Subroutine

I/O

Split

Regular Expression for pattern matching

CGI

Debugging CGI Scripts

Common Programming Mistakes:

Common but Tricky End-of-Line Problem when transfers cgi scripts from Window to Unix:

-----Original Message-----
From: CS301 Web Programming Mail List [mailto:cs301-l@uccs.edu] On Behalf Of Edward Chow
Sent: Sunday, October 03, 2004 12:42 AM
To: CS301 Web Programming Mail List
Subject: [cs301-l]Re: CGI error

Heidi,

This is a tricky but common error!

If you use vi to edit your file, you will find at the bottom of the display there is an indication of "dos" file.
If you ftp the catalog.cgi file from home PC, the dos file will contain additional character at the end of each line.

I use the bigend program I wrote to display the byte content of your catalog.cgi

sanluis.uccs.edu> ~cs520/bin/bigend catalog.cgi
argv=catalog.cgi
LOC 0: 23 21 2f 75 73 72 2f 62 69 6e 2f 70 65 72 6c 0d "#!/usr/bin/perl.p B"
LOC 16: 0a 0d 0a 75 73 65 20 43 47 49 20 71 77 28 3a 73 "...use CGI qw(:sp B

It shows after "perl" which is 70 65 72 6c in hexa decial, your dos file has 0d 0a (carriage return, line feed) for the end of line.
The next line is empty therefore we saw another 0d 0a follows.

If I switch to ~cs301/public_html/cgi-bin/catalog.cgi

sanluis.uccs.edu> ~cs520/bin/bigend catalog.cgi
argv=catalog.cgi
LOC 0: 23 21 2f 75 73 72 2f 62 69 6e 2f 70 65 72 6c 0a "#!/usr/bin/perl.p B"
LOC 16: 75 73 65 20 43 47 49 20 71 77 28 3a 73 74 61 6e "use CGI qw(:stanp B"

You will find there is only one 0a for representing the end of line, not 0d 0a. My catalog.cgi file is created on Unix machine.
The Window and Unix have different ways to representing the end of line.

Why this is causing the problem? Why the CGIWrap complains that it can not find the file or the Perl interpreter?


The reason is that CGIWrap or the web server will treat the catalog.cgi as a Unix file (since the web server is run on a Unix machine) and try to read the first line. It finds the first two characters is #! and understand it needs to find a shell program or a Perl interpreter. The file name of the shell or the Perl interpreter is followed #! Until 0a (line feed). Since your dos catalog.cgi has 0d (carriage return) before 0a. The cgiwrap will consider 0d as the last character of the file name, when it tries to find /usr/bin/perl(0d) with 5 character as its file name, it will not be able find any file there. The perl interpreter in /usr/bin directory only has 4 characters as its file name.

The error message generated by CGIWrap actually is pretty good. It indicates this could be caused by uploading file. But it says control-M was at the end of the first line, instead of 0d for ascii code.

How can we fix the problem?

The best way is to start vi in binary mode and remove the 0d carriage return character of your dos catalog file.

One of the old way I used is to

The CGIwrap error message suggests change the ftp transfer mode from binary to ascii. But I found it does not work. The od in the DOS end of line was not removed by ftp.

Edward

-----Original Message-----
From: Hparsaye@aol.com [mailto:Hparsaye@aol.com]
Sent: Saturday, October 02, 2004 9:51 PM
To: chow@cs.uccs.edu
Subject: Re: CGI error

Dr Chow,
I put -- at the end of the first line to see what is going to happen and make sure perl will see my file, but with out it I get this error:

Thanks
CGIWrap encountered an error while attempting to execute this script:
Error Message: No such file or directory
Error Number: 2
This message usually indicates there is a problem with the script itself. Often this indicates either that the #! line of the script is incorrect, or the script was uploaded in binary mode instead of ascii mode. Check to make sure that the script does not have control-M's at the end of every line. That will prevent it from executing. An easy fix that takes care of this most of the time is to put '#!/.../perl --' instead of '#!/.../perl' on the first line of the script. This is typically a problem if the script was edited or uploaded from a DOS/Windows/Macintosh station to a unix based server.
If you are not the owner of this script, please forward this error and the URL that caused it to the script owner. That is often the component in the URL right after /cgiwrap/.