Learning Perl

Learning PerlSearch this book
Previous: 9.7 ExercisesChapter 10Next: 10.2 Opening and Closing a Filehandle
 

10. Filehandles and File Tests

Contents:
What Is a Filehandle?
Opening and Closing a Filehandle
A Slight Diversion: die
Using Filehandles
The -x File Tests
The stat and lstat Functions
Exercises

10.1 What Is a Filehandle?

A filehandle in a Perl program is the name for an I/O connection between your Perl process and the outside world. We've already seen and used filehandles implicitly: STDIN is a filehandle, naming the connection between the Perl process and the UNIX standard input. Likewise, Perl provides STDOUT (for standard output) and STDERR (for standard error output). These names are the same as those used by the C and C++ "standard I/O" library package, which Perl uses for most of its I/O.

Filehandle names are like the names for labeled blocks, but they come from yet another namespace (so you can have a scalar $fred, an array @fred, a hash %fred, a subroutine &fred, a label fred, and now a filehandle fred). Like block labels, filehandles are used without a special prefix character, and thus might be confused with present or future reserved words. Once again, the recommendation is that you use ALL UPPERCASE letters in your filehandle; not only will it stand out better, but it will also guarantee that your program won't fail when a future reserved word is introduced.


Previous: 9.7 ExercisesLearning PerlNext: 10.2 Opening and Closing a Filehandle
9.7 ExercisesBook Index10.2 Opening and Closing a Filehandle