Filehandles

A filehandle is the name in a Perl program for an I/O connection between your Perl process and the outside world. That is, it’s the name of a connection and not necessarily the name of a file.

Filehandles are named like other Perl identifiers (letters, digits, and underscores, but they can’t start with a digit); since they don’t have any prefix character, they might be confused with present or future reserved words, or with labels, which we will cover in Chapter 10. Once again, as with labels, the recommendation from Larry is that you use all uppercase letters in the name of your filehandle. It will stand out better and will guarantee your program won’t fail when a future (lowercase) reserved word is introduced.

Perl uses six special filehandle names for its own purposes: STDIN, STDOUT, STDERR, DATA, ARGV, and ARGVOUT.[135] Though you may choose any filehandle name you’d like, you shouldn’t choose one of those six unless you intend to use that one’s special properties.[136]

Maybe you’ve recognized some of those names. When your program starts, STDIN is the filehandle naming the connection between the Perl process and wherever the program should get its input, known as the standard input stream. This is generally the user’s keyboard unless the user asked for something else to be the source of input, such as a file or the output of another program through a pipe.[137] STDOUT is the standard output stream. By default, this one goes to the user’s display screen, but the user ...

Get Learning Perl, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.