Lesson 9 - File I/O
File I/O

In this topic we'll discuss File Input and Output (File I/O.) You'll learn how to read files, write files, and how to safely
lock files while reading or writing. We'll also take a look at file handles and how to use them to write to a pipe (the
input stream of another program is the most common use for a pipe) just like you can a file.

Opening A File

Before you can read or write to a file you need to open it. You do this with the open command. The open command looks like
this:

open (FILEHANDLE, "filename");

Change `FILEHANDLE` to something more explained, like `fOUTPUT`. Many of programs head file handles with a lower case `f` and
then all caps for the rest of the file handle. This makes it easier to decipher that it is a file handle.

The other portion that you will need to change is `filename`. If you would like to open a file for reading simply specify it
as `myfile` (looks for `myfile` in the current directory) or `/usr/home/myuser/filename` to specify an exact path. To open a
file for writing, change the filename to `> myfile`. To open it for appension use `>> myfile`.


Closing A File

Files should be closed as soon as you are done with them to deallocate the resouce. Use the close command for this. Simply
use `close (FILEHANDLE);`.


File Locking

File locking is used to stop two processes from writing the file at the same time. What happens if the user `soren` opens a
file for writing, but while he is writing his changes to the file the user `sara` also opens the file for writing. Who's
changes are lost? Who's are saved? We use "File Locking" to lock a file from being written. `soren` will lock the file,
open it, write his changes, and then unlock the file. `sara` can not open the file until it is unlocked by `soren`. `sara`
can't lock/unlock the file either, because `soren` owns the lock.

A file locking command looks like:
flock(FILEHANDLE,$LOCK_EX);

You can specify either LOCK_EX or LOCK_UN for exclusive lock or unlock file. There are also some other advanced options
(which remain beyond the scope of this lesson.)

$LOCK_EX = 2;
$LOCK_UN = 8;

# LOCK_EX Exclusive lock. Only one process may hold an exclusive lock for a given file at a given time.
# LOCK_UN Unlock.


Project

The counter project demonstrates locking a file, opening it for reading and for writing, and unlocking the file. The project
demonstrates a simple text based counter in a perl script. Everytime it gets executed the output is one more than last time,
this is useful for a web page where you wish to count visitors.

Get This Sample

The code follows;

#!/usr/bin/perl

# Open the file and read its contents.
open (IN, "< ./cnt");
chomp($curcount = <IN>);
close (IN);

# Increment the count and output the count.
$curcount++;
print "$curcount";

# Save the new count to the file.
open (OUT, "> ./cnt");
lock(); # Lock file.
print OUT $curcount;
unlock(); # Unlock file.
close (OUT);

sub lock {
flock(OUT,$LOCK_EX);
}

sub unlock {
flock(OUT,$LOCK_UN);
}

Lesson 10 - Databases

Home | Lessons | Get Perl | Resources