0
votes

For a project at work, I need to call one Perl/CGI script from another. An extremely simplified version of that script that I'm using for testing is here (the real scripts don't use recursion, but this way I don't have to copy & paste a lot of code):

#!/usr/local/bin/perl
use CGI qw(:standard);
use POSIX 'setsid';
$|=1;

print "Content-type: text/html\n\n";

@names = param;
print "@names";

if(defined(param('submit'))){
        #delete_all();
        system('perl testParams.pl abc=123');
        exit(0);
} else{
        print "NO SUBMIT PARAM";
}

What this script is supposed to do:

  1. Print names of all parameters.
  2. If a submit parameter is defined, run the script again but with a parameter called "abc".
  3. If a "submit" parameter is not defined, print "NO SUBMIT PARAM".

What the script actually does:

  1. Print names of all parameters.
  2. If a "submit" parameter is defined, run the script again with the same parameters that the original script was run with.
  3. If a "submit" parameter is not defined, print "NO SUBMIT PARAM".

Any idea what's causing Perl/CGI to ignore the new parameters and instead send the old ones when running the script?

2

2 Answers

2
votes

CGI only processes command line args when a CGI environment isn't found. The CGI environment is being inherited from the parent process. You could wipe it using

my %CGI_VARS = map { $_ => 1 } qw(
   REQUEST_METHOD
   CONTENT_LENGTH
   CONTENT_TYPE
   ...
);

local %ENV =
    map { $_ => $ENV{$_} }
     grep !$CGI_VARS{$_} && !/^HTTP/,
      keys(%ENV);

But this reeks of bad design. Really, your two scripts should be thin front ends to a common module.

You could even use the same script for both (by using a symlink), but alter the behaviour based on the URL used to call up the script.

0
votes

From CGI with nested apps, each calling param() to get their args the simple answer is to create new CGI object from @ARGV

#!/usr/bin/perl  --
use strict;
use warnings;

use CGI ();

Main( @ARGV );
exit( 0 );

sub Main {
    my $cgi = @_ ? CGI->new(@_) : CGI->new;
}