Dear Wise Monks,
One of my programs has a simple counter that counts the number of times the program has run. Below is the relevent portion that handles this task. My problem is that sometimes the counter mysteriously loses count and starts from the beginning again, as if the contents were erased somehow. This happens during times when traffic is high and people are accessing the script at the same time on the internet. Can you shed light on why the file contents "disappear"? Thanks in advance.
$completeadd = "mycounter";
$semaphore_file = "mycounterlock";
open(SEM, ">$semaphore_file") || die "Cannot create semaphore $semapho
+re_file: $!";
flock(SEM, LOCK_EX) || die "Lock failed: $!";
open(MFILE, ">>$completeadd") || die "file open1 failed: $!\n";
close(MFILE);
open(MFILE, "$completeadd") || die "file open2 failed: $!\n";
@filedata1=<MFILE>;
chomp @filedata1;
close(MFILE);
$hitcount=$filedata1[0];
if ($hitcount)
{
$hitcount=$hitcount + 1;
}
else
{
$hitcount = 1;
}
$filedata1[0]=$hitcount;
writedata(@filedata1);
release_lock();
-
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.
|