Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Writing a simple posting script which can be used via the web

by wog (Curate)
on Sep 29, 2001 at 18:00 UTC ( [id://115628]=note: print w/replies, xml ) Need Help??


in reply to Writing a simple posting script which can be used via the web

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)

Replies are listed 'Best First'.
Re: Re: Writing a simple posting script which can be used via the web
by jlongino (Parson) on Sep 29, 2001 at 20:00 UTC
    I think wog means  use CGI::Carp not  Crap :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-25 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found