简体   繁体   English

通过mod_perl将命令行perl脚本的输出发送到浏览器

[英]Sending the output of a command-line perl script to the browser via mod_perl

I have a plain perl script that can be run from the command-line via perl -w test.pl . 我有一个普通的perl脚本,可以通过perl -w test.pl从命令行运行。 I then have a mod_perl2 script that can be accessed from a web browser. 然后,我有了一个可以从Web浏览器访问的mod_perl2脚本。 I want to have the latter call the former and send the output to the browser, flushing as it goes. 我想让后者调用前者,并将输出发送到浏览器,并随即刷新。

The mp2 script doesn't have a shebang line, because it's mod_perl, so it doesn't know where perl lives. mp2脚本没有shebang行,因为它是mod_perl,所以它不知道perl放在哪里。 Also, calling system('perl -wc:\\\\path\\\\to\\\\test.pl') results in the error: 同样,调用system('perl -wc:\\\\path\\\\to\\\\test.pl')导致错误:

'perl' is not recognized as an internal or external command,
    operable program or batch file.

for some reason I can't figure out, since it's in my path variable. 由于某种原因,我不知道,因为它在我的path变量中。 Maybe not for the account Apache is running under. 也许不是Apache正在运行的帐户。

Is there some way to run the script and capture its output without calling the perl executable via system() ? 有什么方法可以运行脚本并捕获其输出,而无需通过system()调用perl可执行文件吗? Ie, something that uses the interpreter that's already loaded? 即,使用已经加载的解释器的东西吗?

Aside from the mod_perl issue, the location of the current perl interpreter is in $^X . 除了mod_perl问题,当前perl解释器的位置在$^X If you aren't running under mod_perl, that's how you should find perl. 如果您不是在mod_perl下运行,那应该是找到perl的方式。 Inside mod_perl, of course, you probably don't want that one since it's baked into apache. 当然,在mod_perl内,您可能不想要那个,因为它已经被烘焙到了Apache中。

Some people have mentioned %PATH%, but I recommend against that. 有人提到%PATH%,但我建议不要这样做。 Just find out the full path to Perl and use it explicitly without relying on the %PATH%. 只需找出Perl的完整路径并显式使用它,而无需依赖%PATH%。 Whether you hard-code that or set it in a config is up to you. 您是硬编码还是在配置中设置取决于您。

do "/path/to/test.pl";

or 要么

require "/path/to/test.pl";

will load and evaluate the contents of a file. 将加载并评估文件的内容。

One caveat about require is that evaluating the file has to return "true". 关于require一个警告是评估文件必须返回“ true”。 The usual way to do this is to put a "1;" 通常的做法是将"1;" at the end of your script. 在脚本的末尾。

You need to find where Perl binary lives (on Unix, do which perl , on Windows, find Perl icond and find command line path or find the directory where perl is installed - for example, "c:\\program files\\myPerlDistro\\bin\\perl.exe" 您需要找到Perl二进制文件的存放位置(在Unix上,在Windows上执行which perl ,找到带有图标的Perl并找到命令行路径,或者找到安装perl的目录-例如,“ c:\\ program files \\ myPerlDistro \\ bin \\ perl.exe”

Then you need to either add that full path explicitly to your qx// (don't use system as it loses output) call, or add that path into Apache's PATH variable. 然后,您需要将该完整路径显式添加到您的qx// (不要使用系统,因为它会丢失输出)调用,或者将该路径添加到Apache的PATH变量中。

A second option is to use EmbPerl - it has Execute directive that in-place-executes other scripts and includes their output - in the same interpreter. 第二种选择是使用EmbPerl-它具有Execute指令,该指令可在同一解释器中就地执行其他脚本并包括其输出。 It runs under mod_perl. 它在mod_perl下运行。

Clearly, the problem is that the %PATH% of the account under which httpd is running does not include the path to perl . 显然,问题在于运行httpd的帐户的%PATH%不包含perl的路径。 You can set that up in httpd.conf using PerlSetEnv . 您可以使用PerlSetEnvhttpd.conf进行设置

如果这是Win32(如您的标签所示),您是否不能通过标准Windows东西(例如修改注册表或在资源管理器窗口中转到“工具”>“文件夹选项”>“文件类型”下)将.pl扩展名与Perl关联?

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

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