Murdoch University LogoB211 Advanced Internet Computing

Laboratory Week 6 - Introduction to CGI programming with Perl


Learning Objectives:

Before doing the tutorial, please read:

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:

Instructions:

Trying the examples from the lectures

  1. 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.

  2. 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:

    1. Copy and save the scripts into text files. Give the files a .cgi or .pl extension.

    2. 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.

    3. 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.cgi

      You will need to do this only once for every new script you create.

    4. 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

To edit CGI scripts

Exercises:

  1. Write a CGI script which prints out your name and student ID to the browser.
  2. 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.
  3. 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

  4. 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

  5. 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