--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
WHAT IS A CGI?
CGI stands for common gateway interface. Essentially, a CGI is just a program which runs on your server. It can be written in any programming language, so long as you can run it on your server. Perl has become a popular choice for CGI programming because it is available for all platforms, and it has many useful tools that are ideal for the web.
When I fill in a form on a web page and press 'Submit', 2 things happen on the server.
Everything I typed or selected is copied to the server.
The server looks for the program specified at the top of the form, and runs it.
The first thing the program will usually do is request the contents the web page form, and assign it to variables which it can use. Then the program is able to search a database or send an e-mail or add to a guestbook, or whatever you would like it to do.
As with the JavaScript tutorial, this tutorial will teach by example. For each new topic, we will examine the entire code, then break it down into small pieces and discuss the relevance of each line. Before you can use Perl on a Unix server, however, you will need to know a little bit about Unix.
THE UNIX ENVIRONMENT
You must have the ability to Telnet to your Unix server in order to create CGIs with Perl. Click here if you do not know how to do this.
You must have the ability to FTP documents to your server. You probably have already done this, as it is necessary to publish anything on the web.
There will be times when using Unix when it will seem that we have traveled 20 years back in time. The editors and commands are typed on a command line, as we did with DOS. Actually, Unix is by far the most powerful and flexible operating system available. When used at a local terminal, users use a graphic interface called X-Windows. X-Windows is too slow to use over the internet, however, so we will use the command line for the few Unix commands we need.
The command Prompt
Every time you enter a new line into Unix, you will get a command prompt. It often appears as your machine's name followed by a % or # symbol. This is where you enter your basic commands to create, copy, move, and delete files, among other things.
Click here for a list of basic Unix commands.
Print it out, and keep it close at hand.
Try this exercise:
Open your favorite Mac or PC word processor. As with HTML, use a simple editor which can save your code as plain text. Type the following line: This is a dummy sentence.
Save the file as practice.txt
Use your FTP program to upload practice.txt to your server. NOTE: You should always upload your cgi files as ASCII, not binary.
Use your TELNET program to access your web server.
LOGIN with your user name and password.
Type ls and press [ENTER] (NOTE: use lower case, UNIX is case sensitive) The contents of your web server should list on the screen. You should see practice.txt in the list.
Type pwd and press [ENTER]. pwd stands for 'Print Working Directory'. The path from your server's ROOT directory to your current directory should be displayed. For example, my home directory is at /home/usr/robyoung/htdocs
Type mkdir myfolder and press [ENTER]. You have just created a new directory (folder) called 'myfolder'
Type mv practice.txt myfolder/practice.txt and press [ENTER]. This will move practice.txt into myfolder.
Type cd myfolder and press [ENTER]. You have changed to the myfolder directory
Type pwd and press [ENTER]. The path name now ends with /myfolder
Type ls and press [ENTER]. The contents of myfolder is displayed. practice.txt is the only thing in it.
Type cp practice.txt copy2.txt and press [ENTER]. You have made a copy of practice.txt called copy2.txt
Type ls and press [ENTER]. 2 files are now listed.
Type rm copy2.txt and press [ENTER]. copy2.txt has been deleted.
Type ls and press [ENTER]. Only practice.txt remains.
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
The chmod command
When you create your CGI programs, you will need to change the access privileges to that they can be executed from a web page. To do this, you will use the chmod command. The chmod command has 2 properties, who will have privileges, and which privileges will they have.
1. Who will have privileges?
There are 3 categories of who can have privileges, the User, the Group, and Others.
They are represented by the letters u, g, and o. Also the letter 'a' can represent All the categories together.
2. What privileges can people have?
There are also 3 types of privileges: Read, Write, and eXecute, represented by the letters r, w, and x.
Privileges are assigned with an equals sign (=r, or =rw, or =rx), or added with a plus sign (+r, or +rwx), or subtracted with a minus (-w, or -rwx)
3. How do I type the command? For most CGI programs, I want to give read and execute privileges to the world, but not write, so that no one can delete or edit my program. This is the most common command:
chmod a+rx filename.cgi
This means add the ability to read and execute to everyone. When I first create a file, I have full read, write, execute privileges, but no one else has anything. This command gives the world the ability to run my program.
SHORTCUT: You can also type a single digit number for user,group,others.
Add these values for each usertype:
Read=4
Write=2
Execute=1
To allow yourself all privilages, and everyone else can read and execute (most common)
chmod 755 filename.type
To allow everyone all privilages use: 777
To allow yourself to read and write, and other can only read: 644
Try this example. You are still in the myfolder folder.
Type ls -l and press [ENTER].
A lot of information is displayed about practice.txt, including its size, its owner, and date last saved.
Observe the access privileges, they may read rwx------ or perhaps rw-r--r-- or rw-r-----
The first 3 places indicate the user privileges (read,write,execute). The second three are the group privileges (none) and the third three are the other privileges (none).
usr|grp|oth
Type chmod a+rx practice.txt and press [ENTER].
To add read and execute privileges to all categories:owner, group, and other.
Type ls -l and press [ENTER].
This time the privileges should read rwxr-xr-x , indicating that the owner still has full privileges, and that group and others have read and execute, but not write.
Type chmod go-x practice.txt and press [ENTER].
Type ls -l and press [ENTER].
The privileges now read rwxr--r-- , indicating that the owner still has full privileges, and that group and others have only read privileges.
Remove the myfolder folder
Type rm practice.txt and press [ENTER]
Type cd ..
Type rmdir myfolder
Try this matching game
1. Add Execute to Group users
a) chmod g=rw filename.txt
2. Remove Write from everyone
b) chmod o-r filename.txt
3. Everyone has only Read and Execute
c) chmod a+rx filename.txt
4. Add Read and Execute to everyone
d) chmod g+x filename.txt
5. Remove Write from others and group
e) chmod a-w filename.txt
6. Remove Execute from user
f) chmod a+r filename.txt
7. Group has only Read and Write.
g) chmod go-w filename.txt
8. Add Read to everyone
h) chmod a=rx filename.
txt
9. Remove read from others
i) chmod u-x filename.txt
Here is the answer key:
1 - d
2 - e
3 - h
4 - c
5 - g
6 - i
7 - a
8 - f
9 - b
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Locating the Perl program
The first line of every Perl program gives the location of Perl on the system. This is so a command from an outside web page will instantly know what program to execute, and where to look for it.
From the command prompt, type which perl and press [ENTER].
Hopefully, the system returned a line similar to this:
/usr/bin/perl
or
/usr/local/bin/perl
Write down the result. This is the location of Perl on your system, and you will need it.
If the system did not return a result, try this:
Type whereis perl and press [ENTER].
Again, you are looking for a path similar to the ones described above. You may get several results, but only one should end with just perl and nothing else. Write down the result if you get one.
If you still do not have the path to Perl, you will need to e-mail your system administrator. Finish this page before doing this because you may need to ask for more than one thing.
Locating the sendmail program
Type whereis sendmail and press [ENTER].
Hopefully you will get several results, and one of them will end with just sendmail. For example I get this result on my system:
sendmail: /etc/sendmail.cf /etc/sendmail /etc/sendmail.st /usr/lib/sendmail.no_mx
/usr/lib/sendmail.mx /usr/lib/sendmail /usr/lib/sendmail.hf /usr/man/man8/send
I know that everything good is in the /usr/ directory, and the only one of those which ends with just sendmail is this:
/usr/lib/sendmail
So that's what I will write down.
If you had problems locating either the Perl program, or the sendmail program, you should e-mail your system administrator and ask for the correct paths.
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Using Emacs
Why on earth would I want to use a text based editor, when I have nice GUI mouse oriented editors on my PC? The answer is Emacs runs on your Unix platform, so you can make minor changes easily. Basically you have 2 choices for maintaining your works in progress on a Unix platform over the internet.
1. Create the file using a fun, graphic, mouse friendly text editor on your PC. Then FTP the file to your Unix server. When you make a change, save your changes and FTP it again. This method has the advantage of allowing you to do all your work in a comfortable, mouse oriented editor. The disadvantage is the amount of time spent uploading the file with every minor change.
2. Learn a Unix editor, such as Emacs, and save your changes right to the server. It's not pretty, and you can't use a mouse, but it can do everything you want it to do once you get used to it. I would much rather use Emacs to make a minor adjustment than upload my changes every time.
NOTE: I actually use an editor called Pico. Like Emacs, it runs on the Unix platform, so I can make changes directly on my server. It is a little bit easier to use and the command menu is always at the bottom of the screen. Type pico filename to open a file with Pico.
To launch the Emacs tutorial:
Type emacs and press [ENTER].
The screen will change to display the emacs editor environment. If your server says 'Emacs not found' then you should e-mail your system administrator and ask what text editors are available, and is there a tutorial online?
Type [ctrl]-h and then press t.
This should launch the tutorial. You will want to spend at least a couple of hours with the Emacs tutorial. The better you know Emacs, the easier your Perl editing will be down the road. Don't rush, this tutorial will still be here when you are ready to continue.
Tips:
Save a file by typing [ctrl]-x and then [ctrl]-s.
You can exit Emacs by typing [ctrl]-x and then [ctrl]-c.
Type emacs filename to open a file with Emacs.
You're ready to start working with Perl.