Ignoring that you should use CGI and strict and warnings as mentioned previously (which would fix one problem -- you don't output proper headers), you have a few logic errors in your script:

# Open the '$news_data' file to read the current data or die with an e +rror message. open (NEWS, ">$news_data") || die "Unable to locate $news_data, th +e news data file."

The problem here is that you are actually opening the $news_data file for writing here. Not what you want to do. Your code is simillary reversed for writing the file. Additionally the file not being found is not the only reason the open could fail. Try including $! in your error message to give a better idea what's wrong. (Note that unless you do use CGI::Carp 'fatalsToBrowser' or similar (often not reccommended in production code) die messages will typically go to the error log, not the browser.)

Also, note that opening a file, storing what's in it, and then closeing it before reopening it again opens you up to a race condition. That is, if two people try to add a news article at the same time you may suffer data loss. You should open the file once for update (both read and write, see the docs for open), and use seek (so you can read the file, and then rewind to the beginning to re-write it) and flock to avoid this.

Another problem is in your foreach loop. You check, for every line in the file you've read, if it matches <!--latest news-->, but for every line you throw an error if it doesn't. (On a sidenote you may want to consider what happens if someone puts <!--latest news--> inside the text of a news item.) Furthermore you do not output the lines in the file as you iterate through them, meaning that only your new news item will be outputed.

update: s/Crap/Carp/ (... thanks jlongino)


In reply to Re: Writing a simple posting script which can be used via the web by wog
in thread Writing a simple posting script which can be used via the web by Anonymous Monk

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.