my @lines ; push @lines, scalar readline $handle for 1 .. 5; while( readline $handle ){ ... shift @lines; push @lines, $_; }