Reading lines in reverse order:
use File::ReadBackwards;
my $back = File::ReadBackwards->new(shift @ARGV) or die $!;
print while defined( $_ = $back->readline );
At first, I misunderstood the question and thought that you want to read back and forth in alternating fashion , which seems more interesting. :)
use strict;
use warnings;
use File::ReadBackwards ;
sub read_forward_and_backward {
my ($file_name, $read_from_tail) = @_;
my $back = File::ReadBackwards->new($file_name) or die $!;
open my $forw, '<', $file_name or die $!;
my $line;
return sub {
return if $back->tell <= tell($forw);
$line = $read_from_tail ? $back->readline : <$forw>;
$read_from_tail = not $read_from_tail;
return $line;
}
}
my $iter = read_forward_and_backward(@ARGV);
print while defined( $_ = $iter->() );
source
share