2.8. Getting User Input
At this point, you're probably wondering how to get a value
from the keyboard into a Perl program. Here's the simplest way:
use the line-input operator,
<STDIN>
.[61] Each time you use <STDIN> in a
place where a scalar value is expected, Perl reads the next complete
text line from standard
input (up to the first newline), and uses that
string as the value of <STDIN>. Standard
input can mean many things, but unless you do something uncommon, it
means the keyboard of the user who invoked your program (probably
you). If there's nothing waiting to be read (typically the
case, unless you type ahead a complete line), the Perl program will
stop and wait for you to enter some characters followed by a newline
(return).[62]
The string value of <STDIN> typically has a
newline character on the end of it.[63] So you could do something like this:
$line = <STDIN>;
if ($line eq "\n") {
print "That was just a blank line!\n";
} else {
print "That line of input was: $line";
}
But in practice, you don't often want to keep the newline, so
you need the chomp operator.
 |  |  | 2.7. The if Control Structure |  | 2.9. The chomp Operator |
Copyright © 2002 O'Reilly & Associates. All rights reserved.
|
|