Web Teacher Software
web database
CGI tutorial
database consulting
web database training
contact us about web 
database software
Hosted Database Solutions
webteacher home
Web Teacher Consulting
CGI - A free tutorial for the Total Non-Programmer.
CGI - Let's get startedCGI - Introduction to Perl

The Guestbook CGI code

#********* BEGIN BODY********************

open (LOGFILE, ">>guestbook.log");


print LOGFILE ("$newline\n");

close LOGFILE;

print "<BODY BGCOLOR='BEIGE'><H1>Thank you. ";
print "Your comments have been added</H1>";

#******** END BODY************************

The first line of the code is this:
open (LOGFILE, ">>guestbook.log");

    This line prepares the file guestbook.log to be written to.  The open command is used to open files for both input and output.  The word LOGFILE (which could have been anything) is called a "file handle."  Later, when I want to append to the guestbook.log file, I will write print LOGFILE.  For the remainder of the program, LOGFILE means guestbook.log.
    After the comma and the quotes, the name of the file begins with 2 Greater-than signs >>.  This is one of about 12 special symbols to determine what kind of input or output is being opened.  Will the program read from this file?  Will it write to it?  Will it do both?  Will it overwrite it?  There are 3 symbols that I use frequently here:
    1. <   - open file for input  (read what's in the file).
    2. >   - open file for output (overwrite the file).
    3. >> - append to file (tack something on to the end).

The next line is the join statement.  This is an extremely useful tool for adding data to delimited text files.  The statement
means take each value in the @value array (each entry box on the HTML form), and join them with double colons.

$newline could now contain this:
Robert::Young::5 Main St.::Anytown::MA::02177::(617) 555-1212::robyoung@mediaone.net::on::This is Great

Now that $newline is complete, I am ready to add it to the end of guestbook.log.  Since I already have a file handle for the log file, appending to it is as easy as this:
print LOGFILE ("$newline\n");
It will add the entire contents of $newline to the end of guestbook.log.  I also asked it to add a \n (carriage return) to the end of $newline, so that the next time I append something, it will start at the beginning of a new line.

close LOGFILE;
It stands to reason that if I have to open a connection to the file when I begin my program, I should close that connection when I am through.

Lastly, I printed something to the screen.  This is more than just a courtesy.  You have to write something or else you get a 'document contains no data' error.  So we wrote this:

print "<BODY BGCOLOR=\'BEIGE\'><H1>Thank you.  Your comments have been added</H1>";
BACKHow do I get the data?

Home | WebData - Web Database Software | Javascript | CGI | Consulting | Map Builder | Contact Us | The Press Room