There are many things to fix in your code. First, I should
emphasize some points made earlier:
- Fix the "content type" line.
- 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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.