Finally I think I've found the right answer to my question. And sorry guys I don't think you all get the right point
@theNbomr
Empty string, "0", '0' and 0 (integer) are all evaluated False in the while conditional. It's not about forcing the integer context, it's a scalar context.
@David the H.
You are close. But there is no such "the whole expression being evaluated" thing. Just like C, such kind of conditional depends on the value of the expression, not the expression itself.
Post the right answer here so that we can all learn something
Code:
The following lines are equivalent:
while (defined($_ = <STDIN>)) { print; }
while ($_ = <STDIN>) { print; }
while (<STDIN>) { print; }
for (;<STDIN>;) { print; }
print while defined($_ = <STDIN>);
print while ($_ = <STDIN>);
print while <STDIN>;
This also behaves similarly, but avoids $_ :
while (my $line = <STDIN>) { print $line }
In these loop constructs, the assigned value (whether assignment is automatic or explicit) is then tested to see
whether it is defined. The defined test avoids problems where line has a string value that would be treated as false
by Perl, for example a "" or a "0" with no trailing newline. If you really mean for such values to terminate the
loop, they should be tested for explicitly:
while (($_ = <STDIN>) ne '0') { ... }
while (<STDIN>) { last unless $_; ... }
In other boolean contexts, "<filehandle>" without an explicit "defined" test or comparison elicit a warning if the
"use warnings" pragma or the -w command-line switch (the $^W variable) is in effect.
This could be found in perldoc perlop - I/O operators.