PDA

View Full Version : Need help for CGI News Script


ren
03-09-1999, 08:00 PM
I'm very new to CGI programming. I'm trying to create a script that will open my 'news2.htm' file locate the string '<!-- News Insert -->' and replace it with '<!-- News Insert -->' followed by information from my form. I've gotten the script to the point where is outputs the information from the update form perfectly, but it doesn't amend the file the way I want it. Please please help me! Here is what I've got so far:

--------

#!/usr/bin/perl

&parse_form;

$headline = $form{'headline'};
$source = $form{'source'};
$article = $form{'article'};
$reporter = $form{'reporter'};

($min,$hour) = localtime(time);

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

if ($hour eq 00) {
$hour = "12";
$m = "am";
}elsif ($hour eq 12) {
$hour = "12";
$m = "pm";
}elsif ($hour > 12) {
$hour = $hour - 12;
$m = "pm";
} else {
$hour = "$hour";
$m ="am";
}

if ($min < 10) {
$min = "0$min";
}
if ($hour < 10) {
$hour = "0$hour";
}

$time = "$hour:$min$m";

print "Content-type: text/html\n\n";

print "<!-- News Insert -->";
print "

<UL><FONT COLOR=#FFFFFF FACE=Arial><U>$headline</U></FONT>[nbsp]<FONT FACE=Arial SIZE=1>$time EST - Source: $source</FONT>";
print "<UL><FONT FACE=Arial>$article</FONT>";
print "
<FONT FACE=Arial SIZE=1>Reported by: $reporter</FONT>[/list][/list]";

open(NEWS,"/big/dom/x3dnow/www/news2.htm")| |&ErrorMessage;
@news = <NEWS>;
close(NEWS);

open(NEWS,">>/big/dom/x3dnow/www/news2.htm")| |&ErrorMessage;
foreach $news (@news) {
if ($news =~/<!-- News Insert -->/) {

print NEWS "<!-- News Insert -->\n";
print NEWS "

<UL><FONT COLOR=#FFFFFF FACE=Arial><U>$headline</U></FONT>[nbsp]<FONT FACE=Arial SIZE=1>$time EST - Source: $source</FONT>\n";
print NEWS "<UL><FONT FACE=Arial>$article</FONT>\n";
print NEWS "
<FONT FACE=Arial SIZE=1>Reported by: $reporter</FONT>[/list][/list]\n";
}
}

close(NEWS);


sub ErrorMessage {

print "There was an error posting your news.";

}

sub parse_form {

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);

# Un-Webify plus signs and %-encoding
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

if ($form{$name}) {
$form{$name} .= ", $value";
}else {
$form{$name} = $value
}
}
}

---------

Thanx in advance!

ChrisH
03-09-1999, 10:31 PM
It's not clear to me exactly what you
want to do. Do you want to replace
the comment and everything after it
with the update?

If you open the file with ">>" then
you can only ever append. It looks
like you want to replace from the
comment on. Otherwise there is no
point to the foreach @news loop since
you can only write to the end. Why not
just write to the end, no need to ever
read in the file at all . . .

You can't do this with
append. You have to open the file in
regular write mode (">file.html"). But
then you need to make sure you write the
old stuff back. So, rather than

open (NEWS, ">>file.html");

foreach $news (@news)
{
if ($news=~/<!-- News Insert -->/)
{
print NEWS "bunch o'stuff";
}
}

you might better try

open (NEWS, ">file.html");

foreach $news (@news)
{
if ($news=~/<!-- News Insert -->/)
{
print NEWS bunch o'stuff
# last will stop printing the old stuff!
last;
}
else
#this prints out the old stuff until you
# get to where you want to print out the
# new stuff
{print NEWS $news;}
}

or something like that.

Disclaimer: try it before you buy it!


ch

[This message has been edited by ChrisH (edited 03-09-99).]

Terra
03-10-1999, 03:55 AM
Proof of concept only:

Here's the all in one slurp/search/replace that I use for stuff like this... It avoids all the iteration and handles it as one large scalar blob...

--file xxx.txt
blah blah
<!-- replaceme -->

#!/usr/local/bin/perl -w
use strict;

my $file="xxx.txt";
my $string;
open IN, "$file" or die "Horrible Death: $!\n";
{
local $/;
$string = <IN>;
}
#be defensive
close IN or die "something *in* the way of $file: $!\n";
$string =~ s/<!-- replaceme -->/something witty to say/g;

open OUT, ">$file" or die "whoops, my pencil lead broke on $file: $!\n";
print OUT $string;
close OUT or die "get yer foot *out* of the door $file: $!\n";

--
Terra
--Perl, 99 different complex ways to do 1 simple thing--
FutureQuest

Justin
03-10-1999, 08:37 AM
Here's what I would do (trying the new code tag)....

open(NEWS,"/big/dom/x3dnow/www/news2.htm")| |&ErrorMessage;
@news = <NEWS>;
close(NEWS);

open(NEWS,">/big/dom/x3dnow/www/news2.htm")| |&ErrorMessage;
foreach $news (@news) {
if ($news =~/<!-- News Insert -->/) {

print NEWS "<!-- News Insert -->\n";
print NEWS "

<UL><FONT COLOR=#FFFFFF FACE=Arial>[b]<U>$headline<\/U><\/B><\/FONT> <FONT FACE=Arial SIZE=1>[i]$time EST - Source: $source<\/I><\/FONT>\n";
print NEWS "<UL><FONT FACE=Arial>$article<\/FONT>\n";
print NEWS "
<FONT FACE=Arial SIZE=1>[i]Reported by: $reporter<\/I><\/FONT><\/UL><\/UL>\n";
} else {
print NEWS "$news\n";
}
}
close(NEWS);

In other words, if the line has the <!-- whatever --> tag, print the tag and all of our new news. Otherwise, just print the line.

Also note that you have to escape the /'s with a \, eg <\/font>.

One last thing: This UBB really really needs to be hacked to at least make this stupid input box bigger! I don't thing there are too many 640 x 480's here... like rows=10 cols=60, which is how mine is set up. Just a thought http://www.aota.net/ubb/smile.gif

<edit>
Ok, forget the code tag! No word wrap - twas ugly (and too smal). http://www.aota.net/ubb/smile.gif
<\/edit>

------------------
Justin Nelson, SFE Software
www.vdj.net (http://www.vdj.net)

[This message has been edited by Justin (edited 03-10-99).]

ren
03-10-1999, 11:27 AM
Sweet! That worked! Alright easy news reporting from now on! http://www.aota.net/ubb/smile.gif

ChrisH
03-10-1999, 10:32 PM
Justin,


Why escape the forward slashes? I know
you have to do backslashes, but why the
regular ones?

ch