in reply to How to delete and add line in the middle of a file?

Reading the file into an array might be a big mistake, instead, use the in-place editing:
local ($^I,@ARGV)=('.bak','file.html'); my $p=1; while(<>){ $p-- if /<\/tr>/; print "Add this line\n" if $p==-4 print if $p; $p-- if /<tr>/; }
You can read more about in-place editing in perlvar($^I) and perlrun(-i).