in reply to perl fool - inserting text into html doc

Here is the code for what you are trying to do. I assume that you place a token like <TMPL_VAR NAME=TEXT> in the HTML template file:

#!/usr/bin/perl -w use strict; my $template = get_file ( 'c:/some/template.htm' ); my $textfile = get_file ( 'c:/some/textfile.txt' ); $template =~ s/<TMPL_VAR NAME=TEXT>/$textfile/ or die "No token!"; print "Content-Type: text/html\n\n"; print $template; sub get_file { my $file = shift; open FILE, $file or die "Can't open $file $!\n"; local $/; my $content = <FILE>; return $content; }

Now as it happens <TMPL_VAR NAME=TEXT> is what you use with HTML::Template. The corresponding code would be:

use HTML::Template; # open the html template my $template = HTML::Template->new(filename => 'some/template.html'); # get the text ( get_file() code above) my $sometext = get_file( '/some/textfile.txt' ); # fill in some parameters $template->param( TEXT => $sometext ); # send the obligatory Content-Type print "Content-Type: text/html\n\n"; # print the template print $template->output;

If you *really* have a problem with module installation you can install them locally or as a last resort you can ditch the use HTML::Template; line in the code above and just paste the entire module at the end of your code.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: perl fool - inserting text into html doc
by Anonymous Monk on Apr 02, 2002 at 15:08 UTC
    Thanks a lot everyone, this has been really helpful. Answered all my questions. Now I've got a new one =)

    Tachyon mentioned installing modules locally - I tried that by copying the Template.pm file to my cgi-bin directory and telling it to use Template.pm, but it kept telling me it couldnt find it. Is there a special syntax for using local modules?
      have you tried
      use ./Template.pm;
      Some modules also require that you actually install them, as opposed to just point at them, although I believe HTML::Template can simply be run this way.
      Err.. nevermind what I said. Now that I look closely at your post I realize that you probably have shell access and can just install the module locally (see below). Which is what you SHOULD do. I tend to forget that since my website doesn't allow telnet or ssh.

        ITYM use lib "/my/lib/dir"; use Template;.

        See also perldoc perlmodinstall and perldoc -q 'my own module' for more details.

        < delete >
        feh.... mozilla is not my friend today..