Workshop

The workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try to understand the quiz and exercise answers before you go on to tomorrow's lesson.

Quiz

1:What does CGI stand for? What's it used for?
A1: CGI stands for Common Gateway Interface, and refers to programs that run on the Web server in response to requests from a Web browser (commonly from a form on an HTML page).
2:What does the CGI.pm module give you? How do you get it and use it?
A2: The CGI.pm module provides behavior for writing, running, and debugging CGI scripts. It provides a number of subroutines for managing input to and output from those scripts as well as other utility subroutines for just about any aspect of CGI you can think of.

The CGI.pm module is shipped with most current versions of Perl. If it is not already available as part of your Perl distribution, you can download and install it from the URL mentioned earlier in this lesson.

To use the CGI module, you import it like you do any other module: with use and an import tag such as :standard or :all.

3:Why would you want to run a CGI script on the command line before trying to run it via a Web page?
A3: Running a CGI script from the command line is usually easier to find the smaller syntax or basic errors you might have made without needing to install the script on the Web server, connect to the Internet, and so on. By running the script on the command line and giving it sample data, you can test your script before actually putting it online.
4:How do you use the param() subroutine?
A4: The param() subroutine is used to find out the values of any form elements on the Web page that called this script. Without any arguments, param() returns a list of keys referring to the names of the form elements. With a key argument (a string), param() returns the value associated with that form element.
5:List three ways to print HTML code as output for your CGI script.
A5: You can print HTML code using one of these methods:
  • Use regular print statements. Watch out for embedded quotes!

  • Here document

  • Various subroutines via the CGI.pm module.

6:Why is a here document sometimes more useful than print?
A6: Here documents provide a way to print a block of text verbatim: all newlines and quotes are printed verbatim. Here documents provide a way for you to avoid a lot of repetitive print statements or escaped quotation marks.

Exercises

1:Write a CGI script that prints the form elements it was submitted with as a bulleted list. HINT: bulleted lists in HTML look like this:
<ul>
<li>One item</li>
<li>Two items</li>
<li>Three items</li>
</ul>

Another hint: param() without any arguments gives you a list of all the form element names the script was submitted with.

A1: Here's one way to do it:
#!/usr /bin/perl -w
use strict;
use CGI qw(:standard);

my @keys = param();

print header;
print start_html('Hello!'),
print "<h1>Key/Value Pairs</h1>
";
print "<ul>
";

foreach my $name (@keys) {
    print "<li>$name = ", param($name), "</li>
";
}
print "</ul>
";

print end_html;

2:BUG BUSTER: What's wrong with this bit of script?
if (!param()) { $data{sex_na} ++ }
else {
    if (param() eq 'male') { $data{sex_m} ++; }
    elsif (param() eq 'female') { $data{sex_f} ++; }
}

A2: The param() subroutine is called without any arguments. In some cases—such as the one in Exercise 1—this may be exactly what you want to do. In this case, however, because the tests are looking for string values, param() should probably be called with some sort of string argument to extract a value from the form parameters.
3:Write a CGI script that prints a simple HTML page (a hello world is fine) but also keeps track of how many times the page has been accessed and prints that value on the page as well.
A3: Here's one way to do it (don't forget to create count.txt beforehand):
#!/usr /bin/perl -w
use strict;
use CGI qw(:standard);

my $countfile = 'count.txt';
my $count = '';

open(COUNT, $countfile) or die "Can't open counter file: $!";
while (<COUNT>) {
    $count = $_;
}
close(COUNT);

$count++;

open(COUNT,">$countfile") or die "Can't write to counter file: $!";
print COUNT $count;
close(COUNT);

print header;
print start_html('Hello!'),
print "<h1>Hello, World</h1>
";
print "<p>This page has been visited $count times.</p>
";

print end_html;

4:Write a CGI script to implement a very simple guestbook-like feature that allows users to post one-line comments to a Web page. Keep track of all past postings. You can assume the existence of an HTML form with two elements: a text field called mail for the e-mail address of the poster, and a text field called comment for comments.
A4: Here's one (particularly simple) way of doing it. This script assumes a file of past comments, one per line, with the e-mail address first.
#!/usr/ bin/perl -w
use strict;
use CGI qw(:standard);

my $guestbook = 'guest.txt';
my $mail = '';                  # email address
my $comment = '';               # comments

open(GUEST, $guestbook) or die "Can't open guestbook file: $!";

print header;
print start_html('Comments'),
print "<H1>Comments</H1>
";
print "<P>Comments about this Web site!
";
print "<HR>
";

while (<GUEST>) {
     chomp();
    ($mail, $comment) = split(' ',$_,2);
    if ($mail) {
        if (!$comment) { $comment = "nothing
"; }
        else {  print "<p><n>$mail says:</b> $comment</p>"; }
    }
}

$mail = param('mail'),
$comment = param('comment'),
print "<p><b>$mail says:</b> $comment</p>
";

open(GUEST,">>$guestbook") or die "Can't open guestbook file: $!";
print GUEST "$mail $comment
";

print end_html;

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset