in reply to Re: <STDIN> not initializing $_
in thread <STDIN> not initializing $_

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.

Replies are listed 'Best First'.
Re^3: <STDIN> not initializing $_
by carol (Beadle) on May 03, 2008 at 11:44 UTC
    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.
Re^3: <STDIN> not initializing $_
by ikegami (Patriarch) on May 01, 2008 at 04:32 UTC
    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.