in reply to Re: File Reading and Closing confusion
in thread File Reading and Closing confusion
-
That said, no, those are not the same: while(<FH>){...} assigns to $_, but $line=<FH> assigns to $line. Other than that, they are the same.
Actually, a crucial difference is that while(<FH>){...} gets interpreted by Perl as:
while (defined($_ = <FH>)) { ... }
which effectively ends the while loop once there are no more lines to be read from the filehandle. With $line=<FH>, we need to check for defined-ness.
-
my @lines = do { open my $fh, '<', $file or die "$file: $!\n"; <$fh> };
Another way:
my @lines = do { local @ARGV = $file; <> };
In Section
Seekers of Perl Wisdom