On 7/19/07, John W. Krahn <[EMAIL PROTECTED]> wrote:
Chas Owens wrote:
> On 7/19/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>> On Jul 18, 6:16 am, [EMAIL PROTECTED] (Ayesha) wrote:
>> >
>> > when I use while($line1 = <MYFILE>){
>> > $line = <MYFILE>;
>> > }
>>
>> while ($line=<FH>)
>> {
>> print"$line";
>>
>> }
>
> 2. while ($line = <FH>) is not safe; use while (defined (my $line =
> <FH>)) instead
While the explicit use of defined may have been required in pre-Perl5 versions
that is not the case in current versions where defined is implied in certain
circumstances.
$ perl -MO=Deparse -e'while ( $line = <> ) { }'
while (defined($line = <ARGV>)) {
();
}
-e syntax OK
$ perl -MO=Deparse -e'while ( $line = readdir DH ) { }'
while (defined($line = readdir DH)) {
();
}
-e syntax OK
snip
It looks like the version of Perl in my head has outdated warnings.
What follows is for pedants only.
First a warning was added to tell people what I said earlier
Version 5.003_18: "Warn on '{if,while} ($x = X)' where X is glob,
readdir, or <FH>"
somewhere between 5.3.18 and 5.3.23 it was changed from a warning to
magically doing the "right" thing.
Then a bug fix to handle an alternate version of while
Version 5.003_23: "Protect against '0' in 'stmt while <HANDLE>'"
In 5.5 (aka 5.005) the warning that was removed and first appearance in pod
"while($x=<>) no longer warns (implicit defined added)"
perldoc perlop
and this also behaves similarly, but avoids the use of $_ :
while (my $line = <STDIN>) { print $line }
If you really mean such values to terminate the loop they should be
tested for explicitly:
while (($_ = <STDIN>) ne '0') { ... }
while (<STDIN>) { last unless $_; ... }
So, this has been the case since 1998-07-22 (but for a few bugs that
have cropped up and been fixed). Of course, I originally learned Perl
originally from 2nd Llama (1997) and 2nd Camel (1996) which might
explain why my (mental) version of Perl is a little buggy.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/