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

No, I don't know anything about perl. I know a bit about some other programming languages, but perl seems a little overwhelming. So many modules!

Anyway, on to my problem. I want to write a bit of code that takes a template HTML file and sticks a bit of text from a text file in at a given location. I've been trying to use HTML::template - I've managed to sub variables but I'm having trouble doing files.

I gave up on HTML::template for a while and tried to read each file into a looong scalar and do a search/replace:

template =~ s/TEXTGOESHERE/$textfile/se

I had no luck with either of these, probably due to my total lack of perl-fu. ("Snatch the pebble from your hand master? What pebble?")

If that werent enough, theres more. I have two web servers to work with - one has every perl module known to man, and i use it to test my scripts. Unfortunately the 'production' server has absolutely nothing but barebones perl, and I don't relish the task of convincing the admin to install any modules - the more I ask for the longer it will take (months+) so a solution that uses as few modules as possible would be most appreciated, although anything at all would still be great. Thanks!
  • Comment on perl fool - inserting text into html doc

Replies are listed 'Best First'.
Re: perl fool - inserting text into html doc
by grep (Monsignor) on Apr 02, 2002 at 06:11 UTC
    Unfortunately, recreating the functionality of a Templating system like Mason, HTML::Template, or Template Toolkit, most likely will take you more time than getting that sysadmin to install these modules, that and all 3 solutions are very stable and well tested.

    Not that either of the other systems are not excellent choices, but I prefer Template Toolkit. IMO it is very easy to get up and running and will work with *nix, Win32, Apache, and IIS. Take a look a the tutorial .

    If it helps the install for Template Toolkit takes less than 5 minutes


    FWIW my company has just completed a DB driven dynamic website with TT in about 50 lines of perl code.

    grep
    grep> cd /pub
    grep> more beer

Re: perl fool - inserting text into html doc
by tachyon (Chancellor) on Apr 02, 2002 at 06:46 UTC

    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

      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.
Re: perl fool - inserting text into html doc
by dws (Chancellor) on Apr 02, 2002 at 07:01 UTC
    Your problem has a simple solution, though you should still check out templating mechanisms.

    Instead of   template =~ s/TEXTGOESHERE/$textfile/se try   $template =~ s/TEXTGOESHERE/$textfile/g; The /s modifier that you're using says to that a . in a regular expression should match a newline. Since you don't have a . in your regular expression, this is a no-op.

    The /e modifier says to treat the replacement string as a Perl expression, and to use that value of that expression when making the replacement. This is almost certainly not what you intend.

    By using /g instead, you're saying to replace every occurance of TEXTGOESHERE with the contents of $textfile. If you have only one TEXTGOESHERE, you'll only get one substitution.

    Try it.

Re: perl fool - inserting text into html doc
by webadept (Pilgrim) on Apr 02, 2002 at 06:37 UTC
    I've worked in situtaions like that and it takes a bit, and I'm just going drop a little idea here, not really advice. One of the things I found works really well is to parse up pages into seperate files. Header, Footer, Body, Menu, CSS etc... Areas of the page that change and areas that don't. Then, build the part that changes, and then build the new page from the template files. Most HTML pages can be parsed out this way to some degree, and you may find that yours can be done this way too.

    This method works if its not "real time data" you are after, and if you have ftp access to the main server. The program builds the page, and then sends it to the server replacing the existing one.

    Another simple idea is to put HTML comments in to the file and parse the page into array cells or hash cells using those as markers. Then feed the new information into the proper cell and put the file back out. Neither one of these ideas requires any modules at all, except the core ones. NET::FTP is about it if you want to do this remotly.

    Modules are good, because they are completed tested code, but like your sysadmin, not all ISP's want to deal with the hassles that they bring. So, its good to find tricks to do as well.

    Of course you could build the template pages on the one server and have the program update the production server using a cron job, or something of that nature.

    Good luck to you and I hope this helps

    webadept

Re: perl fool - inserting text into html doc
by cfreak (Chaplain) on Apr 02, 2002 at 15:50 UTC
    If you just have a file of text you want to insert into the HTML::Template then just do this in your template file
    <tmpl_include name="somefile.txt">

    It will insert it in that spot if its in the same PATH as the template.

    Chris

    Some clever or funny quote here.