lagrenouille has asked for the wisdom of the Perl Monks concerning the following question:
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
#!/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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: News script again
by Zaxo (Archbishop) on Oct 03, 2001 at 08:52 UTC | |
|
Re: News script again
by MZSanford (Curate) on Oct 03, 2001 at 14:47 UTC | |
by Anonymous Monk on Oct 03, 2001 at 15:30 UTC | |
by mirod (Canon) on Oct 03, 2001 at 15:36 UTC | |
by MZSanford (Curate) on Oct 03, 2001 at 15:37 UTC | |
by tommyw (Hermit) on Oct 03, 2001 at 15:41 UTC |