G'day again

I finally got round to testing out the CGI script and so on you kindly suggested before. I now have a different problem with the script, whereas before it would die with a server error... now it simply REPLACES the contents of the file it is supposed to be appended to.

The idea is as follows:
/protected/addnews.html - Adds a news item, has a submission form for 'nickname', 'headline' and 'news_text'
/index.html - The main page (no frames site) which is modified by the following script
/cgi-bin/news.pl - see below for source

The idea is to sandwich the news in amongst the already created html document in index.html but at the moment it is simply replaced and not formatted as I would like it (in fact not at all!)
Here is the 'updated' index.html

From: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testingFrom: David</TD> <TD><B>test</B></TD> <TD>testing

and here is the script i am using to update it:

#!/usr/bin/perl -w # news.pl # Attempt #3 at a news script. # Use the 'strict' switch to ensure logical operation of the code. #use strict; # Make use of the CGI module. use CGI qw(:standard); use CGI::Carp 'fatalsToBrowser'; # Initialise variables for the data previously inputted. my $nickname = param("nickname"); # Nickname my $headline = param("headline"); # Headline my $news_text = param("news_text"); # News Item Text # Create a variable to store the location of the display page, "index. +html". my $old_news = "../html/index.html"; # Read the current data or die with an error message. open (NEWS, "<$old_news") || die "$!"; # Read the data from '$old_news' into an array called '@news'. my @news = <NEWS>; # Close the '$old_news' file after use. close (NEWS); # Re-open the '$old_news' file for the input of the new data. open (NEWS, ">$old_news") || die "$!"; # Write the data back into the '$old_news' file with the new data incl +uded where appropriate. # Loop through each line of the '$old_news' file and write it back. #my $line = ""; foreach $line (@news) { # Add the 'nickname' of the poster. print NEWS "From: $nickname</TD>\n<TD>"; # Add the 'headline' of the post. print NEWS "<B>$headline</B></TD>\n<TD>"; # Add the 'news_text' underneath the 'headline'. print NEWS "$news_text"; } # Close the '$old_news' file after use. close (NEWS); # Provide the poster with a link to their news item. print header(), start_html, h2("Thank you, $nickname"), h4("Go here to view your news item: index.html"), end_html; # EOF news.pl

Note that I had to comment out 'strict' becaus I couldn;t work out how to initialise the variable $line (I made it my $line = ""; and it wouldn't work) and I've removed the check for the tag to try adn get some output.

How can I get the script to write out correctly my data into the table I created?
How do I correct the 'race' problem?
(BTW only 1 person will use the script but for completeness and learning I'd like to know how to fix this problem)

Thanks in advance La Grenouille


In reply to News script again by lagrenouille

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.