|
Perl Case Study - Guestbook CGI Ah, the age old guestbook program, which happens to be one of the most common applications of perl programs "for the masses." For this you only need to know the basics of writing to a file (see Perl Basics: Reading, Writing, and Taking a Bite) and a bit of knowledge from our Time CGI Case Study and you'll be able to write a guestbook script like a pro! No more need to use one of those remotely hosted ones - you modify and add your own features as you please :) What the guestbook will consist of the following: a single HTML page onto which the entries are written, a posting page (with the form to sign the guestbook) and the CGI script which handles the updating of the guestbook itself. Let's name our HTML page something pretty obviously like guestbook.html and the form page to sign the book something like sign.html. Our script is guestbook.cgi, and it would merely take in the form values submitted through the sign.html page, format and post this information in guestbook.html. So it's very linear in structure :) sign.html -------> guestbook.cgi --------> guestbook.html Let's zoom in on what's going on in guestbook.cgi:
#!/usr/local/bin/perl
################# @pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
open(GB, "guestbook.html"); Now we get to the harder part, we have the guestbook.html page where we want to insert a new entry...but not exactly at the top of the page. So we wouldn't first start by rewriting guestbook.html with the latest entry. In this case, we might want to stick a comment in the guestbook.html file, like <!-- begin --> just before the entries begin. Now
open(GB, ">guestbook.html");
###################
print "Content-type: text/html\n\n";
################# sub getDate { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $ampm = "a.m.";
if ($hour eq 12) { $ampm = "p.m."; }
@months =
("January","February","March","April","May","June","July","August","September","October","November","December"); }
And that's it! A very simple routine that doesn't require server side includes in the implementation like many other guestbook scripts do. Sign Guestbook Last Updated August 4, 1999
|
||||||||||
©1999 Team 26297 "Ad Infinitum Web." All rights reserved. Any reproduction of this document for commercial or redistribution purposes without the permission of the author is forbidden.