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

这一生的挚爱 提交于 2019-12-25 03:27:15

问题


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.


回答1:


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 (i.e. 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.




回答2:


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.



来源:https://stackoverflow.com/questions/24947109/how-do-i-call-perl-cgi-script-from-another-perl-cgi-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!