dongxi4235 2014-07-20 10:09
浏览 65
已采纳

某个类型的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 test.pl:

#!usr/bin/perl
print "This is a perl file";

Example PHP File named test.php:

<?php
$perl=exec('perl test.pl',$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: test2.pl

When command is given in command line as

perl test2.pl -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:

<?php
$perl=exec('perl test2.pl -u argument1 -m argument2 -p testresult',$out,$r);
print_r($out); //Outputs Array (  )
?>

My output is

Array
(
)

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:,

exec
system
shell_exec

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

My objective is to get the output.

edit: Solved. Thanks to hrunting's answer.

  • 写回答

1条回答 默认 最新

  • 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 test.pl 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:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM