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

Book HomeLearning Perl, 3rd EditionSearch this book

3.9. <STDIN> in List Context

One previously seen operator that returns a different value in an array context is the line-input operator, <STDIN>. As described earlier, <STDIN> returns the next line of input in a scalar context. Now, in list context, this operator returns all of the remaining lines up to the end of file. Each line is returned as a separate element of the list. For example:

@lines = <STDIN>; # read standard input in list context

When the input is coming from a file, this will read the rest of the file. But how can there be an end-of-file when the input comes from the keyboard? On Unix and similar systems, including Linux and Mac OS X, you'll normally type a Control-D[92] to indicate to the system that there's no more input; the special character itself is never seen by Perl,[93] even though it may be echoed to the screen. On DOS/Windows systems, use Ctrl-Z instead.[94] You'll need to check the documentation for your system or ask your local expert, if it's different from these.

[92]This is merely the default; it can be changed by the sttycommand. But it's pretty dependable -- we've never seen a Unix system where a different character was used to mean end-of-file from the keyboard.

[93]It's the OS that "sees" the control key and reports "end of file" to the application.

[94]There's a bug affecting some ports of Perl for DOS/Windows where the first line of output to the terminal following the use of Control-Z is obscured. On these systems, you can work around this problem by simply printing a blank line ("\n")after reading the input.

If the person running the program types three lines, then presses the proper keys needed to indicate end-of-file, the array ends up with three elements. Each element will be a string that ends in a newline, corresponding to the three newline-terminated lines entered.

Wouldn't it be nice if, having read those lines, you could chomp the newlines all at once? It turns out that if you give chomp a list of lines, it will remove the newlines from each item in the list. For example:

@lines = <STDIN>; # Read all the lines
chomp(@lines);    # discard all the newline characters

But the more common way to write that is with code similar to what we used earlier:

chomp(@lines = <STDIN>); # Read the lines, not the newlines

Although you're welcome to write your code either way in the privacy of your own cubicle, most Perl programmers will expect the second, more compact, notation.

It may be obvious to you (but it's not obvious to everyone) that once these lines of input have been read, they can't be re-read.[95] Once you've reached end-of-file, there's no more input out there to read.

[95]Well, yes, if the input is from a source upon which you can seek, then you'll be able to go back and read again. But that's not what we're talking about here.

And what happens if the input is coming from a 400MB log file? The line input operator reads all of the lines, gobbling up lots of memory.[96] Perl tries not to limit you in what you can do, but the other users of your system (not to mention your system administrator) are likely to object. If the input data is large, you should generally find a way to deal with it without reading it all into memory at once.

[96]Typically, that's much more memory than the size of the file, too. That is, a 400MB file will typically take up at least a full gigabyte of memory when read into an array. This is because Perl will generally waste memory to save time. This is a good tradeoff; if you're short of memory, you can buy more; if you're short on time, you're hosed.

Library Navigation Links

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