#!/usr/bin/perl use warnings; use strict; my @positions; my $read_through = 0; while (push(@positions,tell(STDIN)), defined(my $line = )) { print "got line: $line"; # if the line contains a $ and this is the first time we've # got this far in the file, jump back 3 if (index($line, '$') >= 0 && $read_through < $positions[-1]) { $read_through = $positions[-1]; seek(STDIN, $positions[-4] || 0, 0) or die "whoops, couldn't seek: $!"; } # do stuff with $line # only keep positions for last three lines splice(@positions, 0, -3, ()); }