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; <> };
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: File Reading and Closing confusion
by shmem (Chancellor) on Aug 09, 2010 at 00:23 UTC | |
by repellent (Priest) on Aug 09, 2010 at 03:49 UTC | |
by shmem (Chancellor) on Aug 09, 2010 at 09:30 UTC | |
by repellent (Priest) on Aug 09, 2010 at 15:37 UTC |
In Section
Seekers of Perl Wisdom