CGI and Perl

Displaying the Complete Guest List

One last thing you might want to do (now that you have a database with some data in it) is to provide a CGI script that displays the guest book in a nice format (see Figure 7.2). This can be done by parsing the database file and generating the HTML markup on-the-fly. The example in Listing 7.4 shows one option on how you can do this.

Listing 7.4. CGI program to display the guest list.

#!/public/bin/perl5
 use CGI::Form;
 $q = new CGI::Form;
 print $q->header();
 print $q->start_html(-title=>`Guest List');
 print "<H1>Guest List</H1>\n";
 print "<TABLE BORDER>\n";
 print "<TR><TH>Name<TH>Title<TH>Company<TH>Address<TH>City<TH>State<TH>Zip";
 print "<TH>Phone<TH>Fax<TH>e-mail<TH>How they heard<TH>Products of interest";
 print "<TH>Comments";
 open(DATABASE,"< guests.list") ||
    die "Cannot open guest list database for read!\n";
 while(<DATABASE>) {
    print "<TR>\n";
    print "<TD>";
    @fields=split(/\<\*\>/);
    print join(`<TD>`,@fields);
    print "</TR>";
 }
 close(DATABASE);
 print "</TABLE>";

The table of guests in the guest book
Figure 7.2. The table of guests in the guest book.

Review

What you've seen in this section is how to implement your own guest book and the ease with which it can be done using Perl5 and the WWW libraries. You should also consider adding some nice images to your guest book. You should use everything that HTML offers to provide a nice experience for your visitor.

by BrainBellupdated
Advertisement: