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

    Not quite.
    while (<STDIN>)
    is equivalent to
    while ($_ = <STDIN>)
    but when spelled out explicitly, they're both
    while (defined($_ = <STDIN>))

    >perl -MO=Deparse -e"while (<STDIN>) {}" while (defined($_ = <STDIN>)) { (); } -e syntax OK >perl -MO=Deparse -e"while ($_ = <STDIN>) {}" while (defined($_ = <STDIN>)) { (); } -e syntax OK
Re^2: <STDIN> not initializing $_
by ysth (Canon) on May 01, 2008 at 04:17 UTC
    Note that the implicit "$_ = " in a while/until expression is only added for <>, readline, or glob operations.

    There's an implicit defined() also, as ikegami notes, but that's broader. It is added when the while/until condition is a scalar assignment (itself possibly implicit as noted above) from a <>, readline, glob, readdir, or each operation.

      Thank you all, monks, for your insights regarding my question. I'm currently in the process of reading "Programming Perl". Today I discovered the book explicitly explains <> and $_. It also confirms your answers, especially ysth's. The first part of `Line Input (Angle) Operator' in `Input Operators' in Chapter 2 covers the subject thoroughly. A short quote:
      If and only if the line input operator is the only thing inside the conditional of a while loop, the value is automatically assigned to the special variable $_.
      After that some useful examples follow.
      Update: Ignore this post. I read "and a" instead of "from a".

      It is added when the while/until condition is a scalar assignment

      As far as I can tell, that's not true. The following prints nothing:

      perl -le"while ($a=0) { print 'ysth is right'; last }"

      Tested with Perl 5.6.0, 5.6.1, 5.8.0, 5.8.8, 5.10.0.

        An extra edge case:
        perl -le'while($a = 0){print "$a ok\n"; last }' # perl -le'while($a = 1){print "$a ok\n"; last }' # prints 1 ok
        But the "implicit test for defined" is built into the while( ) by language design, there is no extra magic about that in any context?
        You missed the most important part of the quote:
        It is added when the while/until condition is a scalar assignment .. from a <>, readline, glob, readdir, or each operation.

        The implicit defined is not added for normal scalar assignments.