In this lesson, I've given you the basics of I/O and of managing file systems; the lessons you learn here should apply to all your basic Perl programs that need to use files and command-line arguments. On Day 17 we'll explore how to handle aspects of the file system itself. In this lesson's Going Deeper, I'll try and point you to a number of other places to look for more information and details about advanced input and output and handling filesystem features.
All the built-in Perl functions, as I've noted before, are documented in the perlfunc man page. Also of some use may be the FAQ on files and formats contained in the perlfaq man page.
A few other shortcuts and features of the open function:
You can leave off the filename to the open function if, and only if, a scalar variable with the same name as the file handle has already been set to the file to be opened. For example:
$FILE = "myfile.txt"; open(FILE) or die "Can't open $FILE: $! ";
This can be useful for filenames that need to be opened or reopened; you can set a variable at the start of your script, and then reuse that filename over and over again.
Contrary to what I claimed earlier in this chapter, you can open a file both for reading and writing using the +> special character in front of the filename:
open(FILE, "+>thefile") or die "Can't open file: $! ";
Because this can often be confusing, however, I prefer to use separate file handles and to read and write as separate operations.
Filenames that start with pipes (|) operate as if the filename was a command, and the output will be piped to that command via the command shell for your system.
For many more details about the various uses of open, see the perlfunc man page.
Table 15.2 shows several file-related built-in functions that I have not described in this lesson.
The input and output techniques described in this chapter provide simple, line-oriented buffered input and output from and to file handles and standard input, output, and error. If you're interested in doing more low-level sophisticated forms of input and output, explore the various other I/O functions Perl provides for you, as listed in Table 15.3.
Function | What It Does |
---|---|
fcntl | File control (the Unix fctrl(2) function) |
flock | Lock file (Unix flock(2) function) |
getc | Get next byte |
ioctl | TTY control (The Unix ioctl(2) system call) |
read | Input a specific number of bytes (fread(2)) |
rewinddir | Set the current (input) position to the beginning of the directory handle |
seek | Position the file pointer at a specific location in a file (same as fseek() in C) |
seekdir | Same as seek for directory handles |
select | Make file descriptors ready for reading (same as the select(2) command on Unix. Not to be confused with select on a file handle for setting the default file handle. |
syscall | Call a Unix system call (syscall(2)) |
sysopen | Open a file handle with a mode and permissions |
sysread | Read a certain number of bytes using read(2) |
syswrite | Write a certain number of bytes to the file handle with write(2) |
tell | Return the current file pointer position |
telldir | Same as tell for directory handles. |
write | Write a formatted record to an output file handle (see Day 20, “Odds and Ends.”) write is not the opposite of read |
In addition, the POSIX module provides a number of features for handling more sophisticated I/O (but, alas, only works on Unix). See the perlmod man page for more information about POSIX.
Perl provides built-in and module behavior for reading and writing files from and to Berkeley Unix DBM (database) files. These files can be smaller and faster to deal with than flat text-based databases. For more information about DBM, see the DB_File module, the tie function, and the various Tie modules (Tie::Hash, Tie::Scalar, and so on).
CPAN also provides a number of modules for dealing with databases—both those you write yourself, as well as interfaces to and drivers for various commercial databases such as Oracle and Sybase. For the latter, the DBD (Database Drivers) and DBI (Database Interface) packages by the Perl Database Initiative come highly recommended.
Files and directories can both contain timestamps, that is, indications of when that file was created, modified, or just accessed. You can test a file for its timestamp using the -M (modification), -A (access), and -C (inode change, Unix only) file tests, get more information about timestamps via the stat function, or change the timestamp of a file using the utime function. The behavior of these tests and functions might vary from platform to platform.
All times are in seconds elapsed since a certain date; that date is January 1, 1970 for Unix and Windows and January 1, 1904 on the Macintosh. The functions time, gmtime, localtime, and the Time::Local modules might also be of interest for decoding and changing timestamps.
3.15.214.155