thealexbaron
thealexbaron

Reputation: 1577

CGI Commandline Args Disappearing

I have a cgi script that calls another cgi script.

main_script.cgi script looks like this:

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

child_script.cgi looks something like this:

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

If I do ./main_script.cgi in the shell, I get:

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

But if I visit myhost.com/main_script.cgi in the browser, the output is:

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

Upvotes: 0

Views: 135

Answers (1)

Miller
Miller

Reputation: 35218

Your secondary script is inheriting %ENV from your first script. If CGI sees a REQUEST_METHOD it ignores the commandline parameters and instead loads things from the QUERY_STRING, etc.

To fix this, you must first localize the %ENV and delete the REQUEST_METHOD.

The following demonstrates this:

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;

Accessing http://localhost/cgi-bin/part1.pl?a=1&b=2 displays the following:

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")

Upvotes: 1

Related Questions