in reply to Re^2: Playing with non-localized $_ in nested loops.
in thread Playing with non-localized $_ in nested loops.

The automatic setting of $_ comes from the combination of while() and readline. Just a plain readline won't do it. Nor does while with just any operator do the implicit assignment.
  • Comment on Re^3: Playing with non-localized $_ in nested loops.

Replies are listed 'Best First'.
Re^4: Playing with non-localized $_ in nested loops.
by davido (Cardinal) on Aug 23, 2004 at 04:36 UTC
    <DATA>; print $_; __DATA__ This is a test. __OUTPUT__ Use of uninitialized value in print at mytest.pl line 8, <DATA> line 1 +.

    Hmm... You're right. There goes that theory. ;) I was erroneously sure that the while() loop's conditional had nothing to do with the fact that the diamond operator feeds $_. Thanks for the enlightenment. :D


    Dave

      There are actually two pieces of dwimmery going on; first, if a readline or a glob is the only thing in a while condition (whether a statement modifier or while statement), it gets an implicit $_ =. Next, if a while condition is a scalar assignment from a readline, readdir, glob, or each, the whole condition gets an implicit defined() wrapped around it, so the loop continues on "0" or "".

      That is why, in the node you referenced, I was talking about while(<FH>). Invoking the DWIMmery really does require this entire combination of operations.

      Makeshifts last the longest.