Standard Input
$line = <STDIN>; # read the next line
chomp($line); # and chomp it
chomp($line = <STDIN>); # same thing, more idiomatically
”’For array”’
@myarray = <stdin>
press Ctrl-D when done.
”’Diamond operator <>”’
<> operator is used if you want to make a perl program that can be used like the UNIX utilities such as cat, sed, awk, sort, grep lpr etc. The diamond operator is actually a special kind of line input operator. But instead of getting the input from the keyboard, it comes from the user’s choice of input.
while (defined($line = <>)) {
chomp($line);
print “$line\n”;
}
This program when executed with a filename as arguments will print the content of file line by line as of cat.
$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”;
}
shortcut
while (<STDIN>) {
print “I saw $_”;
}