Gbuk has asked for the wisdom of the Perl Monks concerning the following question:

I have 2 files: A text file that contains lines like:
<param name="desc0" value="This is a test line"> <param name="desturl0" value="http://www.testsite.com"> <param name="delay0" value="2000"> <param name="desc1" value="This is another test line"> <param name="desturl1" value="http://www.anothersite.com"> <param name="delay1" value="2000"> etc...
And a html file containing basic html code.

Basically what I would like to do is add all the contents of the text file (in the same format as above) into a specific location in the html code and then save the updated html file.

I have tried many different methods to read in the text file and print out the contents into the html file. One problem I have come across is it seems that the "<" and ">" chars around the text cause problems. When I try and print out the lines read in from the text file ... nothing is written!

Can anybody help me before I lose all my hair?

Thanks

Replies are listed 'Best First'.
Re: Adding contents of a file into another
by Abigail-II (Bishop) on Feb 26, 2004 at 16:33 UTC
    Here's what I would do:
    • Open up the text file and the HTML file for reading.
    • Open up a temporary file for writing.
    • Read from the HTML file (in reasonable sized blocks), up to the point you want to insert, and write each read block to the temporary file.
    • Read from the text file (in reasonable sized blocks), and write each read block to the temporary file.
    • Read the rest of the HTML file (in reasonable sized blocks), and write each read block to the temporary file.
    • Close all files.
    • Move the temporary file to the HTML file.
    • Eat a slice of chocolate cake for a job well done.

    Abigail

Re: Adding contents of a file into another
by blue_cowdawg (Monsignor) on Feb 26, 2004 at 16:39 UTC

        Basically what I would like to do is add all the contents of the text file (in the same format as above) into a specific location in the html code and then save the updated html file.

    Give HTML::Template and/or Template a look. I do that sort of thing from CGI all the time.

    Instead of emitting the resultant merged HTML to STDOUT for display on a browser you can instead write the results to a file.


    Peter L. Berghold -- Unix Professional
    Peter at Berghold dot Net
       Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.
Re: Adding contents of a file into another
by matija (Priest) on Feb 26, 2004 at 16:45 UTC
    Is this "specific place" in the HTML file between two lines? If if is, you could do something like this:
    open(HTML,"<$html_file") || die "Could not open $html_file:$!\n"; open(TXT,"<$txt_file") || die "Could not open $txt_file:$!\n"; open(OUT,">$output_file) || die "Could not open $ouput_file:$!\n"; while (<HTML>) { print OUT $_; if (/this is how you recognise the specific point/) { while (<TXT>) { print OUT $_; } } }
    If you need to replace something (like a custom tag) in the HTML file with the contents of the TXT file, that's a somewhat different story. If you expect the text file to be short, you could just slurp it into a text variable with $txt=join("",<TXT>); and then, at the appropriate point in the loop copying HTML to the output file, you could to a s/<custom tag>/$txt/;
Re: Adding contents of a file into another
by arden (Curate) on Feb 26, 2004 at 16:33 UTC
    Gbuk, show us what you've got so far and any errors it is producing. Then we'll be able to help. I'm sure it's probably a simple thing that many (most even) of us have done to ourselves a couple dozen times too. . .

    - - arden.
          show me the moneycode