html - Perl:使用 HTML::Entities 错误

标签 html perl apache cgi

我正在使用以下代码连接到运行 HTML 页面的服务器:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1st February 2005), see www.w3.org">

  <title>Calculator</title>
  <link type="text/css" rel="stylesheet" href="mystyle.css">
</head>

<body>
  <h1>Calculator</h1>

  <p>This is a calculator form that uses a CGI script.</p>

  <form method="post" action="./cgi-bin/badcalc.pl">
    Expression <input type="text" name="exp" size="10"> 
    <input type="submit" value="Calculate">
    <input type="reset">
  </form><br>

  <p>The cgi script that does the calculation may be viewed <a href=
  "cgi-bin/code2html.pl?file=badcalc.pl">here</a>.</p>
</body>
</html>

这链接到包含代码的 Perl 文件:

#!/usr/bin/perl

use strict;
use warnings;
use Safe; #using Sandbox
use CGI;
use HTML::Entities; #For encoding the output

my $query = new CGI;
my $exp = $query->param('exp');

print $query->header,
      $query->start_html(-title=>'Fixed calculator',
                    -style=>{'src' => '../mystyle.css'},
                -target=>'_blank'),
    $query->h1('Fixed calculator');

my $compartment = Safe->new();

##Defining a new sandbox
$compartment->permit_only(qw(atan2 sin cos exp log sqrt :default )); #Defines te functions that are permitted for execution
my $result = $compartment->reval($exp) or die("Error: ".$@);

#Execute the calculation, if input is trapped or an error occurs, die and print to log
if (defined $result)
{
    print "<br> ".encode_entities($exp)." = " encode_entities($result).""; ##Encodes the output to ensure that there is no problems on the page
}else
{
    print "<br> Oh dear! That input is not allowed or has been incorrectly formatted.\n"; #Makes error message suitable
}
print $query->end_html;

与旧版本(使用 eval() 而不是 reval())相比,这应该可以执行简单的计算并提供更多的安全性,但是当我尝试执行像 1+1 这样的简单计算时,它会返回一个内部服务器错误,错误日志位于下方。

Can't locate HTML/Entities.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at /home/1001542/public_html/cgi-bin/badcalc.pl line 10. BEGIN failed--compilation aborted at /home/1001542/public_html/cgi-bin/badcalc.pl line 10. [Mon May 06 10:58:05 2013] [error] [client 10.0.0.25] Premature end of script headers: badcalc.pl, referer: http://10.0.0.3/~1001542/calc.html

第 10 行是“使用 HTML::Entities;”。

有什么帮助吗?

**EDIT** 奇怪的是,原来的 Perl 文件工作得很好。 代码:

#!/usr/bin/perl

use strict;
use warnings;

use CGI;


my $query = new CGI;


my $exp = $query->param('exp');


print $query->header,
      $query->start_html(-title=>'Broken calculator',
                     -style=>{'src' => '../mystyle.css'},
                 -target=>'_blank'),
      $query->h1('Broken calculator');


my $result = eval($exp);
if (defined $result)
{
    print "<br> $exp = $result";
}else
{
    print "<br> oops! $@";
}

print $query->end_html; 

再次编辑** 我使用我大学提供的服务器来执行此操作,但现在已将其设置在我自己的虚拟机的 apache 服务器上。如果我修复了它,我会回复大家。

最佳答案

我设法自己整理了它。

因为我使用的是我大学提供的服务器,所以我无法安装所需的模块(HTML::Entities)。

然后我决定在虚拟机上我自己的 Apache Web 服务器上设置所需的模块,并在解决一些语法/拼写错误后设法让它以这种方式工作。

我安装 HTML::Entities 模块的方式是通过 synaptic 包管理器,只需搜索 libhtml-parser-perl 即可找到并安装该模块。 我重新启动了 Apache 和 VM,以防万一。

感谢大家的意见!

关于html - Perl:使用 HTML::Entities 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16396475/

相关文章:

javascript - 如何添加链接切换图片?

html - 如何创建两个 div 形状以相互叠加

perl - 如何将参数传递给 perl qw[]?

perl - 确定 Moose 属性和方法是从哪里继承的?

linux - Centos httpd 使用 7.5GB 内存

Linux 端点上的 Node.js 适用于 'api//method' 但不适用于 'api/method'

html - 将鼠标悬停在图像上时,奇怪的故障会导致内容移动

html - 使左浮动元素环绕右浮动元素

mysql - DBIx::Class:仅选择 has_many 大于零的结果

java - HttpPost 不包含 setHeader(String, String)