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


In reply to Re: newbie writing a counter by buckaduck
in thread newbie writing a counter by jrbush82

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.