Files and redirection
Files can be accessed directly by associating a new filehandle
with the actual name of a file, using the function
open . The syntax is curiously familiar. To open a file
(tempfile , say) for reading, using the
filehandle TMP , either of the following will work:
open(TMP, "tempfile");
open(TMP, "<tempfile");
To open tempfile , for writing using the
TMP filehandle, use the following if you want the
current contents of the file to be deleted (just as for the
> file redirection mechanism in the shell):
open(TMP, ">tempfile");
The data written to the file can to be appended using
the following syntax (as the >> file redirection
mechanism in the shell):
open(TMP, ">>tempfile");
For the time being, use printf (syntax virtually
identical to that in the shell) to send output using a filehandle
(if you omit the filehandle, STDOUT is assumed). To
display Hello~World in file tempfile , the
code fragment would be:
open(TMP, ">tempfile");
print TMP "Hello World!\n";
close(TMP);
Note the use of close - there are system limits on
the number of files than can be opened at any one time, and it is
good practice to "close" files when you have finished writing to
(or reading from) them.
Reading a text file or stream is performed by enclosing the
filehandle in angle brackets. Look at the following dialogue:
$v = <STDIN>;
open(TMP, "tempfile");
$w = <TMP>;
close(TMP);
The scalar variable v is set to the next line of
standard input, and w to the first line of
tempfile . Both v and w
contain the newline character at the end of the line (unlike the
shell command read or Awk) - this can be removed by
use of the command chomp :
chomp($v);
A while loop can be used to go through all the
lines in a file or stream. The syntax is
while (< filehandle>) {
... }
The variable $_ contains the current line
of input that has been read in.
Worked example
Write a Perl script to take the name of a file as its first
argument, and then read standard input and copy it to that
file.
Solution: The ARGV array contains the
filename as its first element. Open that file for writing with a
filehandle, and loop through the lines of standard input, not
forgetting to close the file after use.
# Set the filename variable to the first argument
$filename=$ARGV[0];
# Open the file
open(FILE, ">$filename");
# Repeatedly read in lines from standard input
while (<STDIN>) {
# Print each line to the file
print FILE $_;
}
# Finally, close the file
close(FILE);
|