将 Perl 输出返回到 PHP [英] Return Perl-output to PHP

查看:26
本文介绍了将 Perl 输出返回到 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 perl 脚本的输出返回到网页.但是它只返回最后一行.

I want to return the output of a perl script to a webpage. However it only returns the last line.

Perl 脚本:

my $directory = $ARGV[0];
opendir(DIR,$directory);
my @files = grep {/.txt$/ } readdir(DIR);
closedir(DIR);
foreach(@files) {
    print $_."
";
}

PHP 代码:

$perl_result = exec("perl $script_folder $project_folder");
*some code*
<?php print $perl_result; ?>

预期输出(以及脚本在 Linux 命令行中返回的内容):

Expected output (and what the script returns in a Linux command line):

test.txt
test2.txt
test3.txt

PHP 返回的内容:

test3.txt

我必须在我的代码中进行哪些更改才能让 PHP 显示所有行?

What do I have to change in my code to get PHP to show all lines?

谢谢

推荐答案

引用 PHPexec() 的手册页:

Quoting from the PHP manual page for exec():

返回值

命令结果的最后一行.如果您需要执行命令并将命令中的所有数据直接传回而不受任何干扰,请使用 passthru() 函数.

The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.

要获取执行命令的输出,请务必设置并使用output参数.

To get the output of the executed command, be sure to set and use the output parameter.

所以一个建议是停止使用 exec() 并开始使用 passthru() .然而,这是无稽之谈.passthru() 实际上并不返回任何东西.如果 all 您需要使用 $perl_result 将其打印到浏览器就足够了,因此不需要将输出存储在变量中全部.但是,如果您需要匹配输出,或以任何方式对其进行操作,则不需要 passthru().

So one suggestion is stop using exec() and start using passthru() . However, that's nonsense. passthru() doesn't actually return anything. It may be sufficient if all you need to do with $perl_result is print it to the browser, and thus don't really need the output to be stored in a variable at all. But if you need to match against the output, or manipulate it in any way, you don't want passthru().

改为尝试反引号运算符:

<?php
$perl_result = `perl $script_folder $project_folder`;

或者尝试将 exec() 的第二个参数设置为一个空数组:

Or try setting the second argument of exec() to an empty array:

<?php
$perl_result = array();
exec("perl $script_folder $project_folder", $perl_result);

$perl_result = implode("
", $perl_result);  # array --> string

这篇关于将 Perl 输出返回到 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆