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

hi fella monks... i'm a newbie in perl and i'm not sure if this if this is correct. i have an html file and i want to insert that html file to another html file.
@cat1 = "template.html"; #template html $cat2 = "data.txt"; #data file contains html snipets open(F, ">foo.html"); foreach (@cat1) { s/%%insert_here%%/$cat2/ge; } print F @cat1;
it reads the template.html and looks for "%%insert_here%% and substitute the html snipets of data.txt to the searched pattern. then it prints the value of @cat1 to foo.html. the problem is that it prints the contents of data.txt twice. the contents of data.txt doubles when it prints to foo.html. pretty weird huh! or it's just my code. thanks in advance print F $cat

Replies are listed 'Best First'.
Re: Weird printing
by ar0n (Priest) on Aug 09, 2000 at 08:33 UTC
    From the looks of it, HTML::Template does exactly what you want (replace any variables).
    Try it, it's better than reinventing an already rolling wheel :)

    non-smart remark deleted

    -- ar0n | Just Another Perl Joe

        Thanks a lot, Randal!
        I don't know why, but your link got no results. Is this the module you recommended?
Re: Weird printing
by Anonymous Monk on Aug 09, 2000 at 12:08 UTC
    Well, without seeing your html and txt files, it's impossible to say for sure, I tried this with the following:
    template.html: <H>%%insert_here%%</H> sldfjkdslkf sdlfkjsdlf sldkjfdslk <B>%%insert_here%%</B> data.txt: hello foo.html: <H>hello </H> sldfjkdslkf sdlfkjsdlf sldkjfdslk <B>hello </B>
    I will tell you however, that you'd be doing well to code it a little more like
    use strict; my $htmlfile = "template.html"; my $datafile = "data.txt"; my $outfile = "foo.html"; my @cat1 = `/bin/cat $htmlfile`; my $cat2 = `/bin/cat $datafile`; open(OUTFILE, "> $outfile") || die "Can't open $outfile for write: $!" +; foreach my $line (@cat1) { $line =~ s/%%insert_here%%/$cat2/ge; #maybe print OUTFILE "$line\n"; } close(OUTFILE);
Re: Weird printing
by damian (Beadle) on Aug 09, 2000 at 10:05 UTC
    im sorry fellas the code should be like this..
    @cat1 = `/bin/cat template.html`; #template html $cat2 = `/bin/cat data.txt`; #data file contains html snipets open(F, ">foo.html"); foreach (@cat1) { s/%%insert_here%%/$cat2/ge; } print F @cat1;
      I have to take issue with your
      $cat2 = `/bin/cat data.txt`;
      Try undefing the record separator then read in the file from a normal filehandle.
      open(FH, 'data.txt') or die(); my $tmp_fs = $/; my $cat2 = <FH>; $/ = $tmp_fs; close(FH);
        local will do this for you automatically, if you scope it correctly:
        my $cat2; { open(FH, 'data.txt') || die "Some error: $!"; local $/; $cat2 = <FH>; close FH; }
        You have to make sure $cat2 is defined outside of the block, though, otherwise it'll go out of scope when you want to use it.
Re: Weird printing
by BigJoe (Curate) on Aug 09, 2000 at 16:32 UTC
    If you want to stay with cat-ing the file then just do this
    use strict; my $firstfile; my $secondfile; $firstfile = `/bin/cat template.html`; $secondfile = `/bin/cat data.txt`; $firstfile = ~s/%%insert_here%%/$secondfile/g; open(OUTFILE, ">foo.html") || die "Can't open $outfile for write: $!"; print OUTFILE $firstfile; close OUTFILE;


    --BigJoe

    Learn patience, you must.
    Young PerlMonk, craves Not these things.