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

I am writing CGIs in Perl. I have a first CGI (e.g. called CGI1) which generates a web page. I then want to modify this web page with a second CGI (e.g. CGI2). I would therefore like to be able to call CGI1 within CGI2 and direct the output of CGI1 into a string that I can then manipulate within CGI2. How do you do that?

Replies are listed 'Best First'.
Re: Redirecting perl output
by btrott (Parson) on Feb 10, 2000 at 00:17 UTC
    You can call one program from another and capture its output like this:
    my $output = `/foo/bar.cgi`;
    Although that isn't actually calling it as a CGI script. I suppose to do that you could use LWP::Simple and actually make an HTTP request to the CGI script to get the output:
    use LWP::Simple; my $content = get 'http://www.perl.com/'; # manipulate HTML page in $content # ... print $content;
    However, I'm not sure that this isn't a situation where you might want to rethink what you're doing... in other words, perhaps you can combine the functionality of these two CGI scripts so that you don't have to call one from the other, particularly since either of the above solutions would be a performance hit.
Re: Redirecting perl output
by h0mee (Acolyte) on Feb 10, 2000 at 02:18 UTC
    Are you using GET or POST?

    You have several options: 1) You can pass back an HTML redirect to the browser, with the name value pairs in the CGI you are redirecting to:

    http://www.blah.org/cgi-bin/CGI2.pl?name1=value1&name2=value2
    etc...

    2) You can call the CGI script directly through the LWP method refered to in the previous post

    3) You can call the CGI script within your own CGI using backticks or the do command. Since the CGI parameters are stored in the %ENV hash, the subprogram should inherit scoping. You have an additional worry about security...