Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: $_ not set in while <>

by LanX (Sage)
on Jun 06, 2021 at 12:46 UTC ( #11133584=note: print w/replies, xml ) Need Help??


in reply to Re^2: $_ not set in while <>
in thread $_ not set in while <>

You seem to believe that the OP wants to read manual input from STDIN², but - as kcott noted - the OP said files.

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 loop
while (<>) { ... # code for each line }
is equivalent to the following Perl-like pseudo code:
unshift(@ARGV, '-') unless @ARGV; while ($ARGV = shift) { open(ARGV, $ARGV); while (<ARGV>) { ... # 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.)

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11133584]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2021-10-23 02:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (86 votes). Check out past polls.

    Notices?