Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

input into the middle of a file 1

Status
Not open for further replies.

Adkron

Programmer
Jun 6, 2005
39
US
I want to read through an html file and write some html in the middle of it.

Here is what I have so far

#!/usr/bin/perl

use CGI qw:)standard);

print "content-type: text/html\n\n";
$topic = param('topic');
$name = param('name');
$date = param('date');
$story = param('story');

open(infile, ">>../amos.html") or die $!;

while ($line=<infile>)
{
print infile <<EOF if $line =~ /<!--ADDHERE-->/;

<div class="news">
<p class="newshead" lang="en">
<b>$topic</b><br />

<font size="0.90em">$date by $name</font>
</p>
<div class="newsitem">
$story
</div>
</div>


EOF
}


Amos
Computer Science
U of MO - Rolla
 
this should work:

Code:
#!/usr/bin/perl -w
use CGI::Carp qw/fatalsToBrowser/;
use CGI qw/:standard/;
use strict;

print header,start_html;
my $topic = param('topic');
my $name = param('name');
my $date = param('date');
my $story = param('story');

open(IN, '../amos.html') or die "$!";
my @htmlstuff = <IN>;
close IN;

open(OUT, ">../amos.html") or die "$!";
for (@htmlstuff) {
if (/<!--ADDHERE-->/) {
print OUT qq~
 <div class="news">
   <p class="newshead" lang="en">
     <b>$topic</b><br />

     <font size="0.90em">$date by $name</font>
   </p>
   <div class="newsitem">
       $story
   </div>
 </div>
~;
}
else {
   print OUT $_;
}
close OUT;
print 'All done!';
print end_html;

I'm pretty sure this is not a valid attribute of the long-time-ago deprecated font tag:

<font size="0.90em">

you should use a span tag or other tag (the p tag will do) and CSS to use "em" sizing in html documents.
 
What you're asking for doesn't seem to make sense.

Looking at the code you've got, it appears that amos.html is a template file, which contains HTML for a page which wraps a news story selected by parameter. What your asking for will display nothing to the user, but will overwrite your template file making it useless in subsequent visits (the <!--ADDHERE--> placeholder having been replaced by whatever story the first person wanted). What you should be doing in this case is reading from the file but printing to standard output - so it goes to the user's browser.

Maybe I've misunderstood, and amos.html is one of many pages on your site into which you want to incorporate a "latest news" story. You're scuppered here too, since after the first time you do the replacement, you'll have lost the facility to do it again. Have you considered using SSI in your HTML file, and thus dynamically inserting the extra bits of content into each page as it is requested?

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top