File Handles


Beginning | Previous | Next

What are They Handles To?

These handles can be associated with files, pipes or socket connections (depending on how your version of Perl was built). The most common use, is a handle to a file.

Built-in File Handles

There are three built-in file handles that you get for free: STDIN, STDOUT and STDERR. These are the familiar stdin, stdout and stderr that C programmers have come to know and love.

Creating File Handles

You can "create" a file handle with the open() function call, as in: # open $log for reading open (LOG_FILE, $log) or die "Can't open $log: $!\n"; # the same as above open (LOG_FILE, "<$log") or die "Can't open $log: $!\n"; # create file and write to it open (LOG_FILE, ">$log") or die "Can't open $log: $!\n"; # create file and write and read open (LOG_FILE, "+>$log") or die "Can't open $log: $!\n"; # append to existing file open (LOG_FILE, ">>$log") or die "Can't open $log: $!\n"; open (LOG_FILE, "| output-pipe-command") or die "Can't open pipe: $!\n"; # set up output pipe filter open (LOG_FILE, "input-pipe-command |") or die "Can't open pipe: $!\n"; # set up input pipe filter The convention is to use all uppercase characters for file handles.

Using File Handles

Some examples of using file handles include: # Read a line from the keyboard (or stdin). $answer = <STDIN>; # Write a line to a file on disk. print LOF_FILE ("Fatal error in I/O sub-system. " . "System going down\n"); # Read in all the contents of a file into an array. @contents = <RIGHTEOUS_DATA>; # array context Beginning | Previous | Next
Last Modified: $Date: 1997/05/02 07:17:38 $