in reply to Appending a file between XML markers

It's much easier to open the original file for reading, and write a complete new file to another location, and if that all succeeds rename() the new file over the old file:

open INPUT,"<","input.xml" or die $!; open OUTPUT,">","output.xml.tmp" or die $!; while (<INPUT>) { s/<\/markers>/$database_row<\/markers>/; # insert $database row bef +ore marker, if found on this line print OUTPUT $_; # print line to output file } close INPUT; close OUTPUT or die $!; rename "output.xml.tmp","input.xml";
If you're doing anything more interesting than your stated problem, it'll be worth it to use a real XML parser instead. I recommend XML::Twig.