CSCI A348/A548

Lab Notes 5

Fall 1999


This lab contains a set of Perl and CGI.pm exercises that you are encouraged to try to solve by yourself. Also please use the lab time to work on the homework assignment that needs to be turned in at the end of the week.

Activities for Lab 5 (Part I)

CGI and HTML

1. Create an HTML page (you can put it in lab5/one.html) that contains a form that asks the user for her/his username and password. Use one password field and one text field. Ignore, if you want, the method and action fields but provide a submit and a reset button on the form). Here's part of it to get you started:
<form>
  Type your username here: 
  <input type="text" name="username"> 
</form>
2. Create an HTML page (/lab5/two.html) that contains one form that calls a printenv script on your server. The form itself should contain three multiple-choice questions: a RADIO question, a SELECT question, and a CHECKBOXES question. Use GET as the request method in your form. Provide submit and reset buttons. Call up the page with Netscape, fill in the form and submit it. Look at the script's output. Do the same thing with POST. What are the differences in the script's output, if any?

Here's part of it to get you started:

<form method="POST" 
         action="/cgi-bin/printenv">

What is the capital of Italy? 
<input type="radio" name="q1a" value="B"> Bologna
<input type="radio" name="q1a" value="R"> Roma
<input type="radio" name="q1a" value="M"> Milano
<input type="radio" name="q1a" value="T"> Trieste

</form>
3. Write a script (/cgi-bin/lab5/three) that uses CGI.pm and prints 'Howdy World' to the screen.

Here's part of it to get you started:

#!/usr/bin/perl
use CGI;
$query = new CGI; 
4. Write a script (/cgi-bin/lab5/four) that uses CGI.pm that prints out the form developed at step 2 when called with GET and $query->dump when called with POST. Here's part of it to get you started:
if ($query->request_method eq 'GET') {

} elsif ($query->request_method eq 'POST') {

} 
5. Copy /lab5/one.html into /lab5/five.html and change the action field in the form to the script developed at step 4. You now have two entry points in the script. How many more can you have/create?
6. Run the script developed at step 4 from the command line. Does anything strike you as being a useful aspect of CGI.pm compared to the way we developed things so far (with our custom-made CGI processor)?
Activities for Lab 5 (Part II)

Perl

7. Write a Perl program that lists its command line arguments.

Here's part of it to get you started:

foreach $val (@ARGV) { 
or
for ($i = 0; $i <= $#ARGV; $i++) { 
Make the program write its output in both ways.

8. Write a Perl program that reads one line from the terminal and prints it back surrounded by ***( and ***). Use chop, if you need to. Make the output appear on a line of its own.

Here's part of it to get you started:

$x = <STDIN>; 
9. Write a Perl program that reads a file and prints only its even numbered lines. Change the program to print all the lines each line in the output being preceded by its number. Assume the first line in the file has number 1. It's up to you how you make the name of the file known to your program.

Here's part of it to get you started:

@x = <AB> 
Do it whis way too:
while ($x = <AB>) {
Explain the differences between the two approaches.

10. Modify the program from step 9 that numbers lines such that the name of the input file is specified on the command line preceded by a switch -file.

Here's part of it to get you started:

if ($ARGV[0] eq "-file" && $ARGV[1]) {
  open (AB, $ARGV[1]);     

Solutions are posted here.


Last updated: September 28, 1999 by Adrian German