Replacing a line of text in an HTML file ???

G

Guest

I have a situation where I need to "Replace" a line of text in an HTML file.

The code I'm using does not "replace"... but instead, it adds another
line... effectively duplicating the number of lines each time the script
runs.

Below is a sample of my code:

###########################
sub main_page
{
open(MAIN,"$basedir/$mesgfile") || die $!;
@main = <MAIN>;
close(MAIN);

open(MAIN,">$basedir/$mesgfile") || die $!;
if ($followup == 0) {
foreach $main_line (@main) {

##---Title
if ($main_line =~ m/<!--Title-->/) {
print MAIN "<!--Title--> <title>$title</title>\n";
}

##---Page Formatting
if ($main_line =~ m/<!--Pformat-->/) {
print MAIN "<!--Pformat--> <body bgcolor=$BKcolor
text=\"#000000\" alink=\"#FF0000\" vlink=\"#FF00FF\" link=\"#0000FF\">\n";
}

##---Page Heading
if ($main_line =~ /<!--PHeading-->/) {
print MAIN "<!--PHeading--> <FONT FACE=$HeadingFont
SIZE=$HeadingSize COLOR=$HeadingColor> $HeadingText </FONT>\n";
}

#######################################



Any help would be greatly appreciated.
 
J

John Bokma

NoSpamPlease said:
I have a situation where I need to "Replace" a line of text in an HTML file.

The code I'm using does not "replace"... but instead, it adds another
line... effectively duplicating the number of lines each time the script
runs.

Below is a sample of my code:

###########################
sub main_page
{
open(MAIN,"$basedir/$mesgfile") || die $!;
@main = <MAIN>;
close(MAIN);

open(MAIN,">$basedir/$mesgfile") || die $!;
if ($followup == 0) {

I don't know what this test does, but it nothing needs to be done if it
is not 0 then better put it before the open and:

return unless $followup == 0;

or even:

return if $followup;
foreach $main_line (@main) {

##---Title
if ($main_line =~ m/<!--Title-->/) {
print MAIN "<!--Title--> <title>$title</title>\n";
next; # since we have processed this line I assume
}

##---Page Formatting
if ($main_line =~ m/<!--Pformat-->/) {
print MAIN "<!--Pformat--> <body bgcolor=$BKcolor
text=\"#000000\" alink=\"#FF0000\" vlink=\"#FF00FF\" link=\"#0000FF\">\n";
next; # since we have processed this line I assume
}

##---Page Heading
if ($main_line =~ /<!--PHeading-->/) {
print MAIN "<!--PHeading--> <FONT FACE=$HeadingFont
SIZE=$HeadingSize COLOR=$HeadingColor> $HeadingText </FONT>\n";
next; # since we have processed this line I assume

My guess is that you print $main_line here?

Tips:

use -w in the she bang line and put a use strict; in your code.

Also put some more info in your die, ie.
"Can't open file '$basedir/$mesgfile': $!"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top