in reply to Using File::ReadBackwards or equivalent on pre-existing file handle

You can use tell to get the line beginnings and store them in an array. Once you reach the end of the input, use seek to retrieve the lines:
#!/usr/bin/perl use warnings; use strict; my @positions; open my $OUT, '+>', '2.out' or die $!; for my $i (1 .. 20) { unshift @positions, tell $OUT; print {$OUT} "line $i\n"; } open my $BACK, '>', '2.back' or die $!; for my $pos (@positions) { seek $OUT, $pos, 0; my $line = <$OUT>; print {$BACK} $line; }

Update: Used the +> mode to keep the filehandle open.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Using File::ReadBackwards or equivalent on pre-existing file handle
by HYanWong (Acolyte) on Jun 25, 2014 at 23:00 UTC

    Somewhat sneaky, thanks. I was trying to avoid intermediate storage, as in the worst case I need to store 2.5 million positions (lines in the file), but I guess that's doable on modern machines.