Laboratory Week 6 - Introduction to CGI programming with Perl
Learning Objectives:
- Learn to set-up CGI scripts so that they work on on a web server.
- Learn to access CGI scripts using web browsers and using HTML forms.
- Learn to write simple CGI scripts using Perl.
- Learn to manipulate data in Perl using variables and control structures.
Before doing the tutorial, please read:
- Week 5 lectures on CGI and Perl programming.
- Review what you learnt in the week 3 exercises on HTML.
Reference
At some stage after completing the instructions and exercises below, you should have a more careful look through the Web Programming textbook chapters 7 and 8, which gives much more details about Perl and CGI programming.
Keep in mind that there are many different ways to do things in Perl, and some of the techniques used in the textbook are slightly different to the ones in the lectures and the examples below. The reason I have not stuck to the textbook's approach only is because some of the examples are unecessarily complex for a beginners' course.
Software required:
- This lab requires the use of a SSH and an FTP program. The software is available in the internal labs. External students may download the required software from the unit's downloads page.
- This lab also requires a graphical web browser (like Netscape Navigator or Microsoft Internet Explorer), and a text editor (like Notepad).
Instructions:
Trying the examples from the lectures
- Use a web browser to try out all the example CGI scripts in week 5 lecture 1. Just type the given URLs as shown in the screen dumps.
- Unlike JavaScript, which can be loaded from a local HTML file and executed locally by the browser, a CGI script must be served by a web server. In this unit, you will be using the web server on red.murdoch.edu.au to serve your CGI scripts. You have already made use of red's web server to serve you HTML documents in week 3's exercises by putting them into a directory called public_html. For CGI scripts, you will need to put them into your public_html/cgi-bin directory. The Apache web server on red is configured to look in these directories.
Copy the sample code in week 5 lecture 1 and get them to work in your own directory on red. To do so, you will need to do the following for each of the scripts:
- Copy and save the scripts into text files. Give the files a .cgi or .pl extension.
- Use an FTP program to upload the file to the public_html/cgi-bin directory on your account on red. You should have learnt how to do this in week 3. Note that it must go into the public_html/cgi-bin directory, not just the public_html directory.
- You must set the file permission for the file so that the web server can execute it (this is called making the file executable). You need to TELNET or SSH to red as you did in week 3, and then use the chmod (ie "change mode") command. Follow the example below, but replace s202020A with your own student number, and the file name printname.cgi with the name of your file:
Red Hat Linux release 6.1 (Cartman)
login: s202020A
Password:
Last login: Mon Aug 7 08:27:13 from buss428.murdoch.edu.au
[hiew@red hiew]$ cd public_html/cgi-bin
[hiew@red cgi-bin]$ chmod 755 printname.cgiYou will need to do this only once for every new script you create.
- To access the script (either directly using a browser, or through the ACTION field in an HTML form), use the URL similar to the screen dumps in the lectures, except instead of using "www.it.murdoch.edu.au/~hiew/cgi-bin/...", use "red.murdoch.edu.au/~s202020A/cgi-bin/..." where s202020A is your student number.
Get each one of the scripts you tried in (1) to work in your own directory on red.Developing an example search engine
In your assignment 2, you will need to write a CGI script which implements a search engine. To assist you in doing so, the following is a set of example CGI scripts which takes you step-by-step from a very simple do-nothing script to a full search engine script that implements a lot of what is required of you in the assignment. There are comments in the script source code which describe how each version is an improvement over the previous version.
Source Code Working Example (accessed through HTML form) login1.cgi login1.html login2.cgi login2.html login3.cgi login3.html login4.cgi and passwd.txt login4.html login5.cgi and passwd.txt login5.html login6.cgi and passwd.txt
search.cgi and quotes.txtlogin6.html Make sure you understand every step from version 1 to version 6. Version 6 is a working search engine (although a very simple one).
To edit CGI scripts
To make changes to a CGI script locally:
- Use an editor like Notepad and open the script file locally.
- Once you have changed the script, save the file, and upload it to the server using FTP again. You do not need to do chmod anymore, unless the file name have changed.
Another possible way of making changes to the script is to edit the script file directly on the red server's file system. This will save you from having to FTP to the server everytime you make a change. This requires you to TELNET or SSH to red, and use an editor like pico or vi to edit the script file. You need to have some familiarity with the Linux command line to do this. If you do not know linux well enough, but would like to start learning it now, look at this introduction to linux commands. If not, I suggest you keep to using FTP while you are developing your scripts.
Exercises:
- Write a CGI script which prints out your name and student ID to the browser.
- Modify your script in (1) to store your name and student ID in scalar variables. Then prints out your name and student ID (exacly as in 1), but using the variables.
- Modify your script in (2) so that it stores a list of months in a year in an array. Have the script print out your name and student ID, followed by a list of the months (one month per line). Your output should look something like this:
Buffy Summers
s66666666
January
February
March
April
May
June
July
August
September
October
November
December- Modify your script in (3) so that besides storing a list of months in a year, it also stores the number of days in each month (ignoring leap years) in an hash. Have the script print out your name and student ID, followed by a list of the months and the days in each month. Your output should look something like this:
Buffy Summers
s66666666
January 31
February 28
March 31
April 30
May 31
June 30
July 31
August 31
September 30
October 31
November 30
December 31- Modify your script in (4) so that it only prints out the months in a year which has 31 days.
Remember to read the week's development on the Internet by scanning through relevant articles in the IT section of Tuesday's the Australian newspaper - use the online version or the physical copy in the University library. Get into the habit of keeping up with current developments. Test your knowledge of major developments by going through the Good News Week self-tests.
H.L. Hiew
Unit Coordinator
Document author: H.L.
Hiew, Unit Coordinator
Last Modified: Friday, 07-Sep-2001 12:49:33 MST
Disclaimer & Copyright Notice © 2001 Murdoch
University