You are on page 1of 17

A file handle is a label that we use to tell Perl

which file were intending to use.


STDIN of <STDIN> is a file handle for the
special file standard input that is used to read
text from the user typing on a keyboard.
The counterpart of STDIN is the STDOUT
file handle which we implicitly use when we
write print.
The third file handle STDERR is where error
messages printed by die go.

For writing/reading from files, we need file


handles.
File handles are usually one way, which means a
file can be opened for either writing or reading.
To open a file for reading, type:
open FH, $filename or die $!;

is the name of the file handle to the file name


specified by $filename. The second argument can
be a literal as well, like output.log.
When specifying the full path of a file on
Windows, make sure the reverse slashes are
either escaped or use forward slashes.

FH

If the file couldnt be opened for any reason,


the die statement will work. The error
message returned by die will be stored in the
special variable $!.
Printing the contents of the variable $! will let
us know what error was encountered.
die does not print a newline, so whenever you
use a die and print an error message, be sure
to include \n.

open FH,somefile.txt or die $!;


while(<FH>)
{
print $_;
}

The while loop reads the current line from the file
somefile.txt and print it to the console
Instead of while(<FH>), we could also write
while(defined ($_ = <FH>))
Instead of the whole while loop, we could concisely write:
print $_ while(<FH>);
The construct <> is called the readline or diamond operator.

ARGV is a special file handle which opens the


names of the files provided at the time of
running the program. If nothing is provided,
ARGV reads from the standard input.
@ARGV stores all the command line
arguments and the <ARGV> file handle stores
each file in turn.
Perl provides a shortcut for this: <>, the
empty diamond.

while(<>)
{
print $_;
}

This time, Perl will process the file names which


we provide at the time of running the program.
If more than one file name is given, Perl will
process them one by one. The name of the
current file is stored in the special variable
$ARGV.

Used in a scalar context, the file handle


returns only the next line; however used in a
list context, it will return the entire file.
my @file;
@file = <FH>;

Till now, weve been reading in lines from the


files we opened, because a line is the default
definition of a record. But this default behavior
can be changed.
We can do this by changing the value of the
special variable $/ (the record separator
variable). For example, if we set it to (empty
string), we can read in paragraphs from the text
file instead of single lines.
To read in the entire file into a string, set the
value of $/ to undef.

To open a file for writing, we write:


open FH, > filename or die $!;

This will either create a new file or wipe out


the contents of an existing file and start over
again. To append to an existing file, use >>
instead of >.
To write on to the file, we use:
print FH <list>;
where list is what we want written to the file.

On text files, Perl treats special characters like


newlines differently on different operating
systems. This processing is unnecessary for
binary files.
To tell Perl to compensate for binary files, we
use the binmode operator:
binmode FILEHANDLE;

Use binmode only with binary files, never text


files.

Perl gives us the function umask(expr) which


can be used to set user permissions on a file
we create. The expression it expects is a octal
number defining the permissions we want set
on the file.
For example, umask(0710) means oct 710
which in binary is (111,001,000) which means
the owner can read, write, execute the file; his
group can only execute, but not modify or
read; and everyone else has no permissions.

Just like standard input/output, data may be sent


to other programs as well using the concept of a
pipe.
A pipe in Perl connects (usually) the standard
output of a file to the standard input of another.
For example, we may have a Perl file sortMe.plx
that reads in a text file abc.txt and sorts it. If we
want to send the output of sortMe.plx to another
Perl script, say new.plx, we write:
perl sortMe.plx < abc.txt | perl new.plx

Perl provides us with file tests which allow us


to check various characteristics of files before
opening them.
If we want to check whether a file exists, we
write:
if (-e somefile.txt){}

For a list of commonly used file tests, see the


next page.

Just as with files, Perl also allows us to read


directories.
A glob is like a regular expression that allows us
to read directories. In a glob, * matches any
amount of text and ? would match any one
character. To get all the files in a directory, we
could say @fileNames = glob(*);
We could also read in directories just like lines in
a file, however for this, we would use opendir
instead of open. Similarly to read each directory,
we would use readdir.

What do the following regular expressions mean?

(1)
(2)
(3)

$var =~ /(\w+)$/
$code !~ /^#/
s/#{2,}/#/g

Write a program to read in a text file and print out the


last 5 lines in the file.
Write a program that reads in a text file and counts out
the number of times the word we occurs in it.
Write a program to sort the lines in a text file and print
them out to the screen.
Write a program that reads in the names of two text files
and copies the first into the second.
Visit learn.perl.org and read through the section on
regular expressions

You might also like