Section 5.1. Input from Standard Input


5.1. Input from Standard Input

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

[*] What we're calling the line-input operator here, <STDIN> is actually a line-input operator (represented by the angle brackets) around a filehandle. You'll learn about filehandles later in this chapter.

     $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.[*] This is something you'll want to do fairly often, so naturally Perl has a shortcut for it. The shortcut looks like this:

[*] You probably noticed that we never chomped that input. In this kind of a loop, you can't 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 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, $_, as if you had written this:

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

Now, before we go any further, we must be clear about something: this shortcut workparticular, 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.[] If you put anything else into the conditional expression, this shortcut wont apply.

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

Otherwise, there's no other connection between the line-input operator (<STDIN>) and Perl's favorite default variable ($_). In this case, 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, and 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 this loop places 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, 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 whenever possible, since it processes input one line at a time.



Learning Perl
Learning Perl, 5th Edition
ISBN: 0596520107
EAN: 2147483647
Year: 2003
Pages: 232

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net