Hi,
Here's a quick question. This doesn't have anything to do with perl on
my mac, but just a perl question. I have a a small script that simply
increases a number by 1, when it is run, then writes the changes back
to the file. I am using it to check how many people come to the site by
way of an email. Everything seemed to work fine until I got here this
morning and the file was only left with 1 row of 1 number verses 2 rows
of 10 numbers each.
Here is what the file should look like
(The zeros would not necessarily be zeros, but could be any number.
0:0:0:0:0:0:0:0:0:0
0:0:0:0:0:0:0:0:0:0
What I have found is that when I click at a normal pace, everything
works fine, BUT... when I click the reload button unmercifully, the
data file gets rewritten to look like the following:
0 (or some other number - less then what it was previously)
That's it! everything else is gone. Is is possible that if there are so
many hits that it can't keep up with the opening and writing of the
file? This simple database is potentially being alter by 2 million
people, as the email went out to that many people.
Any help would be greatly appreciated. Below is the script.
Thanks,
Mark Wheler
Here is the script:
----------------------------------------------------------------------
#!/usr/local/bin/perl -w
#=======================================================
# format for "ab" variable in html email:
#
# control(0)/test(1):week(1-10)
#
# So 0:5 would be control group, week 5 and 1:9 would be
# test group, week 9
#=======================================================
use strict;
use CGI ':standard';
if (!-e "blastinfo-test.txt") { # Create blastinfo-test.txt file in not
in place
open (FILE, "> blastinfo-test.txt") || die ("Cannot open file");
flock (FILE, 2);
print FILE "0:0:0:0:0:0:0:0:0:0\n0:0:0:0:0:0:0:0:0:0";
close (FILE);
}
#=======================================================
# Set and get variables
#=======================================================
my $pathtolandingpage='../email_blasts/carnival/carnival1.html';
my $pathtodatafile='blastinfo-test.txt';
my @blasts_split;
my $ab = param('ab');
my @ab_split = split(/:/, $ab);
$ab_split[1]--;
#my $anchor = param('anchor');
open (FILE, "< $pathtodatafile") || die "$!";
flock (FILE, 2);
my @blasts = <FILE>;
close (FILE);
if ($ab_split[0] eq "0") {
chomp($blasts[0]);
@blasts_split = split(/:/, $blasts[0]);
$blasts_split[$ab_split[1]]++;
$blasts[0] = join(":", @blasts_split)."\n";
} else {
@blasts_split = split(/:/, $blasts[1]);
$blasts_split[$ab_split[1]]++;
$blasts[1] = join(":", @blasts_split);
}
#====================================================
# Write changes back to file
#====================================================
open (FILE, "> $pathtodatafile") || die ("Cannot open file");
flock (FILE, 2);
print FILE @blasts;
close (FILE);
#====================================================
# Send to landing page at correct anchor
#====================================================
print "Location: $pathtolandingpage\n\n";
exit;
------------------------------------------------------
Randal L. Schwartz - 16 Jun 2005 00:05 GMT
>>>>> "Mark" == Mark Wheeler <musicarr@comcast.net> writes:
Mark> open (FILE, "> $pathtodatafile") || die ("Cannot open file");
Mark> flock (FILE, 2);
0wn3d!
That's your breakage.
Once in a blue mooon, you'll kill your entire data this way.

Signature
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Mark Wheeler - 16 Jun 2005 00:30 GMT
Hi,
Sorry, I don't understand. What's my breakage? Should I not use "flock"?
Thanks,
Mark
Mark> open (FILE, "> $pathtodatafile") || die ("Cannot open file");
Mark> flock (FILE, 2);
0wn3d!
That's your breakage.
Once in a blue mooon, you'll kill your entire data this way
Randal L. Schwartz - 17 Jun 2005 05:31 GMT
>>>>> "Mark" == Mark Wheeler <musicarr@comcast.net> writes:
Mark> Hi,
Mark> Sorry, I don't understand. What's my breakage? Should I not use "flock"?
You should use "flock", but properly. :)
google for "site:stonehenge.com flock" for many examples of how to do it right.

Signature
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Ken Williams - 16 Jun 2005 01:53 GMT
Hey Mark,
I doubt any mortal brain could follow all the various permutations of
clients hitting your files and trying to lock them while some other
client is doing the same. But here's one way your data could get
hosed: one client reads the file in between the time another client
opened it for writing (thus blowing it away) and locked it.
I'd suggest opening the file for both reading *and* writing, and not
making any changes to the file until you can get your lock. Keep the
file locked for the entire duration you've got the filehandle open. If
you want, you can use a shared lock up until you start to write, at
which point you can convert to an exclusive lock.
How you get your data back, well, that's another matter. I suggest
going back in time and using an SQLite or Berkeley DB database to begin
with.
-Ken
> Hi,
>
[quoted text clipped - 102 lines]
>
> ------------------------------------------------------