Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: Writing a simple posting script which can be used via the webby wog (Curate) |
on Sep 29, 2001 at 18:00 UTC ( [id://115628]=note: print w/replies, xml ) | Need Help?? |
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:
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 Section
Seekers of Perl Wisdom
|
|