CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Database Unexpected Erasure Problem: Edit Log



gregarios
stranger

May 18, 2002, 7:05 PM


Views: 3459
Database Unexpected Erasure Problem

I can't figure out why about once every two weeks, this code will completely
erase the database contents. It seems to work just fine with no errors for
hundreds of hits, then POW, no ads...

The database is a pipe delimited file with a list of ads and their view counts in it.
A random text ad is displayed and that ad's display count is raised on each viewing.

I even made it to have the ad display and not raise the count if the file
cannot be opened in a timely manner due to a exclusive locks from heavy use.

Can someone tell me what is wrong here? :-)


Code
#!/usr/bin/perl5 -wT 
use strict;

$| = 1;
if (!open (ADDCTX, "<ads.dta")) {die "Could not open file: $!";}
flock(ADDCTX,1) or die "Could not lock file: $!";
seek(ADDCTX,0,0);
my @ads = <ADDCTX>;
close(ADDCTX);
my @TLAD = split(/\|/,$ads[rand @ads]);
$TLAD[2]++;
$ads[$TLAD[0]] = "$TLAD[0]|$TLAD[1]|$TLAD[2]\n";
print "Content-type: text/html\n\n";
print "Advertisement: $TLAD[1]";
if (open (ADDCTZ, "+<ads.dta")) {
flock(ADDCTZ,2) or die "Could not lock file: $!";
truncate ADDCTZ,0;
seek(ADDCTZ,0,0);
print ADDCTZ @ads;
close(ADDCTZ);
}
exit;



The database (ads.dta) is in this format:

Code
0|advertisement|123 
1|anotheradvertisement|32
2|advertisementthree|55


Please help if you can.

Greg J Piper
[url=http://www.macpicks.com]MacPiCkS



(This post was edited by gregarios on May 18, 2002, 7:08 PM)


Edit Log:
Post edited by gregarios (stranger) on May 18, 2002, 7:08 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives