Wait a minute, I know that +< and seek can make things quite complex, and too many seeks may lower the performance, but we have to look at this case by case. If we can write a program by using seek and +<, yet it is very neat and no performance issue, then why not?
use Fcntl qw(SEEK_CUR SEEK_END); #import constant
use strict;
use warnings;
use constant CLOSING_TAG => "</events>"; #deinfe the closing tag as co
+nstant
my $event1 = "<event time=\"time1\" type=\"type1\"><detail>detail1</de
+tail></event>";
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);
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.