您可以使用以下代码在CGI脚本中获取查询参数:
#!C:\Perl64\bin\perl.exe
use CGI;
my $cgi = CGI->new();
my $file = $cgi->param( "file" );
print "Content-Type: text/plain\n\n";
my $text = do {
open(my $f, "<:encoding(UTF-8)", $file)
or die("Can't open \$filename\": $!\n");
local $/;
<$f>
};
print $text;
但如果请求是POST,则这不起作用,我将使用fetch API发送此请求:
await fetch('cgi-bin/script.pl?file=..\foo.php', {
method: 'POST',
body: 'Some Text'
}).then(r => r.text());
如果我在浏览器中打开脚本,我将获得文件的内容。有没有办法告诉CGI模块从QueryString获取参数而不是POST数据?或者任何其他在Perl中没有CGI模块的原始查询字符串解析方法?