in reply to <STDIN> not initializing $_
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: <STDIN> not initializing $_
by ikegami (Patriarch) on May 01, 2008 at 01:02 UTC | |
|
Re^2: <STDIN> not initializing $_
by ysth (Canon) on May 01, 2008 at 04:17 UTC | |
by carol (Beadle) on May 03, 2008 at 11:44 UTC | |
by ikegami (Patriarch) on May 01, 2008 at 04:32 UTC | |
by stiller (Friar) on May 01, 2008 at 05:39 UTC | |
by mscharrer (Hermit) on May 01, 2008 at 07:33 UTC |