The part that you may be missing is that '$_' is not created by <STDIN> - it's created as a result of the 'while' loop, in the same way that it would be created if you'd used a 'for' loop.
while (<STDIN>){ # Loops over <STDIN> as long as it returns ' +true' print; } while ($_ = <STDIN>){ # Exact equivalent of the above but "spelled + out" explicitly print $_; # Ditto for this line - exactly equvalent to + a simple 'print;' } for (1,2,3){ # Loop over specified list print; # Print out each item (which has been assign +ed to $_) in turn } for $_ (1,2,3){ # Exact equivalent of the above but "spelled + out" explicitly print $_; # Same story }
In general, whenever you see a variable being omitted (e.g., 's/foo/bar/' instead of '$xyz =~ s/foo/bar/'), '$_' is being used. It's part of the standard Perl idiom, and you need to get familiar with it. At the very least, you'll need it to read other people's code.
-- Human history becomes more and more a race between education and catastrophe. -- HG Wells
In reply to Re: <STDIN> not initializing $_
by oko1
in thread <STDIN> not initializing $_
by carol
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |