Perl makes it very easy for you to read input, from either the keyboard or
a file, with the ``diamond operator'' (<>
). Each call to this
operator will return one line from the current input source, which can be
stored in a variable for later use. (In this setting, a line is a string
of characters terminated with the input record separator, stored in the
automatic variable $/
, whose default value is the newline character.)
In many cases, you'll supply this
operator with a name, known as a filehandle (See Section ),
but the diamond operator is also very useful when used all by itself, as
explained later in this section.
To provide some simple examples, we can use the filehandle STDIN
. This
filehandle is always available inside of perl, and it represents what is known
as the standard input stream, usually being input from the keyboard. So to
ask a user for their name, and to store that name in a variable called
name
, we could write:
print "What is your name? "; $name = <STDIN>;First, note that there is no newline at the end of the question which will be printed; this is so that the cursor will be on the same line as the question when the user types his name. Perl treats newlines as any other character; it won't automatically put them at the end of your print statements. (The automatic variable
$\
, which defaults to an empty string,
could be set to newline if you wanted perl to always use a newline at the end
of print statements.) Perl will also retain the newline in the output of the
diamond operator. For this reason, perl provides a function called
chomp
which will remove a newline from the end of a scalar character
variable if it is present. It's very common in perl to see occurrences of
the diamond operator immediately followed by a call to chomp
, since we
usually don't want the newline as part of the text we're reading.
In the previous example, the target of the diamond operator was a scalar, and the operator returned a single line. If the target of the operator is an array, then it will return an array containing the entire input stream. Each element of the array will contain one line from the input stream. Thus, the statement:
@names = <STDIN>;will result in the
@names
array containing as many elements as
there were lines of input, with each element representing one line of the input
(including its newline character). To signal the end of input from the standard
input stream, type a control-D (Unix) or a control-Z (Windows) on a line by itself.
Reading a single line from a file isn't that useful; generally we'll want to
read each line from a file one at a time, and process them. Of course, there
are many ways to do this in perl. Using a while
loop, we can store the
result
of the diamond operator in a variable called $line
. Since the operator will
return an undefined value when there is no more input, we can use the assignment
to the variable $line
as the test expression in a while
statement:
while($line = <STDIN>){ # do something with $line here }Alternatively, we could read the file all at once, and loop over the individual lines at our leisure:
@allofit = <STDIN>; foreach $line (@allofit){ # do something with $line here }Using the diamond operator to process every line of a file or other input stream is such a common operation that perl has a special construct to deal with it. When the test expression of a while loop consists solely of a diamond operator, perl puts the operator's output into the special variable
$_
. Although
this kind of ``magic'' takes a little getting used to, this is
the technique most perl programmers use when reading a file:
while(<STDIN>){ # do something with $_ here }Since most functions which expect a single argument will use the value of
$_
as a default if no argument is given, it's very common to
see perl programs which look like this:
while(<STDIN>){ chomp; @T = split; print "The first word was $T[0]\n"; }This is exactly equivalent to the following loop, but most perl programmers prefer the
$_
approach, since it eliminates so much typing.
while($line = <STDIN>){ chomp $line; @T = split(' ',$line); print "The first word was $T[0]\n"; }
Just to show that there really is more than one way to do it in perl, suppose
you want to get the entire contents of a file in a single scalar variable,
instead of, say, reading the file one line at a time or creating an array
whose elements are the lines of the file. Simply set the input record separator
($/
) to some character that can never occur with a statement like
$/ = 0777;and a statement like
$file = <STDIN>;will read from standard input until the end of input is signaled.