简体   繁体   English

如何将变量从一个CGI脚本传递到另一个?

[英]How can I pass variables from one CGI script to another?

I have a CGI perl script called install-app-pl.cgi : 我有一个名为install-app-pl.cgi的CGI perl脚本:

#!/usr/bin/perl -w

print header('text/html');

use strict;
use CGI ':standard';

# Get me some vars

my @params = param();

my $APP_NAME          = param('app_name');
my $APP_WEB_PORT      = param('app_web_port');
my $APP_WEB_USER      = param('app_web_user');
my $APP_WEB_PASS      = param('app_web_pass');
my $DOWNLOAD_DIR      = param('download_dir');
my $CONFIG_DIR        = param('config_dir');
my $LIBRARY_DIR       = param('library_dir');
my $TEMP_DOWNLOAD_DIR = param('temp_download_dir');

# Run another script

if ( $APP_NAME ) {
    print "Installing $APP_NAME...";
    print "<pre>";
    system ("perl /var/www/mysite.local/public_html/lib/$APP_NAME/install-$APP_NAME.pl");
    print "</pre>" ;
}
else {
    print "No app specified, check the error log";
}

I'm trying to get it to pass the variables defined from the CGI parameters to install-$APP_NAME.pl 我正在尝试将其从CGI参数定义的变量传递给install-$APP_NAME.pl

#!/usr/bin/perl -w

print header('text/html');

use strict;
use CGI ':standard';

require "/var/www/mysite.local/public_html/cgi-bin/install-app-pl.cgi"

# Echo my vars

print "$CONFIG_DIR $DOWNLOAD_DIR $LIBRARY_DIR $PGID $PUID $TZ $APP_WEB_PORT";

But I'm not sure of the best way to pass those on. 但是我不确定传递这些最好的方法。

Are you sure that install-app-pl.cgi is a CGI program? 您确定install-app-pl.cgi是CGI程序吗? Are you sure that it's not just a Perl command-line program? 您确定它不仅是Perl命令行程序吗? I mean, I see how it's named, but it seems very strange to call a CGI program using system() like that. 我的意思是,我知道它的命名方式,但是使用诸如此类的system()调用CGI程序似乎很奇怪。

And the difference is crucial here. 在这里,差异至关重要。 CGI programs access their parameters in a different way command-line programs. CGI程序通过命令行程序以不同的方式访问其参数。

If it really is a CGI program, then you have a few options: 如果它确实是一个CGI程序,那么您有几种选择:

  • Make an HTTP request to it (using something from the LWP bundle of modules ). 向它发出HTTP请求(使用LWP模块捆绑中的内容 )。
  • Use CGI.pm's debugging mechanism to call it the same way as you're currently calling it, but passing the CGI parameters like foo=xxx&bar=yyy&baz=zzz (see the DEBUGGING section of the CGI.pm documentation for details). 使用CGI.pm的调试机制以与当前调用它相同的方式进行调用,但传递CGI参数,例如foo=xxx&bar=yyy&baz=zzz (有关详细信息,请参见CGI.pm文档DEBUGGING部分 )。 This, of course, relies on the program using CGI.pm and it feels a bit hacky to me. 当然,这依赖于使用CGI.pm的程序,这对我来说有点黑。
  • Ask yourself if the program really needs to be a CGI program if you're calling from another program using system() . 问自己,如果您正在使用system()从另一个程序进行调用,则该程序是否真的需要成为CGI程序。 And then decide to rewrite it as a command-line program. 然后决定将其重写为命令行程序。 If you want both a CGI version and a command-line version, then you could move most of the code to a module which could be used by two thin wrappers which just extract the parameters. 如果您同时需要CGI版本和命令行版本,则可以将大多数代码移至模块,该模块可以由两个仅提取参数的精简包装器使用。

A few other points about your code. 有关代码的其他几点。

  • Perl 5.6 (released in 2000) introduced a use warnings pragma. Perl 5.6(于2000年发布)引入了use warnings Most people now use that in place of -w on the shebang line. 现在,大多数人用它代替shebang行上的-w
  • It seems weird to call the header() function before loading the CGI module that defines it. 在加载定义它的CGI模块之前调用header()函数似乎很奇怪。 It works, because the use is handled at compile time, but it would be nice to re-order that code to make more sense. 之所以有效,是因为use是在编译时处理的,但是最好重新排序该代码以使其更有意义。
  • Similarly. 同样。 most people would have use strict (and use warnings ) as the very first things in their program. 大多数人会在程序中首先use strict (并use warnings )。 Immediately after the shebang line. 在shebang行后立即。
  • system() returns the return value from the process. system()返回过程的返回值。 If your second program produces useful output that you want displayed on the web page, you should use backticks instead. 如果第二个程序产生了想要显示在网页上的有用输出,则应改用反引号。
  • If all of your output is going to be in a <pre> element, why not just remove that element and return a content type of "text/plain" instead? 如果所有输出都将放在<pre>元素中,为什么不删除该元素并返回内容类型为“ text / plain”呢?

Update: And I'd be remiss if I didn't reiterate what many people have already said in comments on your original question - this sounds like a terrible idea. 更新:如果我不重申许多人在对您的原始问题的评论中已经说过的话,那我将不予理--这听起来像一个可怕的主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM