A filehandle is a named internal Perl structure that associates a physical file with a name. All filehandles are capable of read/write access, so you can read from and update any file or device associated with a filehandle. However, when you associate a filehandle, you can specify the mode in which the filehandle is opened.
Three basic file handles are - STDIN, STDOUT, and STDERR.
There are following two functions with multiple forms which can be used to open any new or existing file in Perl.
open FILEHANDLE, EXPR;
sysopen FILEHANDLE, FILENAME, MODE, PERMS;
sysopen FILEHANDLE, FILENAME, MODE;
Here FILEHANDLE is the file handle returned by open function and EXPR is the expression having file name and mode of opening the file.
$append = 0;
open(OUTFILE, ">filename.out"); #open for write, overwrite
open(OUTFILE, ">>filename.out"); #open for write, append
print OUTFILE "Timestamp: "; #write text without newline
print OUTFILE join(',',localtime(time)); #write text
print OUTFILE "\n"; #write newline
### Print text usinh heredoc
print OUTFILE <<"MyLabel";
Steve was here
and now is gone
but left his name
to carry on.
# close the file
Opening for Read requires no angle brackets in the filename. If you wish, you can put in a left angle bracket <, which means "input file". It's good practice to close any files you open. Files can be read line by line, or the entire contents of the file can be dumped into a list, with each list element being a line. Here is an example of a program that reads a file, capitalizes each line, and prints it to the screen:
Reading a File a Line at a Time
# Good practice to store $_ value because
# subsequent operations may change it.
my($line) = $_;
# Good practice to always strip the trailing
# newline from the line.
# Convert the line to upper case.
$line =~ tr/[a-z]/[A-Z]/;
# Print the line to the screen and add a newline
Reading a Whole File at Once
Sometimes it's easier to read a whole file into a list, especially with complex break logic, read-ahead totals, or sorting. Here's a program that reads a file and prints it in sorted order:
open(INFILE, "<filename.out"); # open for input
my(@lines) = <INFILE>; # read file into list
@lines = sort(@lines); # sort the list
foreach $line (@lines) # loop through list
print "$line\n"; # print in sorted order
You might occasionally want to grab an entire file without paying attention to line termination. You can do that by undefing the $/ built in variable, and then assigning the <file> to a scalar. This is called "slurping" the file.
The following code slurps the STDIN file, then splits it into lines, then reassembles the lines into a single string, and prints the string:
my $recTerminator = $/;
local $/ = undef;
my $buf = <STDIN>;
local $/ = $recTerminator;
my @lines = split /$recTerminator/, $buf;
$buf = "init";
$buf = join $recTerminator, @lines;
The preceding code works like this:
- First we store the terminator character, which by default on Linux systems is linefeed -- "\n".
- Now we undef the line terminator character
- Now we slurp the entirety of STDIN
- Now we restore the line terminator character
- Now we split the string we read using the termator as a border
- Now we join the array back into a string
- We print the string
- Last but not least, we print an extra newline to fix a picket fence condition
Large buffer I/O is not efficient the way it is in C. If the file is large enough to save time by whole file reads, then it's so large as to exhaust electronic RAM memory, thus incurring swap penalties.
The most efficient algorithm reads a line, writes a line, and stores nothing. That's not always practical, and it's certainly not the easiest way to design code.
If you really want to get faster I/O in Perl, you might experiment with the sysopen(), sysread(), sysseek(), and syswrite() functions. But beware, they interact quirkily with normal Perl I/O functions.