in reply to Doubt on defined-ness

while (<FH>) ...

is the same as doing

while ($_=<FH>) ...
Not quite. while (<FH>) is shorthand for
while (defined($_ = <FH>))

Which should clear up the other half of your confusion.


Unless I state otherwise, all my code runs with strict and warnings

Replies are listed 'Best First'.
Re^2: Doubt on defined-ness
by waldner (Beadle) on Jun 18, 2008 at 19:25 UTC
    The camel book lists the following (among others) as being equivalent:
    while (defined($_=<STDIN>)) ... while ($_=<STDIN>) ... while (<STDIN>) ...
    so I guess that only in the case of a while both the second and third form are equivalent to the first. This is not true for other boolean context like eg if.
      It seems perl applies its magic test for definedness to any while that involves a filehandle (at least, I couldn't find a case where it didn't apply).
      $ perl -MO=Deparse -e 'while (<>) {} while ( <ARGV> ) {} while ( $_ = +<> ) {} while ( $x = <> ) {}' while (defined($_ = <ARGV>)) { (); } while (defined($_ = <ARGV>)) { (); } while (defined($_ = <ARGV>)) { (); } while (defined($x = <ARGV>)) { (); } -e syntax OK


      Unless I state otherwise, all my code runs with strict and warnings

        That's almost the limit. Anything more complex doesn't add a defined.

        >perl -MO=Deparse -e"while (<FILE> && foo()) {}" while (<FILE> and foo()) { (); } -e syntax OK