gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:29:25 26 4
gpt4 key购买 nike

我有一个 scriptA.cgi,它调用 scriptB.cgi。

scriptB.cgi 需要一个参数。

我都试过了在我尝试过的 scriptA.cgi 中:

`perl -l scriptB.cgi foo="toast is good" `;

还有

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

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

有没有更丑陋的方式来调用另一个 cgi 并传入参数?

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

欢迎所有想法。

最佳答案

如果许多脚本之间有共同的功能,把它放在一个模块中

模块可能看起来很吓人,但它们确实非常简单。

文件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.

现在,在 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;

perlmod , 和 the docs for Exporter了解更多信息。

关于perl - 为什么我对另一个 CGI 脚本的系统调用可以在命令行上运行,但在作为 CGI 程序运行时却不行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2002664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com