简体   繁体   中英

How do I call perl cgi script from another perl cgi script

I have a cgi script which takes few parameter like below.

testScript.cgi?arg=1&arg2=SomeThingElse&arg3=otherThing.....

The above script works well and perfectly.

Now I have another perl cgi script called mySecondScript.cgi . It does its own thing but I want to call textScript.cgi with arguments which are calculated in this script. How do I do that. Any elegant solution will be appreciated.

You probably want to use LWP::Simple to call the second program. Inside mySecondScript.cgi you would need something like this:

my $output = get("http://someserver.somedomain/testScript.cgi?arg=1&arg2=SomeThingElse&arg3=otherThing");

This will return the output from the CGI program (ie the HTML page that it generates). If you want more control over what you get back, then you need to use LWP::UserAgent .

my $ua = LWP::UserAgent->new;
my $resp = $ua->get("http://someserver.somedomain/testScript.cgi?arg=1&arg2=SomeThingElse&arg3=otherThing");

$resp will be an HTTP::Response object.

Alternatively, if both of your program are running locally, it might be more convenient to refactor the important bits of testScript.cgi into a module that you can just load and use within mySecondScript.cgi.

There are several ways of calling a "system call" from inside of an perl script. That might be any shell operation or even another perl script. My favorite is qx/[command]/

Look at this mini example to see how it works

my $result = qx/pwd/;
print "result: $result\n";

pwd is a shell command, as would be in your case eg

my $result = qx/mySecondScript $param1 $param2/;

This way you can even tunnel the results from the inner script to the outer script. Other ways could be backticks or the "system" command, but qx ist my personal favorite.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM