in reply to Re^2: $_ not set in while <>
in thread $_ not set in while <>
Please note that an empty diamond <> defaults to <ARGV> .
And ARGV only defaults to "-" ° if the array @ARGV is empty.
So if you want to read from STDIN, better say so explicitly by writing <STDIN> .
from perlop
Input from <> comes either from standard input, or from each file listed on the command line. Here's how it works: the first time <> is evaluated, the @ARGV array is checked, and if it is empty, $ARGV[0] is set to "-", which when opened gives you standard input. The @ARGV array is then processed as a list of filenames. The loopis equivalent to the following Perl-like pseudo code:while (<>) { ... # code for each line }except that it isn't so cumbersome to say, and will actually work. It really does shift the @ARGV array and put the current filename into the $ARGV variable. It also uses filehandle ARGV internally. <> is just a synonym for <ARGV>, which is magical. (The pseudo code above doesn't work because it treats <ARGV> as non-magical.)unshift(@ARGV, '-') unless @ARGV; while ($ARGV = shift) { open(ARGV, $ARGV); while (<ARGV>) { ... # code for each line } }
NB: took me a while to find this, IMHO this should be at least mentioned/referenced in @ARGV
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
°) IIRC "-" is just an alias for STDIN which can be passed literally as argument at the command line
²) STDIN is not necessarily the keyboard, it depends how the program was started
|
|---|