CGI Commandline Args消失了

时间:2014-07-31 02:37:25

标签: perl cgi

我有一个调用另一个cgi脚本的cgi脚本。

main_script.cgi脚本如下所示:

print qx/child_script.cgi arg1=foo arg2=bar/;

child_script.cgi看起来像这样:

use CGI;
use Data::Dumper;
my $query = CGI->new;
warn Dumper($query);

如果我在shell中执行./main_script.cgi,我会得到:

$VAR1 = bless( {
             '.parameters' => [
                                'arg1',
                                'arg2'
                              ],
             'use_tempfile' => 1,
             '.charset' => 'ISO-8859-1',
             '.fieldnames' => {},
             'param' => {
                          'arg1' => [
                                        'foo'
                                    ],
                          'arg2' => [
                                        'bar'
                                    ]
                        },
             'escape' => 1
           }, 'CGI' );

但是如果我在浏览器中访问myhost.com/main_script.cgi,则输出为:

$VAR1 = bless( {
             '.parameters' => [],
             'use_tempfile' => 1,
             '.charset' => 'ISO-8859-1',
             '.fieldnames' => {},
             'param' => {},
             'escape' => 1
           }, 'CGI' );

1 个答案:

答案 0 :(得分:1)

您的辅助脚本将从您的第一个脚本继承%ENV。如果CGI看到REQUEST_METHOD它会忽略命令行参数,而是加载来自QUERY_STRING等的内容。

要解决此问题,您必须首先本地化%ENV并删除REQUEST_METHOD

以下内容证明了这一点:

part1.pl

#!perl
use strict;
use warnings;

use CGI;
use Data::Dump;

my $q = CGI->new;

print "Content-type: text/plain; charset=iso-8859-1\n\n";
dd $q;

# Localize the REQUEST_METHOD so that the secondary process doesn't see it.
my $text = do {
    local $ENV{REQUEST_METHOD};
    qx(perl part2.pl arg1=val1 arg2=val2);
};

print $text;

part2.pl

#!perl
use strict;
use warnings;

use CGI;
use Data::Dump;

my $q = CGI->new;

print "Content-type: text/plain; charset=iso-8859-1\n\n";
dd $q;

访问http://localhost/cgi-bin/part1.pl?a=1&b=2会显示以下内容:

bless({
  ".charset"     => "ISO-8859-1",
  ".fieldnames"  => {},
  ".parameters"  => ["a", "b"],
  "escape"       => 1,
  "param"        => { a => [1], b => [2] },
  "use_tempfile" => 1,
}, "CGI")
Content-type: text/plain; charset=iso-8859-1

bless({
  ".charset"     => "ISO-8859-1",
  ".fieldnames"  => {},
  ".parameters"  => ["arg1", "arg2"],
  "escape"       => 1,
  "param"        => { arg1 => ["val1"], arg2 => ["val2"] },
  "use_tempfile" => 1,
}, "CGI")