calling cgi script from other cgi script

1.3k Views Asked by At

I found a puzzling behavior using perl cgi for which -- I guess -- there is a perfectly valid explanation but I couldn't find one. There is this question on stackoverflow, but what it describes seems to fail for me. The situation: I have two perl cgi scripts. One of them (let's say script.pl) can produce a website standalone, but also has a parameter let's call it BARE so that it will simply spit out non-formatted information.

#!/usr/bin/perl                                                                                                                                                                                                                   

use strict;
use CGI;

my $cgi = new CGI;

unless ( defined $cgi->param('BARE') ) {
    print $cgi->header, $cgi->start_html,$cgi->h1('Hello World'), $cgi->end_html;
} else {
    print "Bare";
}

Now I am trying to call this script with its 'BARE' parameter set as local script (as they are in the same directory) from another script (let's call it call_my_script.pl).

#!/usr/bin/perl                                                                                                                                                                                                                   

use strict;    
use CGI;

my $cgi = new CGI;

my $result = qx(./script.pl "BARE=1");

print $cgi->header,$cgi->start_html,$result,$cgi->end_html;

When running this script from the command line, I get:

> ./call_my_script.pl 
Content-Type: text/html; charset=ISO-8859-1

<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
Bare
</body>
</html>

When instead I call the same script via browser it returns

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"><head><title>Untitled Document</title>
</head><body>Content-Type: text/html; charset=ISO-8859-1

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"><head><title>Untitled Document</title>
</head><body><h1>Hello World</h1></body></html></body></html>

So despite the fact that I am calling the first script locally using qx and I am passing the parameter to get bare formatting, this does not get respected when calling things via browser.

I am probably overlooking something obvious, but I'd be grateful to understand what it is.

1

There are 1 best solutions below

7
On BEST ANSWER

A CGI program normally expects to fetch the parameter values from the environment variable QUERY_STRING. Passing parameter values on the command line is a debugging facility, and works only when the program is run from the command prompt

You could try something like this

my $result = do {
    local $ENV{QUERY_STRING} = 'BARE=1';
    qx{./script.pl};
};