Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl problem

weird thing's happening

         

Aardvark Freak

8:04 pm on Aug 12, 2001 (gmt 0)

10+ Year Member



i wrote a topsites script. it works like a charm, except every once in a while the text file i use to store all of the hits a site gets is overwritten and replaced with something like inf124297217133131231311. it really stumped me and all the people i've asked so far. anyone know what's going on?

sugarkane

8:14 pm on Aug 12, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Aardvark_Freak, welcome to WmW!

Are you using file locking to prevent multiple attempts to write to the file at the same time?

Bolotomus

8:30 pm on Aug 12, 2001 (gmt 0)

10+ Year Member



What's supposed to be in the file? A single integer or something more complex than that?

Aardvark Freak

2:33 am on Aug 13, 2001 (gmt 0)

10+ Year Member



yes, i'm using flock. the file is supposed to just be integers seperated by ¦. the server is red hat linux and apache.

Bolotomus

2:33 am on Aug 14, 2001 (gmt 0)

10+ Year Member



Next question: show us exactly what the file contains. I don't like how you say "something like." Does it really say "inf"? That is a clue, I think. Perhaps your code does a division-by-zero type operation and flips out, crashes halfway through writing to the file, etc. It would probably do you well to review the section on "inf" and "NaN" in the Camel book. Me too, I'm murky on that subject.

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.

Aardvark Freak

3:29 am on Aug 14, 2001 (gmt 0)

10+ Year Member



sometimes it will be inf with a whole bunch of numbers after inf. other times it will be a whole bunch of numbers with e stuck somewhere in the middle. this is probably a good reason i should just switch the damn thing to sql, ain't it? that's prolly what i'll end up doing.

Bolotomus

4:00 am on Aug 14, 2001 (gmt 0)

10+ Year Member



I'm sure if you converted to SQL this bug would go away but the easier way would be to just fix your code, assuming this is the only problem. There's no reason why you can't reliably write a file in Perl. Put your sourcecode online and lets have a look at it.

Aardvark Freak

5:15 am on Aug 14, 2001 (gmt 0)

10+ Year Member



ok. please be kind, i know you all are probably way better than me :)

www.aardvark.nu/cgi/topsites/topsites3b2.zip

Bolotomus

8:25 am on Aug 14, 2001 (gmt 0)

10+ Year Member



A zip file?! OK I looked at your code, not in great detail. I assume out.pl is the problem child, because it writes to the Data/*.txt files.

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.

Aardvark Freak

4:02 pm on Aug 14, 2001 (gmt 0)

10+ Year Member



I put the flock in a subroutine so people running servers that die on flock don't have to comment out all the lines. The eval takes care of that.

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.