Flat file missing

A

asg

i use the code below for a page counter. when it gets to around a 900
count, it resets to 1. any ideas as to why?



my $counterFile = "adcounter/page1";

if (-e $counterFile) {
open(COUNT,"$counterFile");
flock (COUNT, 2);
my $ergsege= <COUNT>;
close(COUNT);
$ergsege++;
open(COUNT,">$counterFile");
print (COUNT "$ergsege");
close(COUNT);
flock (COUNT, 8);
}
else{
my $ergsege= "1";
open(COUNT,">$counterFile");
flock (COUNT, 2);
print COUNT "1";
close(COUNT);
flock (COUNT, 8);
}
 
J

J. Gleixner

asg said:
i use the code below for a page counter. when it gets to around a 900
count, it resets to 1. any ideas as to why?

page counter... A cold chill goes through the newsgroup...

Two quick reasons:
The file didn't exist.
The open failed, therefore ergsege is undefined/0.

There's no way to tell because you aren't checking if
the various functions were successful or not.
my $counterFile = "adcounter/page1";

if (-e $counterFile) {
open(COUNT,"$counterFile");
flock (COUNT, 2);
my $ergsege= <COUNT>;
close(COUNT);
$ergsege++;
open(COUNT,">$counterFile");
print (COUNT "$ergsege");

print COUNT $ergsege;
close(COUNT);
flock (COUNT, 8);
}
else{
my $ergsege= "1";
open(COUNT,">$counterFile");
flock (COUNT, 2);
print COUNT "1";
close(COUNT);
flock (COUNT, 8);
}

This is a pretty poor way to do what you want, for
a much better approach, see:

perldoc -q "I still don't get locking."
 
T

Tad McClellan

asg said:
open(COUNT,"$counterFile");

open(COUNT, $counterFile) or die "could not open '$counterFile' $!";
print (COUNT "$ergsege");

print (COUNT $ergsege);


See:

perldoc -q vars

What's wrong with always quoting "$vars"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,530
Members
48,250
Latest member
Bette22B13

Latest Threads

Top