use Fcntl qw(SEEK_CUR SEEK_END); #import constant use strict; use warnings; use constant CLOSING_TAG => ""; #deinfe the closing tag as constant my $event1 = "detail1"; open(LOGFILE, "+<", "log.xml"); seek(LOGFILE, -length(CLOSING_TAG), SEEK_END); my $cur_char; #you may have newlines, blanks etc after the closing tag #that's why we do the following to make the program robust do { sysread(LOGFILE, $cur_char, 1); seek(LOGFILE, -2, SEEK_CUR) if ($cur_char ne "<"); } until ($cur_char eq "<"); seek(LOGFILE, -1, SEEK_CUR); print LOGFILE $event1 . "\n"; #you can do as many prints as you want before close #no more seek, we did it once and forever print LOGFILE CLOSING_TAG; close(LOGFILE);