fraktalisman has asked for the wisdom of the Perl Monks concerning the following question:

How can I call a CGI script from another CGI script - more independently than by using 'do' or 'require'.

I want the script that is called by the first one to run as independently as if it was itself called by a web browser. The two scripts should share no variables, and even the (Apache) environment variables shall be independent for each script.

I want to avoid opening a new browser window, because the called script resides in a directory of its own which has a set of .htaccess / .htpassword files, so the web user would have to log in which should not be necessary.

Is there any way to achieve this? I found no way in the documentation, nor using super search.
  • Comment on Call a CGI script from another CGI script most independently

Replies are listed 'Best First'.
Re: Call a CGI script from another CGI script most independently
by sauoq (Abbot) on Jul 29, 2003 at 17:23 UTC

    Use LWP. If you only need to do a GET request, you should be able to get by with LWP::Simple.

    -sauoq
    "My two cents aren't worth a dime.";
    
      The LWP suggestions seems to come closest. Calling via system would mean I had to rewrite the scripts - they are originally intended to be called by a browser.
Re: Call a CGI script from another CGI script most independently
by freddo411 (Chaplain) on Jul 29, 2003 at 18:12 UTC
    To execute another CGI (#2) from within your CGI (#1) you have two basic options

    Option 1

    Execute as if from the command line. There are a couple of variations on this, as discussed by shemp in his reply. In this case the web server is completely bypassed, and you'll need to simulate any environment vars that your script is expecting to be populated by the web server (browser version for example) and simulate any expected parameters coming into the CGI as well. You may want to do this if, for example, you wish to bypass the .htpasswd access controls.

    This can be as simple as this code snippet of mine from one of my tests (using backticks):

    $err = `./makeCounter-report.cgi foo=bar`; ok( $err , '*** makeCounter-report : compile/run ');
    Note that this particular CGI uses the debug option allowing command line args to be tranmogrified into params:
    use CGI qw/:standard -debug/;

    Option 2

    Access the CGI #2 as if you were a browser. This can be accomplished using the LWP or CGI module for example as discussed by LameNerd or sauoq . In this case since you have gone through the web server, you'll be forced to abide by all the .htaccess permissions.

    -------------------------------------
    Nothing is too wonderful to be true
    -- Michael Faraday

Re: Call a CGI script from another CGI script most independently
by LameNerd (Hermit) on Jul 29, 2003 at 17:22 UTC
    You could do something like ...
    ... use CGI; ... my $out = new CGI; ... print $out->redirect( -URL => "/cgi-bin/the_other_CGI_script" );
    ... in your CGI script.
Re: Call a CGI script from another CGI script most independently
by shemp (Deacon) on Jul 29, 2003 at 17:22 UTC
    Do you want its output to also go back to the client that called the first script? That does make a difference.
    If the 2 scripts share no variables, where does the second one get its apache environment from? Do you mean that the second script invokes with a separate copy of the environment from the first, or an ENV attained elsewhere?

    How about a system call - blocks the calling program until the called program is done.
    or an exec() - which will end the current program, and replace it with the new one.

    Each solution has different caveats.
Re: Call a CGI script from another CGI script most independently
by mildside (Friar) on Jul 30, 2003 at 01:36 UTC
    If by 'independantly' you mean 'simultaneously' then you should look at using fork in combination with exec.

    Cheers