Apache 上的 Perl 脚本未检索 POST 数据

Perl scripts on Apache not retrieving POST data

我正在尝试在本地 MAMP apache2 服务器上获取预先存在的 PERL Web 应用程序 运行,但在获取 post 数据时遇到问题。该应用程序使用 cgi.pm 尝试从 $cgi->param 获取数据,但即使格式正确 post 数据以 x-www-form-urlencoded 格式发送,参数也始终为空。

此外,STDIN 始终为空,如果没有以 \*.

开头,则会引发错误

你好世界-form.cgi:

#!/[localpath]/perl5/perlbrew/perls/perl-5.24.0/bin/perl -d

use strict;
use warnings;

print "Content-type: text/html\n\n";
print qq(
  <!doctype html>
  <html>
  <body>
    <form action=\"hello-world.cgi\" method=\"POST\">
      <input type=\"text\" name=\"myparam\" />
      <input type=\"submit\" value=\"submit post var\" />
    </form>
  </body>
  </html>
);

你好-world.cgi:

#!/[localpath]/perlbrew/perls/perl-5.24.0/bin/perl -d

use strict;
use warnings;
use CGI;

my $q = CGI->new();

print "Content-type: text/html\n\n";
print "<p>key = " . $q->param('myparam') . "</p>\n"; #this is always empty
my @names = $q->param;
foreach my $name (@names) {
  print "<p>$name = " . $q->param($name) . "</p>"; #this never runs
}

my $postdata = '';
my $in = \*STDIN;
my $bytes = read($in, $postdata, $ENV{'CONTENT_LENGTH'});
print "<p>postdata = $postdata</p>\n"; # postdata is empty
print "<p>content length = " . $ENV{'CONTENT_LENGTH'} . "</p>\n"; # this number seems correct

foreach my $key (keys %ENV) {
  print "<p>$key --> $ENV{$key}</p>\n"; # nothing special here
}

httpd.conf:

...
LoadModule cgi_module modules/mod_cgi.so
LoadModule perl_module modules/mod_perl.so
...
ScriptAlias /cgi-bin/ "/[siteroot]/cgi-bin/"
Alias /perl/ "/Applications/MAMP/cgi-bin/"
<IfModule perl_module>
    PerlModule ModPerl::Registry
    <Location /perl>
        SetHandler perl-script
        PerlResponseHandler ModPerl::Registry
        PerlOptions +ParseHeaders
        Options +ExecCGI
    </Location>
</IfModule>

我是 运行 PERL v5.24.0,通过 perlbrew 使用这些模块:

> perlbrew list-modules

CGI
Date::Parse
Encode::Locale
HTML::Parser
HTML::Tagset
HTTP::Date
HTTP::Message
IO::HTML
LWP::MediaTypes
Perl
Sub::Uplevel
Test::Deep
Test::Needs
Test::NoWarnings
Test::Warn
Try::Tiny
URI

有什么想法吗?

从 shebang 中删除 -d 解决了这个问题。感谢 Corion 的回答。