my @cache = (undef) x 6; while (<>) { ... shift @cache; push @cache, $_; }