Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Invoking a cgi script from another cgi script using system()

by bibliophile (Parson)
on Dec 31, 2014 at 21:33 UTC ( #1111861=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to have a CGI script ("a.pl") call another CGI script ("b.cgi"), using system(). ("b.cgi" is used elsewhere; I just want to grab its (json) output and do things with it (using Capture::Tiny).

This works fine from the command line, but from the browser, "b.cgi" doesn't get the parameters that "a.pl" sends... or rather, the parms end up in @ARGV rather than $query->param.

Here's a minimal example. In this example, I'm not trying to process b.cgi's output in a.pl... I just want to show that b.cgi is not getting the parameters in $query->param.

a.pl

#!/usr/bin/perl use strict; use warnings; my @args = ("animal=duck", "vegetable=carrot"); system("/usr/bin/perl", "/tmp/b.cgi", @args);

and b.cgi:

#!/usr/bin/perl use warnings; use strict; use CGI; my $query = new CGI; print "Content-type: text/plain\n\n"; print "b.cgi query dump:\n"; print $query->Dump . "\n"; print "b.cgi argv dump:\n"; print join(',', @ARGV); print "\n";

Output when a.pl is invoked from the command line:

Content-type: text/plain b.cgi query dump: <ul> <li><strong>animal</strong></li> <ul> <li>duck</li> </ul> <li><strong>vegetable</strong></li> <ul> <li>carrot</li> </ul> </ul> b.cgi argv dump: animal=duck,vegetable=carrot

Output when a.pl is invoked through the browser:

b.cgi query dump: <ul></ul> b.cgi argv dump: animal=duck,vegetable=carrot

Is there some difference between a command-line invocation and a system() invocation?

I'm clearly misunderstanding something here... Any help you could give would be... well... helpful!

update: spelling

Replies are listed 'Best First'.
Re: Invoking a cgi script from another cgi script using system()
by Anonymous Monk on Dec 31, 2014 at 22:50 UTC

    you have to read the source for the CGI.pm module , someone may have edited it to enable/disable @ARGV processing

    Although, if your cgi program consisted of

    use MyApp; MyApp->runcgi;

    then you wouldn't have to use system to call your cgi program, you could use MyApp .... regular perl programming

      Ah, yes. That's better :-)

      (In the real project, I'm going to pull the guts out of "b.cgi" into a new "c.pm", and have both a.pl and b.cgi use c.pm to do the heavy lifting, without needing to mess about with having a.pl capturing output from b.cgi)

      Thanks for the tip on CGI.pm, too. $DEBUG was actually set to 1 (meaning that this *should* have worked...) I still don't know why it didn't work, but I learned something new today, and that's always a win.

      Thanks.

        You could also write like this because webservers won't put stuff into @ARGV
        my $query = @ARGV ? CGI->new({ @ARGV }) : CGI->new;

        Invoke as

        system $^X, 'foo.cgi', qw/ key value key value /;
Re: Invoking a cgi script from another cgi script using system()
by bibliophile (Parson) on Dec 31, 2014 at 21:36 UTC

    I should note that invoking b.cgi from the browser ("/cgi-bin/b.cgi?animal=dog&vegetable=radish") works, giving:

    b.cgi query dump: <ul> <li><strong>animal</strong></li> <ul> <li>dog</li> </ul> <li><strong>vegetable</strong></li> <ul> <li>radish</li> </ul> </ul> b.cgi argv dump:

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1111861]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2020-11-30 03:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?