in reply to Re: Read Last Line of A File Only
in thread Read Last Line of A File Only
Or better yet, using the safer syntax options available in 5.6 and later:
my $lastline = do { open my $pipe, '-|', tail => -1, $file or die "Can't spawn tail: $!\n"; <$pipe>; }; print $lastline;
I'm not sure why you chose to write your loop that way, either. Why the obfuscatory infinite loop with an abort condition as in this?
while( 1 ) { my $file = shift || last; # ... }
It took me a moment to realize that you meant something entirely different than what I assumed at first glance. The natural thing would have been to put the abort condition where you normally put it: in the loop condition.
while( @ARGV ) { my $file = shift; # ... }
Of course, that's clearer written as
for my $file ( @ARGV ) { # ... }
So we get
for my $file ( @ARGV ) { my $lastline = do { open my $pipe, '-|', tail => -1, $file or die "Can't spawn tail: $!\n"; <$pipe>; }; print $lastline; }
I find that significantly clearer despite the changes not changing any of the semantics.
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Read Last Line of A File Only
by bronto (Priest) on Aug 24, 2004 at 10:20 UTC | |
by Aristotle (Chancellor) on Aug 24, 2004 at 11:04 UTC |