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

I need to call a cgi script from within a cgi script and display the results of the second script, not the calling script....any ideas?

Edited 2002-10-01 by Ovid

  • Comment on Calling a CGI script from a CGI script (was: CGI)

Replies are listed 'Best First'.
Re: CGI
by Ovid (Cardinal) on Oct 01, 2002 at 15:29 UTC

    Not to sound discouraging, but this is an idea which is often considered, but usually shouldn't be. Whenever you find yourself needing to do something "tricky", you need to step back and ask yourself "what problem am I trying to solve"? I have, on far too many occassions, found myself working on a solution without bothering to reconsider the question when someone else comes along and points out a much better way to solve my problem.

    So, what problem are you trying to solve? As far as I can tell, you're saying "here's my solution. How do I implement it?" Since you're not sure how to implement it, you're really not sure that you have a solution! :)

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: CGI
by joe++ (Friar) on Oct 01, 2002 at 13:49 UTC
    Return a "302 Found" header, with the url of the new script. See perldoc CGI for documentation and examples.

    --
    Cheers, Joe

Re: CGI
by hiseldl (Priest) on Oct 01, 2002 at 14:35 UTC

    If your first CGI is modular, e.g. it has a sub you can call that produces results, you could add a "1;" to the end of the script, then require it into your first CGI and call the sub. Or you can wrap the body of the script into a sub and then require/import and call your sub.

    For example, I had a few small scripts where I wrapped them in a sub something like:

    #!perl -wT use strict; use CGI; # use more modules; &mysub1(); exit 0; sub mysub1 { # the body of the script } 1;
    and then used CGI::Application to call the wrapped methods as run modes. If you can't install CGI::Application, you should still be able to require/import the scripts and call the methods directly.

    --
    hiseldl
    What time is it? It's Camel Time!

Re: CGI
by sch (Pilgrim) on Oct 01, 2002 at 13:49 UTC

    The way I've done this is to have the first CGI script generate a form, which then calls the 2nd

    Alternatively, you can look at using redirects

Re: Calling a CGI script from a CGI script (was: CGI)
by nutshell (Beadle) on Oct 01, 2002 at 17:20 UTC
    To me, at least, your problem seems a bit confusing :)

    But, uhhhh... this isn't what you want is it:

    open FH, "/path/to/secondscript.cgi|"; # note the pipe! print join '', <FH>; close FH;

    --nutshell

Re: CGI
by Anonymous Monk on Oct 01, 2002 at 13:59 UTC
    if you haven't sent a header yet, try to pass control with exec()