使用HTML :: Template模块时出现问题

时间:2012-12-14 22:14:56

标签: perl cgi

我无法在CGI中执行HTML :: Template函数。

我正在按照我在此处找到的简单教程:http://metacpan.org/pod/HTML::Template

我在主服务器上的服务器上创建了一个新文件test.tmpl

我创建了一个名为frt.cgi的新文件...(这是问题吗?它应该是一个不同的文件扩展吗?)

#!/usr/local/bin/perl -w
use HTML::Template;

# open the html template
my $template = HTML::Template->new(filename => '/test.html');

# fill in some parameters
$template->param(HOME => $ENV{HOME});
$template->param(PATH => $ENV{PATH});

# send the obligatory Content-Type and print the template output
print "Content-Type: text/html\n\n", $template->output;


我修改了第1行以反映我为宿主提供的主机程序路径。我不知道-w是什么我只是知道我已经尝试了这个和没有它。我也尝试过改变代码:

use warnings;
use strict;
use CGI qw(:standard);
use HTML::Template;


我搜索了...
https://stackoverflow.com/search?q=HTML%3A%3ATEMPLATE+&submit=search
https://stackoverflow.com/search?q=HTML%3A%3ATEMPLATE
https://stackoverflow.com/search?q=HTML%3A%3ATEMPLATE+PERL&submit=search

但我仍然没有看到答案。 我甚至搜索谷歌.TMPL编码因为我认为可能需要一些特殊类型。请帮忙。

2 个答案:

答案 0 :(得分:0)

如果您查看服务器日志,您可能会看到以下行的错误消息:

HTML::Template->new() : Cannot open included file /test.html : file not found.

您需要在文件系统上提供路径,而不是相对于生成的文档的URI。

答案 1 :(得分:0)

首先,您可能指定了错误的路径 - 将/test.html更改为test.html。 此外,您的系统中可能没有$ ENV {HOME}变量,因此将标志die_on_bad_params设置为0:

my $template = HTML::Template->new(
filename => 'test.html',
die_on_bad_params => 0,
);

另外,不要忘记将你的Perl文件标记为chmod 755的可执行文件。

选项-w使Perl启用警告,因此之后无需编写use warnings;。 您可以使用模块B :: Deparse检查Perl命令行选项的作用,如下所示($ ^ W变量禁用/启用警告):

perl -w -MO=Deparse -e "print;"

这将打印:

BEGIN { $^W = 1; }
print $_;