2014-07-20 10:09
浏览 55

某个类型的perl文件在PHP exec函数执行时没有提供输出

My OS is Ubuntu, 14.04, I have lampp. I want to execute a perl file from PHP through my browser. I simply use the exec function (in PHP) to do that and it works. I have seen similar questions in stackoverflow but they aren't related to this.

Example Perl File named

print "This is a perl file";

Example PHP File named test.php:

$perl=exec('perl',$out,$r); //Works successfully
print_r($out); //Outputs Array ( [0]=>This is a perl file )

But I need to execute some other perl file. I can execute that successfully from the command line. Lets assume name of that file is:

When command is given in command line as

perl -u argument1 -m argument2 -p testresult

It takes a fraction of second to execute the above command. I get the output in command line.

But when I execute the same command from PHP as:

$perl=exec('perl -u argument1 -m argument2 -p testresult',$out,$r);
print_r($out); //Outputs Array (  )

My output is


Now I am not getting the output, however the perl file is executing, but I am unable to get the output in $out . I can assure you that the perl file was executed because it also makes some kind of file after execution.

I don't understand why its not giving me the output.

I have also tried following functions in php already:,


None of them is giving me the output, they are working fine for but not for ( and are mentioned above).

My objective is to get the output.

edit: Solved. Thanks to hrunting's answer.

图片转代码服务由CSDN问答提供 功能建议

我的操作系统是Ubuntu,14.04,我有灯泡。 我想通过浏览器从PHP 执行perl文件。 我只是使用exec函数(在PHP中)来做到这一点,它的工作原理。 我在stackoverflow中看到了类似的问题,但它们与此无关。


 #!usr / bin / perl 

示例名为test.php的PHP文件 :

 $ perl = exec('perl',$ out,$ r);  //成功运作
print_r($ out);  //输出数组([0] =&gt;这是一个perl文件)

但是我需要执行一些其他的perl文件。 nI可以从命令行成功执行。 让我们假设该文件的名称是:


   perl -u argument1 -m argument2 -p testresult 

执行上述命令需要几分之一秒。 我得到输出 在命令行中。


 $ perl = exec  ('perl -u argument1 -m argument2 -p testresult',$ out,$ r); 
print_r($ out);  //输出数组()


(\  n)

现在我没有得到输出,但是perl文件正在执行,但是我无法在 $ out中获得输出。 我可以向你保证perl文件已被执行,因为它在执行后也会生成某种文件。

我不明白为什么它没有给我输出。 \ n





编辑:已解决。 感谢hrunting的回答。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doufei8250
    doufei8250 2014-07-20 12:26

    Your second Perl script isn't outputting anything to STDOUT. In your first Perl script, the print statement specifies no output destination, so it will default to STDOUT. In the second Perl script, every print statement either goes to a file or goes to STDERR (with the exception of your --help message). As the PHP exec() function only captures output on STDOUT, you get no output in PHP when you run it, even though you see output when you run it manually.

    You have a few options. Two are presented below:

    1. Redirect STDERR to STDOUT when calling exec()

      `exec('perl 2>&1', $out, $r);`
    2. Write output messages to STDOUT in your Perl script

      If the output is expected, I would change your print STDERR calls to simple print calls.

    There are more options in this Stack Overflow answer:

    点赞 评论