![]() |
|
![]() |
||
![]() |
![]() |
![]() |
![]() |
![]() |
Working with Data Filesby Brent MichalskiAug. 21, 1998 Working with data adds extreme power to your Web applications. Data access is what the Web is about these days. For leading-edge Web sites, static HTML pages are out, and dynamically generated pages are in. Many dynamic sites store and use large amounts of data, both for and about their visitors. Accessing and updating this data can be easily accomplished with Perl CGI scripts. OverviewThis is the first part of a series that will build, piece-by-piece, a database application which uses the Web as its front-end. A Web front-end is much better than a client-based interface because the Web front-end is platform independent. Try running a database program that uses Visual Basic as a front-end on a UNIX machine and you'll see what I mean. DatabasesWe are going to use text files as our data files. You can also use a full-fledged database management system like MySQL for storing data, but we will use plain text files in this project. This database will be useful for smaller applications and there are many small types of data ideal for databases like this.The first part of our series is probably the easiest. We will learn how to add records to the data file. I chose the easiest application first because as I write this I am sitting on an airplane headed toward the Perl Conference. Once we finish the add program, we'll work on deleting, searching, and finally modifying database records. We're going to work with the modify program last for reasons which will soon become apparent. Once we finish all of the parts we will assemble the complete application into a fully-functional database. Jumping inAs I said, adding a record to a data file is the easiest of database operations. Because of this, it helps to introduce several of the concepts we'll use later on in the other database operations. Let's jump right in to the program and see what's going on.The script is outlined below, and you can try the application here. There is also a copy of the script without line numbers. 1: #!/usr/bin/perl 2: use CGI qw(:standard); 3: $EXCLUSIVE = 2; 4: $UNLOCK = 8; 5: $key=time(); 6: $database="datafile.txt"; 7: $returnto="add.html"; 8: $record = join '|', $key,param(name),param(email),param(phone),param(notes); 9: &AddRecord($database,$returnto,$record); 10: sub AddRecord{ 11: $database=$_[0]; 12: $returnto=$_[1]; 13: $record =$_[2]; 14: open (DB,">>$database") or die "Error: $!\n"; 15: flock DB, $EXCLUSIVE; 16: seek DB, 0, 2; 17: print DB "$record\n"; 18: flock DB, $UNLOCK; 19: close(DB); 20: print "Content-type: text/html\n\n"; 21: print<<HTML; 22: <HTML><BODY BGCOLOR="#FFFFFF" TEXT=ARIAL> 23: <FONT SIZE=6><CENTER>Record Added!</CENTER></FONT><HR> 24: <P> 25: <FONT SIZE=5>Back to: <A HREF="$returnTo">$returnTo</A> 26: </BODY></HTML> 27: HTML 28: return; 29: } Line-by-line explanationLine 1: Tells the program where to find Perl on the Web server. This line will vary depending on where Perl is installed on your server so you need to make any necessary changes. On a UNIX server, this line is required. If you are running this program on an NT server, this line is not required but won't hurt anything if included.Line 2: Loads the CGI.pm module into the program. The arguments in the qw(:standard) bring in more functions for us to use in the program. These functions are part of the CGI.pm module. Lines 3-4: These lines set up some variables we will use with the flock function later in the program. 2 creates an exclusive file lock and 8 unlocks a file. Line 5: Grabs the system time. I use this as a key field in our database. A key field is a unique field that aids us greatly when searching for data. On a very busy site, you would want to implement a different method for obtaining a key because this method is not truly unique. If two people click the submit button at the exact same time, they would get identical keys since time is stored in seconds. Line 6: Creates a variable with the value of the data file in it. We use this variable so we don't have to type the whole name every time we reference the data file, and also so we can change the name or location of the data file in a single place. Line 7: Creates a variable we use as the link back once we have added the data to the database. Line 8: Uses the join function to create the $record variable. The $record variable is all of the pieces of data, or fields, that make up one record in the database. The join function allows us to specify a delimiter, and then puts all of the strings together with the delimiter in between them. Join is the opposite of the split command. Line 9: Calls the AddRecord subroutine, passing the database file name, the location to return to, and the record to add to the database, to the subroutine. Line 10: Begins the AddRecord subroutine. Lines 11-13: When you pass data to a subroutine, it is placed in the @_ array. The items in this array can be accessed in order as $_[0], $_[1], and $_[2]. Since accessing the data using a variable like $_[2] is not very readable, I store the data I passed into some easier-to-read variables. Line 14: Opens the database file we want to access. We give it a name of DB; this is the filehandle which is how we reference this file when we want to manipulate it. The >> opens the file in append mode. Append mode means the data we add will go onto the end of the file. If we cannot open the file, we print out an error message telling the user. The $! system variable contains the error returned by the operating system, which we dutifully print out to help debug any problems. Line 15: Calls the flock function to lock the file. We lock it in exclusive mode so nobody else can write to it. Note that if you are running this program on a Windows 95/98 platform, you will have to remove all references to flock because it is not supported on those operating systems. Line 16: Seeks to the end of the file. We do this because if someone had just been accessing the file, there is a chance now that we are not at the end of the file. This ensures that we are at the end. Line 17: Prints the record to the database file and adds a \n onto the end of it. The \n is the newline character, which we are using as a record separator -- in other words, each record is stored on a line by itself. Line 18: Another call to the flock function. This time, we are unlocking the file to allow other users a chance at using it. Line 19: Closes the database file. Line 20: Tells the Web server what kind of data it is sending. Lines 21-27: These lines use CGI.pm to print out an HTML page that tells the user he or she has successfully added a record. Line 28: Returns back to the point in the program where we called this subroutine. Actually, it returns to the line after the line that called the subroutine. Line 29: Closes the subroutine. Wrapping it upThis is a simple example of how to add records to a database. Next time, we'll cover another feature or two of our CGI database application. Before you know it, you'll have a database where you can add, delete, modify and search the records.
Source Code for Working
with Data Files
Next: Searching a Data File
|
|
|
Web Techniques and Web Design and Development copyright © 1995-99 Miller Freeman, Inc. ALL RIGHTS RESERVED |