my $MAXLINES=20; # not including header open my $fh, '+<', 'foo.txt' or die $!; my @lines = <$fh>; splice @lines, 1, @lines-$MAXLINES if @lines>$MAXLINES; push @lines, "newline\n"; truncate $fh, 0 or die "truncate failed"; seek $fh, 0, 0 or die "seek failed"; print $fh @lines; close $fh; #### use Tie::File; my $MAXLINES=20; # not including header tie my @lines, 'Tie::File', 'foo.txt' or die "tie failed"; splice @lines, 1, @lines-$MAXLINES if @lines>$MAXLINES; push @lines, "newline\n"; untie @lines;