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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate: Re: [yapp] Database Unexpected Erasure Problem: Edit Log


May 23, 2002, 1:34 PM

Views: 8819
Re: [yapp] Database Unexpected Erasure Problem

Thanks... Ok, then. Judging from my original code seen here:

#!/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: $!";
my @ads = <ADDCTX>;
my @TLAD = split(/\|/,$ads[rand @ads]);
$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;
print ADDCTZ @ads;

I've apparently done only a few things different:
1) Open a file only once in a given cgi when you wish to read, edit, then write back to it.
2) Use "use Fcntl qw(:flock);" in addition to the actual flock command and "LOCK_EX" style instead of the numeric equivelent.
3) Seek to the EOF when appending, which I am not. The file is a fixed number of lines and it stays that way.
4) Use "sysopen" instead of just the "open" command.

Could you please tell me why each of these things makes a difference? I've really been trying to find out the details on this for some time but cannot find information about the "why's and wherefore's" of this stuff. Perldocs are only so good at telling you how to use certain things, but not so good as to the why's or when's. :-)

(This post was edited by gregarios on May 23, 2002, 1:37 PM)

Edit Log:
Post edited by gregarios (stranger) on May 23, 2002, 1:37 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