本文介绍了为什么我的系统调用另一个CGI脚本可以在命令行上运行,而不能作为CGI程序运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a scriptA.cgi which calls scriptB.cgi.

scriptB.cgi需要一个参数。

scriptB.cgi requires a param.

我已经在scriptA.cgi中尝试过两个
我都尝试过:

I have tried both inside scriptA.cgi I have tried:

`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打印为空。

When I call scriptA.cgi from the command line it works as expected.However when I call scriptA.cgi through the browser scriptB.cgi is executed but it fails to read the incoming param and prints foo as empty.

scriptB不必是cgi,如果可以使用直接的.pl进行此操作更简单,那么从另一个调用一个cgi并传递参数呢?和args,我也很乐意这样做...但是arg必须是带空格的带引号的字符串。

scriptB does not have to be a cgi, if it's easier to do this with a straight .pl and args, I'm happy to do that too... but the arg has to be a quoted string with spaces.

欢迎所有想法。

推荐答案

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

If there is common functionality shared between many scripts, put it in a module

模块看似令人生畏,但它们确实非常简单。

Modules may seem intimidating, but they are really very simple.

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

请参见和以获取更多信息。

See perlmod, and the docs for Exporter for more information.

这篇关于为什么我的系统调用另一个CGI脚本可以在命令行上运行,而不能作为CGI程序运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 19:39