| CGI & Perl > General
What is Perl?
Perl is a high level programming language based on 'C' programming, and originally developed by a computer programmer named Larry Wall.
There isn't time or space to take this subject into 'depth' but the following may help you to understand a little about what goes on, and how to use cgi.
Perl files are really scripts that are read and interpreted by a Perl interpreter that resides on the main web server, and strictly speaking not a true 'program' in it's own right.
Naming Convention:
Perl scripts are normally named something like myscript.cgi or myscript.pl
The first one, myscript.cgi is the most usual... .pl filenames are conventionally reserved as Perl Library scripts which are still scripts, but contain useful functions or common 'routines'.
Putting convention aside... If you do want to run scripts with an extension of .pl
- e.g. somescript.pl then it will still run (execute) on the server exactly the same as a file ending with .cgi
What is CGI?
CGI stands for Computer Gateway Interface
and is a mechanism that allows you to create what is, in effect, a program on the web server.
CGI programs can process forms, make calculations, process shopping cart information, and a welter of user interactive online applications.
Writing a 'simple' CGI application is fairly easy and straight forward... Click here for examples
A simple cgi script example....
##### A simple 'Hello World' cgi application #####
#!/usr/bin/perl
print
"Content-type: text/html\n\n";
print
"Hello World";
exit
0;
The code works like this....
The line
#!/usr/bin/perl is a reference to the location of the Perl interpreter on the web server, and must be the first line on your perl or cgi script.
This is commonly known as The Path to Perl
The line print
"Content-type: text/html\n\n"; tells Perl to print what is inside the speech marks to a blank web page. This line tells the web browser that will receive the page to expect either a text or html document.
The line print
"Hello World"; tells Perl to print (write) Hello World to the web page.
The final line exit
0; tells the Perl interpreter that the script ends at this point.
Before a script can 'run' on the server it has to be CHMOD'd to make it executable.
CHMOD
CHMOD is a mechanism that sets the access permissions for a file on a web server (normally CGI).
The 'normal' access permission for cgi files is 775 which means....
Read access for - Owner, Group, World
Write access for - Owner, Group
Execute access for - Owner, Group, World
With file permissions set to 775 a cgi script will run successfully... Assuming there are no coding errors!
To CHMOD a file using the ftp application WS_FTP You right click on the file (on the server window), and select CHMOD from the menu, then check every checkbox apart from Write(other).
The WS_FTP ftp application is available in our download area.
De-Bugging your CGI scripts
It's often frustrating to write a CGI script and find that it doesn't work on the server... Returning an http 500 error, which isn't very useful in telling you what went wrong!
The first thing to check is... Did you upload the script in ASCII
(text) mode?
If you didn't, then you are guaranteed to get an error.
Remedy:: Re-upload the script, making sure it is uploading in ASCII mode.
The second thing to check is... Have you made the script executable by
CHMOD'ing it?
If not, then CHMOD the script. goto CHMOD
Assuming the above are all OK.
Does your script print " Content-type: text/html\n\n";
before it tries to write anything else to a web page? This is the commonest reason for
getting an 'undefined' script error.
We have a cgi script de-bugging application running on our servers that will help you to work out what's going wrong.
To de-bug (test) a cgi script on the server you need to put a copy of the script in the same directory but named scriptname
.cgid (scriptname
is the same name as the script you are de-bugging)
Then instead of calling (running) scriptname.cgi, run scriptname.cgid instead. This will run both scripts in parallel and produce an output to the browser which tells you what the script is doing, and will often tell you what is failing to work as expected.
The de-bugger isn't totally 'foolproof' but may help you to work out the problem. Path to Sendmail
The path to SendMail is
/usr/sbin/sendmail
FormMail
FormMail is a generic HTML form to e-mail gateway (CGI) that parses the results
of any form and sends them to the specified users.
This script has many formatting and operational options, most of which can be
specified within each form, meaning you don't need programming knowledge or
multiple scripts for multiple forms.
This also makes FormMail the perfect system-wide solution for allowing users
form-based user feedback capabilities without the risks of allowing freedom of
CGI access.
Any users who have versions of FormMail prior to v1.91, including the popular
version 1.6, should upgrade immediately. v1.91 plugs several more spam-related
security holes. The following fixes have also been implemented since v1.6:
prevents unwanted access to environment variables and problem of receiving
e-mail while using the redirect option. The script has two extra arrays (new in
v1.7) you must define, but will not affect current forms or the way they appear
after having been submitted.
The latest FormMail script can be downloaded from Matts
Script Archive
|