Appending Files
Create a File
When you append a file, you simply add an extra line (or more) at the end of the file, after everything that was already in there. This is useful if you want to add information to a file but not delete other information in it. Before you try this on any important information, you should create a test file to make sure these commands work properly. We will make a test file here which holds some web site information. Suppose we had a file that looked like this, let's call it "websites.cgi":
PageResource|http://www.pageresource.com|Tutorials
JavaScript City|http://www.javascriptcity.com|Scripts
Appending
Now suppose we want to add another site to the list. We can do that by appending the websites.cgi file. First, we open the file, like we did when we read from it:
$sitedata="websites.cgi";
open(DAT,">>$sitedata") || die("Cannot Open File");
Notice that we have quotes around the command after the comma, plus we have the two greater than characters (>>) before the file to open variable. Having two of these indicates we want to append the file, rather than read or overwrite it.
Now to append it, we basically send data to the file with the print command:
print DAT "Mysite\|http://www.mysite.com\|My website\n";
Notice that we follow print with the file handle DAT, then we include what to print between the quote marks, just like a regular print command. Here, we want the information in the same format as the rest of our file, so we put it in the same order. Also, we place the pipe symbols to separate the information, but we have to be sure to escape them with \ characters or it will make a mess. So, between each item we have \| to make it print the pipe character. At the end, we also need to remember to place the \n character at the end so that we have a new line set and added in.
When we are done, we can close the file and it should now have the new information:
$sitedata="websites.cgi";
open(DAT,">>$sitedata") || die("Cannot Open File");
print DAT "Mysite\|http://www.mysite.com\|My website\n";
close(DAT);
Of course, you can use variables in place of the straight data. The variable method is more useful if you have read in data from elsewhere though. Here we don't gain much by it, other than it would be a little clearer if we wanted to change the values of the variables later:
$sitename="Mysite";
$siteurl="http://www.mysite.com";
$description="My website";
$sitedata="websites.cgi";
open(DAT,">>$sitedata") || die("Cannot Open File");
print DAT "$sitename\|$siteurl\|$description\n";
close(DAT);
File Locking
One thing that should be done for scripts that will have more than one user writing to them is to incorporate file locking so that only one person may write to the file at a time. Perl offers the flock command to do this (note that flock will not work with all file systems, so another method may need to be used to lock a file on such systems). To use flock, you will need to have a "use" statement in your code to tell Perl to use the Fcntl module:use Fcntl;
To make sure you can use the constants that go with the flock command, you will also want to add the :flock tag to the "use" statement:
use Fcntl qw(:flock);
Use of the flock command would look like this:
flock(HANDLE, CONSTANT);
The constants for the flock command are listed below:
| Value | Purpose |
|---|---|
| LOCK_SH | Shared Lock - typically used when reading from a file |
| LOCK_EX | Exclusive Lock - typically used when writing to a file |
| LOCK_NB | Non-Blocking Lock - used if you do not want the script to stall if a lock is not obtained |
| LOCK_UN | Unlock - unlocks the file: can lead to data loss (see below) |
We will be using the LOCK_EX in this tutorial, as we are going to be writing to the file to add information to the end of it.
Unlocking the file with LOCK_UN is not usually recommended as the file will be unlocked automatically when you close the file using the normal close() command. Unlocking the file before closing it could potentially lead to data loss, which is something we want to avoid!
It should also be noted that flock only locks files from others trying to access it using flock. If you have another script writing to it in another way, it won't be locked out. Just be sure that if you write to the file that all your scripts trying to access it also use the flock command.
For our purposes, we will lock the file using LOCK_EX. The example below shows how this would be implemented:
use Fcntl qw(:flock);
$sitename="Mysite";
$siteurl="http://www.mysite.com";
$description="My website";
$sitedata="websites.cgi";
open(DAT,">>$sitedata") || die("Cannot Open File");
flock(DAT, LOCK_EX);
print DAT "$sitename\|$siteurl\|$description\n";
close(DAT);
This locks the file right after it is opened. Once we have written to the data file, we close the data file— and this will also unlock the file.
One other piece you may wish to add is the seek command. This is used in case the flock command was waiting for someone else to write to the file, which may cause your data not be written in its intended place in the file. To use the seek command, you need to add the :seek tag to the "use" statement in addition to the :flock tag.
use Fcntl qw(:flock :seek);
The seek command is used in the following manner:
seek(HANDLE, OFFSET, LOCATION);
The location can be set with one of these constants:
| Value | Location |
|---|---|
| SEEK_SET | Beginning of the file |
| SEEK_CUR | Current position in the file |
| SEEK_END | End of the file |
The location can also be set with numbers: 0 for the beginning of the file, 1 for the current position in the file, or 2 for the end of the file.
The offset allows you to specify how many bytes from the location you would like to move. If you want to move to the very beginning or end of the file, you would leave the offset at 0.
For our purposes, we need to go to the end of the file for the append. Thus, we will use SEEK_END as our location with the offset at 0:
use Fcntl qw(:flock :seek);
$sitename="Mysite";
$siteurl="http://www.mysite.com";
$description="My website";
$sitedata="websites.cgi";
open(DAT,">>$sitedata") || die("Cannot Open File");
flock(DAT, LOCK_EX);
seek(DAT, 0, SEEK_END);
print DAT "$sitename\|$siteurl\|$description\n";
close(DAT);
Now we open the file, lock the file, seek the end of the file, write to the file, and close the file... which will also unlock it.
Well, that's all for now, let's go on to: Writing to Files.
Copyright © 1997-2009 The Web Design Resource. All rights reserved. Disclaimer.
