in reply to Appending a file between XML markers

If I understand things right you just need to overcome the stupid decision of the XML specification authors to require that a XML file is all onclosed in a single tag effectively preventing appending to XML files, right?

One solution would be to leave of the closing marker from the file as it's being appended to and only add it whenever you need to read it. The other would be to open the file in <+ mode, seek to a position a few bytes before the end and write the data and the closing tag.

my $marker = "<marker>\n"; my $endmarker = "</marker>\n"; my $marker_length = length($endmarker); $marker_length++ if $^O =~ /MSWin/; # because of the \n -> CRLF conver +sion sub append { my ($file, $data) = @_; if (!-s $file) { open my $FH, '>', $file or die qq{Can't open "$file" : $^E\n}; print $FH $marker, $data, $endmarker; close $FH; } else { open my $FH, '+<', $file or die qq{Can't open "$file" : $^E\n} +; seek $FH, -$marker_length, 2; print $FH $data, $endmarker; close $FH; } } append('text.xml', "<foo>ahoj</foo>\n"); append('text.xml', "<foo>cau</foo>\n"); append('text.xml', "<foo>co</foo>\n"); append('text.xml', "<foo>delas?</foo>\n");

I did not include any locking so that it doesn't obscure the solution. You should definitely add it.