0
votes

I would like to make use of the HTML::Template module but somehow I can't set it up to work properly. Here is a very simple representative code I'm testing on:

use strict;
use warnings;

use CGI;
use HTML::Template;

my $test = new CGI;
my $tmpl = HTML::Template->new(filename => 'TemplateSimple.html');

$tmpl->param(
    title => 'Test',
    body  => '<p>This is a test</p>',
);

my $out = $test->header(
    -type    => 'text/html',
    -charset => 'utf-8'
);


print $out;
print $tmpl->output;

When calling the page I always end up with the browser displaying the server error message:

502 - Web server received an invalid response while acting as a gateway or proxy server.

TemplateSimple.html

<!DOCTYPE html>
<html lang="de">
    <head>
        <meta charset="utf-8">
        <title><TMPL_VAR NAME=title></title>
        <link rel="SHORTCUT" ICON href="favicon.ico" />
    </head>

    <body>
        <TMPL_VAR NAME=body>
    </body>
</html>

I have to use CGI, because I want to process user input on the web page, but I would like to define the basic HTML structure in a template where I can insert code segments as necessary.

Edit

I think that it could have something to do with different configs between the local Perl (run from eclipse, which does run fine) and the Perl CGI config. Does anybody know of such a case?

Edit

After setting up a Perl CGI configuration in Eclipse, the script runs as expected from the local host. However, the problem when calling the page from an external source persists. So like DaveCross suggested, the bug lies in the web server configuration rather than the Perl script.

1
What gets written to the web server error log? What do you see if you compile the program from the command line (with perl -c)? Is HTML::Template installed? It's 2017, why are so many people still writing CGI programs? :-(Dave Cross
Hehe, sorry the company I work for still uses it...I would prefer something more modern too. I don't know where the server error log is, but when I run the perl script within eclipse, i get the following console output: Content-Type: text/html; charset=utf-8 HTML::Template=HASH(0x5a2f74). When i use print $tmpl->output the console output looks as desired, but the in the browser I still get the error message.N. Maks
@DaveCross because no-one is writing a proper full-featured guide on how to deploy PSGI stuff. I hear you have a fancy blog... want some TUITs? :)simbabque
@N.Maks - You code works (well, modulo needing the ->output call, but you've already spotted that). So the problem is in the way your program is interacting with your web server. And you've told us nothing about how that is configured. Saying "I don't know where the server error log is" isn't very helpful. You can't do serious web development without access to the error log. I'm confident there will be useful clues in there.Dave Cross
@N.Maks If you work it out, please come back and write an answer - so it helps other people with the same problem.Dave Cross

1 Answers

1
votes

When initializing the HTML::Template object in the Perl script

my $tmpl = HTML::Template->new(filename => 'TemplateSimple.html');

I had to specify the full path instead of just the filename, so

my $tmpl = HTML::Template->new(filename => 'C:/inetpub/wwwroot/Project/TemplateSimple.html');

This solved my problem.

To whom it may interest, the webservice was set up with IIS 7, in the very basic and standard way.