| 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 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 open (FILEHANDLE, "filename"); Change `FILEHANDLE`
to something more explained, like `fOUTPUT`. Many of programs head file
handles with a lower case `f` and The other portion
that you will need to change is `filename`. If you would like to open
a file for reading simply specify it
Files should be
closed as soon as you are done with them to deallocate the resouce.
Use the close command for this. Simply
File locking is
used to stop two processes from writing the file at the same time. What
happens if the user `soren` opens a A file locking command
looks like: You can specify
either LOCK_EX or LOCK_UN for exclusive lock or unlock file. There are
also some other advanced options $LOCK_EX
= 2; #
LOCK_EX Exclusive lock. Only one process may hold an exclusive lock
for a given file at a given time.
The counter project
demonstrates locking a file, opening it for reading and for writing,
and unlocking the file. The project The code follows; #!/usr/bin/perl #
Open the file and read its contents. #
Increment the count and output the count. #
Save the new count to the file.
sub lock {
sub unlock { |