Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: newbie writing a counter

by buckaduck (Chaplain)
on Aug 08, 2001 at 01:09 UTC ( [id://102916]=note: print w/replies, xml ) Need Help??


in reply to newbie writing a counter

There are many things to fix in your code. First, I should emphasize some points made earlier:
  1. Fix the "content type" line.
  2. Check that perl is really in /usr/bin/perl and that your program is executable. Check your web server logs.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://102916]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-26 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found