Re: <> tag in perl
- From: rpdooling@xxxxxxxxx
- Date: 28 Oct 2005 16:38:29 -0700
To go with Paul's answer:
from http://cslibrary.stanford.edu/108/EssentialPerl.html
(great little refresher course or intro on the essentials)
STDIN, STDOUT, and STDERR are standard filehandles that are
automatically opened before the program runs.
Surrounding a file handle with <> is an expression that returns one
line from the file including the "\n" character, so <STDIN> returns one
line from standard input.
The <> operator returns undef when there is no more input.
The "chop" operator removes the last character from a string, so it can
be used just after an input operation to remove the trailing "\n". The
"chomp" operator is similar, but only removes the character if it is
the end-of-line character.
$line = <STDIN>; ## read one line from the STDIN file handle
chomp($line); ## remove the trailing "\n" if present
$line2 = <FILE2>; ## read one line from the FILE2 file handle
## which must be have been opened previously
Since the input operator returns undef at the end of the file, the
standard pattern to read all the lines in a file is...
# read every line of a file
while ($line = <STDIN>) {
## do something with $line
}
.
- References:
- <> tag in perl
- From: RSummersJr
- <> tag in perl
- Prev by Date: Re: grep help request -- data fields max size
- Next by Date: Re: Perl/sh/bash scripts and emacs
- Previous by thread: Re: <> tag in perl
- Next by thread: Re: grep help request
- Index(es):
Relevant Pages
|