in reply to newbie writing a counter
#!/usr/bin/perl #begins for loop of READCOUNTER so that it has to lock file before rea +ding READCOUNTER: for ($i=0; defined($i); $i++){ #opens counter data file open(COUNTERDAT,"./counter.dat"); #locks file, and if can't, goes back to READCOUNTER to try again flock COUNTERDAT, 1 or next READCOUNTER; #puts first line of counter data file in first array (0) called counte +rdat while (<COUNTERDAT>){ push @counterdat, $_; } #closes counter data file close COUNTERDAT; #ends for loop of READCOUNTER last READCOUNTER; } #sets previous number in counter.dat to one higher $pnum = $counterdat[0]; $pnum++; #begins loop of WRITECOUNTER so that it has to lock file before writin +g WRITECOUNTER: for ($i=0; defined($i); $i++){ #opens counter data file again open(COUNTERDAT,">./counter.dat"); #locks file, and if can't, goes back to WRITECOUNTER flock COUNTERDAT, 2 or next WRITECOUNTER; #writes new number to counter data file print COUNTERDAT "$pnum"; #closes counter data file close COUNTERDAT; #ends loop for WRITECOUNTER last WRITECOUNTER; } #begins for loop of READCOUNTER2 so that it has to lock file before re +ading READCOUNTER2: for ($i=0; defined($i); $i++){ #opens counter data file open(COUNTERDAT2,"./counter.dat"); #locks file, and if can't, goes back to READCOUNTER to try again flock COUNTERDAT2, 1 or next READCOUNTER2; #puts first line of counter data file in first array (0) called counte +rdat2 while (<COUNTERDAT2>){ push @counterdat2, $_; } #closes counter data file close COUNTERDAT; #ends for loop of READCOUNTER2 last READCOUNTER2; } print<<HTMLEND; content type; text/html $counterdat2[0] HTMLEND
|
|---|