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

I'm sure this is ridiculously simple, but I'm missing something.

I pass <form> data to script1.cgi.
Based on some criteria I either want to process the data in script1.cgi or pass it, via a post, to script2.cgi. In that case, script1.cgi ends and the HTML doc is returned to the client from script2.cgi

A search of PM yielded HTTP::Request to local script but it seems that this returns something to script1.cgi which is not what I want.

### script1.cgi use strict; use LWP::UserAgent; use CGI; my $q = new CGI; ## determine if HTML returned from this script OR pass the data to nex +t script. my $name = $q->param('name'); if (use next script) { ## I can do it like this but that does not 'post' the data print "location: script2.cgi?querystring\n\n"; ## OR.... my $ua = LWP::UserAgent->new; # pass one item my $req = POST "/cgi-bin/script2.cgi", [ name => $name ]; # pass all items ?? my $req = POST "/cgi-bin/script2.cgi", $q; print $ua->request($req); exit; } else { &returnHTML(); exit; } ### script2.cgi use strict; use CGI; .. ## process posted data &returnHTML(); exit;

Replies are listed 'Best First'.
Re: Pass 'posted' data to a 'chained' script
by tilly (Archbishop) on Feb 08, 2005 at 06:58 UTC
    The "right solution" is to write both scripts as functions in a library, and then make your CGI just require (or use) that library and call the appropriate function.

    Now your problem goes from how to execute a script and create its environment etc to how to have one function call another.

    If you don't wish to take this solution, then your best bet really is to make a local http request, have the data be returned to the first script, and have it echo it along to the webserver. This is definitely not a great approach, which is why I'm recommending the library approach above.

      The website, where this is to be used, has around 100 scripts, each independant of the other and many driven by a database. The site is organised such that each user gets a different combination of scripts. I have used the 'library' approach whenever practically feasable.

      What I'm trying to do here is to provide, at login, a different starting page for different groups of users. (6+ differerent pages that do not lend themselves to a 'library' approach )

        It should always be possible to take the library approach. If you're having trouble getting it to work, CGI::Application may help.

        Hmm ... perhaps you are shooting for the BigBallOfMud methodology?