[an error occurred while processing this directive]
#*********
BEGIN BODY********************
open(LOGFILE, "<guestbook.log");
@entries = <LOGFILE>;
close LOGFILE;
print "<BODY bgcolor=beige>\n";
print "<TABLE>\n";
foreach $line (@entries) {
@fields = split(/::/,$line);
print "<TR><TD>$fields[0]
$fields[1]<TD>$fields[9]\n";
};
print "</TABLE>\n";
#********* END BODY********************
Copy this script to the BODY of the template.txt file.
Save it as getlog.cgi,
Use your FTP program to upload getlog.cgi to the perltour
folder on the server. (Ascii transfer only)
Switch to a Unix prompt, be sure you are in the perltour folder,
and type chmod a+rx getlog.cgi
Point your browser to http://your.server.name/perltour/getlog.cgi
The screen should turn beige and display a list of all the
people and comments you have entered.
This script is almost exactly the reverse of the previous script.
The guestbook.cgi script on the previous page would get text from the page,
combine it into one long line, and add it to guestbook.log. This
script will get a line of text from the guestbook.log file, parse it into
individual values, and display them on the screen. I chose to display
only the name and comments for this first example. It is easier to
read this way.
open(LOGFILE, "<guestbook.log");
The first line of this script is the same as the first line of the
last script, with one important difference. Remember the 2 greater-than
signs in the last open statement? This time we are using just
1 less-than sign, indicating that the file is being opened for input.
LOGFILE will again be the file handle, so from now on, LOGFILE means "input
from guestbook.log."
@entries = <LOGFILE>;
This is a really cool command. It creates an array called @entries.
Each line in the file, LOGFILE (therefore, each line in our guestbook)
is an entry in the array. If I were now to execute this command
print "$entries[0]"
then the first line of guestbook would appear.
IMPORTANT: Notice that when I refer to an entire array I use the @
sign, but when I want
just one value I use the $ sign. So I could write:
@fruit = ('apples','pears','grapes');
To get the second value from the array @fruit, I would type this:
print $fruit[1];
print "<BODY bgcolor=beige>\n";
print "<TABLE>\n";
Just some HTML to set the mood. The background will now be beige,
and we are ready to start writing rows and data to our table. By
the way, I didn't have to add a newline (\n) to the end of the HTML tags.
As you know, HTML doesn't care whether you type the source code on 1 line
or 50. This will make the page easier to proofread, however, if for
some reason we should want to view the page source.
foreach $line (@entries) {
This is another very nice feature in Perl. The foreach command
is a special loop just for arrays. It will run once for each value
in @entries. Each time it runs the loop, it will assign the
next value in @entries to the variable $line.
So the first time throught the loop $line will be equal to the
first value in @entries (the first line of our guestbook).
Then the next time through the loop, $line will be equal to the
second value (the second line of our guestbook). It will automatically
stop when it reaches the end of @entries.
@fields = split(/::/,$line);
The split command is one of the most useful special tools in
Perl. It will split a value into two or more pieces, and store each
piece in an array. In our situation, we stored 10 values on each
line, seperated by 9 double colons (::). A typical line looked
like this.
Robert::Young::5 Main St.::Anytown::MA::02177::(617)
555-1212::robyoung@mediaone.net::on::This page is great!
The split command would locate the double colons, split the line
into 10 seperate values, and stack them each to the array @fields.
Now, I can analyze my data, because
$fields[0] = Robert
$fields[1] = Young
$fields[2] = 5 Main St.
etc.
print "<TR><TD>$fields[0] $fields[1]<TD>$fields[9]\n";
}
If I wanted to print out the entire line, I could have typed this:
foreach $value (@fields) {
print "<TD>$value";
};
The entire line is too big for most screens, however, and the wrapping
looks ugly in narrow HTML tables. Instead, I chose to print only
the first and last name in one cell, and the comments in another.
We can easily create another CGI for the address, phone, and e-mail.
print "</TABLE>\n";
Now that we have written a <TR> table row for each line in the guestbook,
it is time to close the
<TABLE> tag.
That's it. Now, since this CGI does not need an user information,
it does not need to be the ACTION element of a form. You can
create a link to getlog.cgi the same way you would create a link
to any other page. You can even bookmark it.
| |
|
|
Home | WebData - Web Database Software | Javascript | CGI
| Consulting | Map
Builder
| Contact Us | The Press
Room
|