There's a trick used in some Perl code, suggested as a way to write to a file with an exclusive lock, which requires you to seeking to the first byte, then writing it, then doing a "truncate" to snip off the rest of the file. If you have code like that, and it dies halfway, you end up with a garbled mix of the old file and the new file. Maybe, just a thought. This is pretty hard to debug without any more info.
Observation #1: You seem to be a talented programmer, but one day you'll learn to use strict and use warnings -w and to localize all your variables with "my," and then you'll look back on this code and barf. Take my word! Without them, for all we know the problem is some little typo, a misspelled variable name somewhere.
Obvervation #2: I noticed is you turned flock into a subroutine called &lock defined like this
sub lock {
eval { flock($_[0],$_[1]); }
}
Then you use it like this:
open(hits,">$dir/Data/$id.txt");
&lock(*hits,2);
foreach $line(@hits) { print hits "$line\n"; }
# you might consider: print hits join "\n",@hits;
close(hits);
open(info,"$dir/Data/info.cgi");
&lock(*info,2);
@info = <info>;
close(info);
That looks like it *should* work... I don't know, I've never done it quite like that. I'm not sure why the eval{} is necessary, it's over my head. But in any case, why bother with that? You can just call flock directly and you type one less character. E.g., instead of &lock(*info,2) just do flock(info,2). (By the way, they suggest you use capital filehandles so you don't clash with any possible future reserved words. So start using HITS and INFO and stuff like that.)
Observation #3: Watch out for this line open(ssi,"$1");
it has 'security hole' written all over it.
Here's another point... when you increment your data you are doing something like this: "open file, lock file, read data, close file, increment data, open file, lock file, write data, close file." That's not quite right because you give up your lock in between the read and the write. The solution to this entails sysopen; I referred to it in my post above. It's in the Camel book on page 421. But that's not your problem, because that mistake can't mung the data.
One last thought: every time I hear "sometimes it works, sometimes it doesn't" I also think of mod_perl. You're not running this under mod_perl are you? Because that would be folly.
Well that's about it. I guess you're on your own from here! Let me know if you find the problem. For some reason I've become curious.
Observation #3 is a good one. I'll change that.
It's not running under mod_perl. If I ever get it to work correctly, I'll post here. Thanks for all your help.