为什么我的系统调用另一个CGI脚本在命令行上运行,但在作为CGI程序运行时却不行?

Gen*_* S. 2 perl cgi

我有一个调用scriptB.cgi的scriptA.cgi.

scriptB.cgi需要一个参数.

我在scriptA.cgi里面都尝试过,我试过了:

`perl -l scriptB.cgi foo="toast is good" `;
Run Code Online (Sandbox Code Playgroud)

以及

@args = ("perl", "-l", "scriptB.cgi", "foo=\"toast is good\"");
system(@args);
Run Code Online (Sandbox Code Playgroud)

当我从命令行调用scriptA.cgi时,它按预期工作.但是当我通过浏览器调用scriptA.cgi时,脚本执行了.执行但是它无法读取传入的参数并将foo打印为空.

是否有一种不那么丑陋的方式从另一个调用一个cgi并传入params?

scriptB不一定是cgi,如果使用直接的.pl和args更容易做到这一点,我也很乐意这样做......但是arg必须是带空格的带引号的字符串.

欢迎所有的想法.

dao*_*oad 5

如果许多脚本之间共享共同功能,请将其放在模块中

模块可能看起来令人生畏,但它们非常简单.

档案SMSTools.pm:

package SMSTools;
use strict;
use warnings;
use Exporter qw(import);

# Name subs (and variables, but don't do that) to export to calling code:
our @EXPORT_OK = qw( send_sms_message );

our @EXPORT = @EXPORT_OK;  
# Generally you should export nothing by default.
# However, for simple cases where there is only one key function
# provided by a module, I believe it is reasonable to export it by default.


sub send_sms_message {
    my $phone_number = shift;
    my $message      = shift;

    # Do stuff.

    return; # Return true on successful send.
}

# Various supporting subroutines as needed.

1;  # Any true value.
Run Code Online (Sandbox Code Playgroud)

现在,使用您的模块foo.cgi:

use strict;
use warnings;
use CGI;

use SMSTools;

my $q = CGI->new;

my $number = $q->param_fetch( 'number');
my $message = $q->param_fetch( 'msg');

print 
    $q->header,
    $q->start_html,
    (    send_sms_message($number, $message) 
         ? $q->h1("Sent SMS Message") 
         : $q->h1("Message Failed")
    ),
    q->end_html; 
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅perlmodExporter的文档.