Comatose 290 Taboo Programmer Team Colleague

Catching Up
In The Previous Tutorials, we have covered a lot of information. We've gone over a few of the functions of the CGI.pm, and and figured out how to retrieve information from a web page form, submitted by the user. However, reading this information, and thanking them by name isn't very productive. Sure it works, has functionality, and gives us understanding, but what do we do with that information? The plan is simple, we'll store this information in a text file.

Flat Files
At it's very simplest description, a "flat file" is just a text file that stores all of your records, 1 record per line. That also means that all of the fields have to be on 1 line. Now, You have to know how these fields are seperated (a record is a whole bunch of fields. So, A Field would be "first name", while a record would contain "first name, last name, middle initial"). Each field has to be seperated by some kind of standard, static character or characters. A Lot of programs like to use the tab character, or even commas. For our purposes, however, we are going to stick with Tabs.

Open, Print and Close
The process for writing the information to the file, is not very difficult at all. It takes a simple 3 step process in order to do this. Don't get me wrong, there are a ton of other methods for saving data, and some that may be more efficient, we could even use the DBI Module and Functions in order to save things in SQL or even Access. For something that isn't extremely complex, however, or for something that is only there temporarily (say, until another program does something with it), then the simplist and best solution is to store it with a flat file. How do we do that? Three very easy steps are how we save data. Here is the code for saving the data we recieved (in the previous tutorial's) into a tab delimited text file:

$filename = "~./userinfo.txt";
open(FH, ">>$filename");
     print FH "$fname\t$lname\n";
close(FH);

A Little explaination is in order here, about a few of the techniques used in order to make this work. The first line is simply assigning a variable to a path. The path is the user's home directory, with a filename of "userinfo.txt". Next is the open statement, which contains two things that we need to understand. The Filehandle, which without going into a much more complicated and detailed discussion, we'll suffice to say it's a variable that represents the open file. We Use This "variable" to read from and/or write to the file. In our code above, the filehand is FH. The next important portion of the open statement, is the redirection symbols. >> is the means by which we tell the open statement, that we want to open the file for writing. If we had only used one >, then it will create the file if it does not exist. If it does exist, however, it will erase everything in the file, and put the new information in it. When using two >>, this convention will create the file if it does not exist, however, if it does exist, then the information in the file will be safe, and the new information will be Appended (added) to the end of the file. We don't want to erase the info, which is why we used >>. The next line is simple enough, we print, to the filehandle (FH) the information we retrieved from the user through the web page. Thus the first name ($fname), a tab (\t), the last name ($lname), and finally a "new line" character (\n). Then, we close the filehandle, having saved the first name and last name, separated by a tab, to the file.

Saving Data Conclusion
Ok Maybe opening files with filehandles and redirection isn't very complicated, but it's certainly a needed piece of the programming process. Saving data is one of the very most important parts to any good program, and knowing how to do it as a programmer is essential. The data could be saved in any way that you want. It doesn't have to be tab delimited... you could even have written the results to the file in HTML, or some other format. Here is the source code for the project now:

#!/usr/bin/perl

use CGI qw(:standard);

if (param()) {
     $filename = "~./userdata.txt";
     chomp($fname = param('fname'));
     chomp($lname = param('lname'));
     open(FH, ">>$filename");
          print FH "$fname\t$lname\n";
     close(FH);
     &start_page;  
} else {
     &complain;
}

sub start_page
{
     print header;
     print <<HTMLPAGE;
	<HTML>
	<HEAD>
		<TITLE>Thank You $fname</TITLE>
	</HEAD>
	<BODY BGCOLOR="#000000" TEXT="#FFFFFF">
		<CENTER>
			<H1>Thank You $fname!</H1>
			<HR><BR>
			<INPUT TYPE="BUTTON" VALUE="Back" onClick="history.go(-1);">
		</CENTER>
	</BODY>
	</HTML>
HTMLPAGE
}

sub complain
{
     print header;
     print <<HTMLPAGE;
	<HTML>
	<HEAD>
		<TITLE>CGI Error: No Data</TITLE>
	</HEAD>
	<BODY BGCOLOR="#000000" TEXT="#FFFFFF">
		<CENTER>
			<H1>I Need More Information</H1>
			<HR><BR>
			<INPUT TYPE="BUTTon" VALUE="Back" onClick="history.go(-1);">
		</CENTER>
	</BODY>
	</HTML>
HTMLPAGE
}