CGI or Common Gateway Interface is a VERY powerful and useful computer language. It allows for a web designer to add a great amount of user interactivity to his/ her web site, along with many useful functions, like forms, quizzes, polls, page builders, free for all link pages and more. Unlike java script, cgi process data much more efficiently, writes to files much better and runs separate from HTML (except withSSI).
The first thing you need to know about CGI/perl is whether your host allows it or not and if they require .cgi extension or .pl extensions or both. If your host is Geocities, AOL, Tripod, AngelFire, or one of many more, cgi will not work so don't try to use it. If your host does allow cgi, have them set up a cgi-bin or something like one for your account. After you have done that, email them asking for the path to your root directory (may be like /usr/ait/htdocs/yourname/public_html/). Also ask them for the path to your send mail program (like /usr/bin/sendmail) and for the path to your date program (like /usr/bin/date/). You will also need to location of your perl interpreter (like #!/usr/bin/perl).
The very first part of every web ready script is defining the location of the perl interpreter, which is something like #!/usr/bin/perl/ or something like that. This is followed by the beginning of the script it's self. A good script will have all variables and comments after this.
$variable?A varriable is a command that you will use to take data, write data and hold data with, it is a given value for something.
$variable = "line";
What this means is somewhere in the script it requires you to tell it what to do. For example if you were to have a program that prints your name it would ask you this...
$myname = "John";
And somewhere in the script is
print "My name is $myname\n";
The scripts see that it needs to print "My name is $myname\n"; and by defining $myname, it knows that $myname equals "John". The \n just tells the server to end the line.
Path and URLCGI very often asks you to specify the path or URL to an HTML or another CGI page. It's very important that you don't get these two confused.
A path is directly relative to the current document. This is the path allocated by your server. A path will not work in the web browser, just the script. An example is:
A URL, on the other hand, looks like this:
Other Important Codes
An array is used to keep and capture data, that will be used later in the script or from user information.
The chop function acts to remove the last character of a line form a data file or variable.
Printing informationSince the most common function in perl is printing information to the user, it becomes very important to know how to do it right,
The MIME (content) header is required whenever cgi prints out information. Without it, the browser would not know what it was displaying and would create a 500, server error).
print "Content-type: text/html\n\n";
Printing a standard line, with html code or without. Our first script also shows an example of printing
print "Hello, world!\n";
Your first script:
The first script we will show you is a simple script that prints one line of text. While it may seem simple, printint text is very important and necessary.
#first line ALWAYS goes to your perl interpreter
# hello.pl - a demo of a cgi that displays text
print "Content-type: text/html\n\n";
# the MIME content type
# The mandatory HTML head element
# and title element
print "Hello, world!\n";
# The print statement sends
# characters to the standard output
# with our actual message
The next script will be a little more complicated and list a few more variables, but does a lot more.
This script tells about the environment it is hosted on by using $ENV.
This next script uses one variable and a simple array to write to a file and performs some interesting
This next script is one that most people like a lot, it is a random fortune (magic 8-ball script).
It uses a pre made text file, a few variables and a lot of cgi to randomly pick what fortune to use.
(note this script may be downloaded at our download center).
This next script is a counter cgi, which is very popular and not that hard to set up. The main
difference with this script is that the web page is separate from the script and therefore you must
make a page separate to use the script. Remember to change all the variables.
The Perl Code
The HTML code:
That is the last script we will explain, continue reading through the above scripts until you understand how to use arrays, variables, other scripts and html in cgi. Below is some information on how to use cgi script and other cgi related stuff. Enjoy.
Defining variables: This is the next part of a script and probably the part people make the most mistakes on, when defining variables you need to remember that a path may not always be http:// or anything like that. Since servers run off of internal, hidden directories, cgi is able to run though your host. Make sure all your variables point to the right location, and the right files. Also make sure these files are properly configured and chmoded correctly.
Chmoding is an important part of cgi scripting that can not be forgotten. What is does is tells
the server how the script may be used and what may be done with it. Here are some common chmod commands.
After you have chmoded the files, changed the variables and uploaded them, it is time to run
the script. If you picked an essay script and did everything using the proper commands, ruining the script
will work just fine. If you get a 500 error (internal server error), check the variables, chmod
the file again and check the location of the perl interpreter. If it still does not work,
check the script again, try starting over from an unedited copy, then ask the place where you got
the script and/or your host.
Other things about CGI scripting: CGI scripting can be used for many things such
as processing forms, and other cool little tricks, even counters. There are a great
number of free scripts out there, we have made a few and added some free ones to our
script center. Check them out, try them out
and enjoy them. If you need more specific help, send us an email us.
After you have chmoded the files, changed the variables and uploaded them, it is time to run the script. If you picked an essay script and did everything using the proper commands, ruining the script will work just fine. If you get a 500 error (internal server error), check the variables, chmod the file again and check the location of the perl interpreter. If it still does not work, check the script again, try starting over from an unedited copy, then ask the place where you got the script and/or your host.
Other things about CGI scripting: CGI scripting can be used for many things such as processing forms, and other cool little tricks, even counters. There are a great number of free scripts out there, we have made a few and added some free ones to our script center. Check them out, try them out and enjoy them. If you need more specific help, send us an email us.