#!/usr/bin/perl # news.pl # Attempt #3 at a news script. # Set up the standard HTML header. print "Content-type: text/html\n"; # Read in the data from the "addnews.html" file. $data_length = $ENV{'CONTENT_LENGTH'}; $bytes_read = read (STDIN, $temp_data, $data_length); # Store the 'temp_data' in an array called '@temp_name_value' for manipulation. @temp_name_value = split (/&/, $temp_data); foreach $temp_name_value (@temp_name_value) { # Split up the 'name=value' pairs. ($name, $value) = split (/=/, $temp_name_value); # Replace '+'s with ' 's. $name =~ tr/+/ /; $value =~ tr/+/ /; # Translate hexidecimal characters back into their original characters. $name =~ s/%(..)/pack("C",hex($1))/eg; $value =~ s/%(..)/pack("C",hex($1))/eg; # Create an associative array using the '$name" and '$value' variables. # NOTE: If there is no '$name' specified '$value' is used instead. if ($form_data{$name}) { $form_data{$name} .= "\t$value"; } else { $form_data{$name} = $value; } } # Create a variable to store the location of the display page, "index.html". $news_data = "/index.html"; # Open the '$news_data' file to read the current data or die with an error message. open (NEWS, ">$news_data") || die "Unable to locate $news_data, the news data file."; # Read the data form '$news_data' into an array called '@news'. @news = ; # Close the '$news_data' file after use. close (NEWS); # Re-open the '$news_data' file for the input of the new data. open (NEWS, "<$news_data") || die "Unable to locate $news_data, the news data file a second time."; # Write the data back into the '$news_data' file with the new data included where appropriate. # Loop through each line of the '$news_data' file and write it back. foreach $line (@news) { # Search for the '' marker. if ($line =~ //i) { # Write itself back for future use. print NEWS "\n"; # Add the 'nickname' of the poster. print NEWS "From: $form_data{'nickname'}\n"; # Add the 'headline' of the post. print NEWS "$form_data{'headline'}\n"; # Add the 'news_text' underneath the 'headline'. print NEWS "$form_data{'news_text'}"; } else { die "Unable to find the position header in $news_data, the news data file."; } } # Close the '$news_data' file after use. close (NEWS); # Provide the poster with a link to their news item. print << "EOF";

Thank you for your news item

Click here to view your news item.
Note that you may have to reload the file before your post is visible.

EOF # EOF news.pl