in reply to newbie writing a counter
These may fix your program if it works on the command line but not in a web browser.
Your code also seems needlessly complicated. Here's some simpler code which I adapted (okay, stole) from an answer to your frequently asked question:
# Let the Fcntl module define some constants use Fcntl qw(:DEFAULT :flock); # Open the counter file read/write and lock it. # If the file hasn't been created yet, it creates one. # Of course, the usual flock cautions apply. sysopen(FH, $file, O_RDWR|O_CREAT) or die "Can't open $file_banner: $!\n"; flock(FH, LOCK_EX) or die "Can't write-lock $file: $!\n"; # Read one line from the file. Set $num to the # value from that line, or to zero by default. # It's a little silly to read every line into an array # and reference the first element if you only want # this one value, isn't it? $num = <FH> || 0; # Start at the beginning of the file seek(FH, 0, 0) or die "Can't rewind $file: $!\n"; truncate(FH,0) or die "Can't truncate $file: $!\n"; # Print the new number to the file and close it print FH $num+1, "\n" or die "Can't write to $file: $!\n"; close(FH) or die "Can't close $file: $!\n";
Note how the error checking is done wherever feasible so that you can get more feedback on your success/failure than that things "don't work".
buckaduck
|
|---|