home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book HomeLearning Perl, 3rd EditionSearch this book

Chapter 6. I/O Basics

We've already seen how to do some input/output (I/O), in order to make some of the earlier exercises possible. But now we'll learn a little more about those operations. As the title of this chapter implies, there will be more about Perl's I/O operations in Chapter 11, "Filehandles and File Tests".

6.1. Input from Standard Input

Reading from the standard input stream is easy.[139] We've been doing it already with the <STDIN> operator.[140] Evaluating this operator in a scalar context gives you the next line of input:

[139]If you're already familiar with the workings of standard input, output, and error streams, you're ahead of the game. If not, we'll get you caught up when we get to Chapter 14, "Process Management". For now, just think of "standard input" as being "the keyboard," and "standard output" as being "the display screen."

[140]What we're calling the line-input operator here, <STDIN>, is actually a line-input operator (represented by the angle brackets) around a filehandle. We'll learn about filehandles in Chapter 11, "Filehandles and File Tests".

$line = <STDIN>;                # read the next line
chomp($line);                   # and chomp it

chomp($line = <STDIN>);         # same thing, more idiomatically

Since the line-input operator will return undef when you reach end-of-file, this is handy for dropping out of loops:

while (defined($line = <STDIN>)) {
  print "I saw $line";
{

There's a lot going on in that first line: we're reading the input into a variable, checking that it's defined, and if it is (meaning that we haven't reached the end of the input) we're running the body of the while loop. So, inside the body of the loop, we'll see each line, one after another, in $line.[141] This is something you'll want to do fairly often, so naturally Perl has a shortcut for it. The shortcut looks like this:

[141]You probably noticed that we never chomped that input. In this kind of a loop, you can't really put chomp into the conditional expression, so it's often the first item in the loop body, when it's needed. We'll see examples of that in the next section.

while (<STDIN>) {
  print "I saw $_";
}

Now, to make this shortcut, Larry chose some useless syntax. That is, this is literally saying, "Read a line of input, and see if it's true. (Normally it is.) And if it is true, enter the while loop, but throw away that line of input!" Larry knew that it was a useless thing to do; nobody should ever need to do that in a real Perl program. So, Larry took this useless syntax and made it useful.

What this is actually saying is that Perl should do the same thing as we saw in our earlier loop: it tells Perl to read the input into a variable, and (as long as the result was defined, so we haven't reached end-of file) then enter the while loop. However, instead of storing the input into $line, Perl will use its favorite default variable, $_, just as if you had written this:

while (defined($_ = <STDIN>)) {
  print "I saw $_";
}

Now, before we go any further, we must be very clear about something: this shortcut works only if you write it just as we did. If you put a line-input operator anywhere else (in particular, as a statement all on its own) it won't read a line into $_ by default. It works only if there's nothing but the line-input operator in the conditional of a while loop.[142] If you put anything else into the conditional expression, this shortcut won't apply.

[142]Well, okay, the conditional of a for loop is just a while conditional in disguise, so it works there, too.

There's no connection between the line-input operator (<STDIN>) and Perl's favorite default variable ($_). In this case, though, it just happens that the input is being stored in that variable.

On the other hand, evaluating the line-input operator in a list context gives you all of the (remaining) lines of input as a list -- each element of the list is one line:

foreach (<STDIN>) {
  print "I saw $_";
}

Once again, there's no connection between the line-input operator and Perl's favorite default variable. In this case, though, the default control variable for foreach is $_. So in this loop, we'll see each line of input in $_, one after the other.

That may sound familiar, and for good reason: That's the same behavior as the while loop would do. Isn't it?

The difference is under the hood. In the while loop, Perl reads a line of input, puts it into a variable, and runs the body of the loop. Then, it goes back to find another line of input. But in the foreach loop, the line-input operator is being used in a list context (since foreach needs a list to iterate through). So it has to read all of the input before the loop can start running. That difference will become apparent when the input is coming from your 400MB web server log file! It's generally best to use code like the while loop's shortcut, which will process input a line at a time, whenever possible.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.











??????????????@Mail.ru