while(<$fh>) { $i++; $buf[$i%($p)] = $_; } my @tail = (@buf[ ($i%($p) + 1) .. $#buf ], @buf[ 0 .. $i%($p)]); for (@tail) { print if $_; }