Fall Semester 2007


Week Three: CGI with Perl.
There are two parts to this set of notes: Tuesday and Thursday.

Each part has two parts: a warm-up and an exercise. We'll work them out both in class.

Part One (Tuesday lecture).

The warmup:

  1. Write a short CGI script that simply prints $ENV{SCRIPT_NAME}.

    Call this partOne and place it in your cgi-bin folder.

  2. Create a form with three text fields and a submit button in it.

    Call the form partOne.html and put it in htdocs.

    Make the form submit the data to the program partOne in your cgi-bin.

  3. Modify partOne (the program) to also print $ENV{QUERY_STRING} when called.

    Use the form to send some data to the program. What do you notice?

  4. Modify the program to report the names of the fields and values associated that it receives.

  5. Write a CGI script that prints a link, to itself.

    Call the program counter and place it in cgi-bin next to the other program.

  6. Make the program check what data it receives: if a balance is noticed on the URL make the program report it.

    So if the URL is counter?balance=6 the program should report the balance (as an <h1>) then the link as before.

  7. If a balance is received add one to it, report it, and also put it on the link (as a parameter, data).

    How does the program work now? What does it do?

  8. Can you implement this program now?

The exercise:

#!/usr/bin/perl

use CGI;                                  # import the CGI.pm module (library) 

$q = new CGI;                             # data binding: create object $q that parses and stores incoming data

print $q->header, $q->start_html;         # instance methods header and start_html produce strings for corresponding HTML sections

print $q->param('balance');               # the value associated with balance (if any) is extracted, and printed
                                          
print $q->end_html;                       # print the string for the end of HTML document, produced by instance method end_html

Try rewriting one of the two programs discussed above (e.g., counter) using this information.

Part Two (Thursday lecture).

The warm-up:

  1. Write a program (call it portfolio) that prints four links and a picture.

    Put the program in cgi-bin, make it executable, test it. Here's the model.

  2. Now make sure that those links ask for different pictures. (Set up a parameter name, then use four different values).

    Have the program report what's being asked (the param and the value or, at the very least, the query string).

  3. Change the program such that it prints a different picture per each request, or the default picture otherwise.

  4. Make one more change: the link that matches the (valid) request for one of the four pictures should be shown in plain text.

    Play with this script to clarify what that means if it's unclear.

  5. Compare this program with the one you wrote last time: which program keeps state, which doesn't?

The exercise:

  1. Write a program that prints a form, composed of a textfield and a submit button.

  2. Implement the functionality of the counter program from Part One using one textfield and one submit button only (no embedded links).

    Hint: textfield's name is balance so you can store your state there. (Ugly, perhaps, but functional. What if the textfield becomes a hidden field?)

  3. Turn this program into a calculator by adding textfields in the interface to indicate

  4. Check the prototype posted and see if you can implement it (with or without CGI.pm, but using hidden fields).
Can you implement something like the Flag Quiz (the program is almost complete).


Last updated: Sep 10, 2007