|
CGI - Flat File Databases |
tutorial by Matt |
What is a Flat-File Database? A Flat-File Database is a cheaper
way of storing data rather then spending money on Microsoft Access. A flat-file
database is just a simple text file with the .txt extension. Flat-file databases
can't do everything that Access can do but it can do the most common tasks. First
I will show you the different components of using a flat-file databases then we
will finish up this lesson with a nice example.
First at the start of your script you must set a
variable to hold the name of the file you wish to
open later. You would initialize the variable like
this: $nameoffile = "nameoffile.txt"; Where nameoffile
is the value youy choose for the variable which could be anything then for
nameoffile.txt you must fill in the name of you text file. Now is the fun part,
you now get to find out how to open a file and read it, open it and write to it,
or open it to read/write. Figure 5.1 shows the syntax for opening a file and reading it.
Figure 5.1
open (READFILE, "< $nameoffile") or
die "Can't open $nameoffile";
while () {
do something with the data here
}
Ok now first we declare to open the file in $nameoffile and if we can't open it
then the die function exits and prints that message. After that we use a while loop
to read through the filehandle READFILE. Then you would be able to do something
inside the body of the loop. Notice the red line. that is the line which will
make the major difference when doing different things to the file. Look at
Figure 5.2 to see the different symbols for read, write, read/write.
Figure 5.2
< This reads from the file.
>> This writes to the file.
+>> This reads/writes.
Ok now lets try an example. This example will add a username and
password to the flat-file database
Enter the HTML Document below and the CGI Script. You must also
create a text file, save it as login.txt.
JOIN
--------------------------------------------
#!/usr/bin/perl
use CGI;
$query = new CGI;
$login_file = "login.txt";
&set_variables;
&add_user;
&print_thankyou;
sub print_start {
print $query->header;
print "\n\nJoin\n\n\n";
}
sub print_end {
print "\n\n";
}
sub set_variables {
$username = $query->param('uname');
$password = $query->param('pass');
}
sub add_user {
$record = $query->param('uname') . "\t";
$record .= $query->param('pass') . "\n";
eval {
open (JOIN, ">> $login_file") or
die "Can't open User Database.";
flock JOIN, 2;
print JOIN $record;
close JOIN;
}
sub print_thankyou {
&print_start;
print "Thank you for joining! \n";
&print_end;
}
Now there is a lot to cover in this example. First you start of the CGI script
like you always do. Then you declare a variable called $login_file to hold the
text file name which is "login.txt". You then declare the process that the
program will take. First you set the form variables with the subroutine
&set_variables then you go onto the &add_user subroutine to add the user and
finally you print a nice little thank you message to the user. You also have
other subroutines that print the start and the end of the HTML Document. Now
we get into the &add_user subroutine does. First you declare a variable called
$record and you assign it the value of the first text box. Then you use the .=
operator to add to the variable the value of the second text box too.
The .= acts as an addition operator almost, the . starts for plus when used in
this fashion so it basically reads like +=. On the other side of the equal signs
you notice something strange . "\t"; This will act as you
hitting the TAB key in a text file. As you remember from before the \n is the
newline character so it prints the value of the first text box then it moves to
the next tab space and prints the value of the second text box and moves the next
text printing space to the next line for the next user that joins. After that we get
into something called an eval block. This will trap any
errors that might occur inside of that area such as not being able to opent he text
file. This will allow you to handle the error rather then giving you user some wacky
crazy code that PERL produces as the error. After that you open the file and attach
the filehandle JOIN to the file. Next you flock the file which prevents other users
joining at the same time to join that second. You add the 2 there to tell PERL that
you want an exclusive lock on the file. Now you print the varialbe $record to the file
then you finally close the file. That is how this script works. It really isn't hard
its just something you have to get used to.
Thats it for this lesson move onto the next section Validation.
|
|
|
|