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

标签 perl cgi

我有一个 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/

相关文章:

perl - 为什么 pleenv install-cpanm 安装到错误的位置?

c++ - 内容处置文件名中的特殊字符

python - 显示部分搜索结果的最佳实践(当它们来自辅助服务器时,一一显示)

perl - 如何从模块的 CPAN 页面判断它是否在核心发行版中?

arrays - 将 csv 数据转换为特定格式 DYNAMIC

perl - 如何使用 Perl 提供图像并具有一定的安全性和最少的资源?

c++ - 如何为 C++ CGI 脚本配置 apache?

apache2 - 访问 Docker 容器内的 cgi 脚本时出错,不允许操作 : Couldn't bind unix domain socket

arrays - 在 perl 中打印数组散列值

正则表达式匹配排除其他一些词