Felix Geerinckx wrote: > > on Tue, 14 May 2002 16:11:00 GMT, [EMAIL PROTECTED] (Drieux) wrote: > > > or Why the complex > > > > defined(my $thisLine = <STDIN>) > > But this exactly equivalent to your 'while (<STDIN>) { ...}', > except that it uses an explicit loop variable instead of $_. > The 'defined' is necessary for the rare occasions where you have '0' > as your last line, without trailing "\n", which would evaluate to > false, but would still be defined.
This is only true on older versions of Perl (I don't remember when they changed it.) On recent versions defined() is not required and it still does the Right Thing. perldoc perlop [snip] 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 $_; ... } John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]