我有一个2种格式的示例程序perl & embperl
perl版本作为CGI工作,但embperl版本不起作用。
如有任何解决方案的建议或建议,将不胜感激。
操作系统: Linux版本2.6.35.6-48.fc14.i686.PAE (.)(gcc版4.5.1 20100924 (红帽4.5.1-4) (GCC) ) #1 SMP星期五2010年10月22日15:27:53
注意:我最初把这个问题发到perlmonks [x]和embperl邮件列表[x]上,但没有得到解决方案。
perl工作脚本
#!/usr/bin/perl
use warnings;
use strict;
use IPC::Open3;
print "Content-type: text/plain\n\n";
my $cmd = 'ls';
my $pid = open3(*HIS_IN, *HIS_OUT, *HIS_ERR, $cmd);
close(HIS_IN); # give end of file to kid, or feed him
my @outlines = <HIS_OUT>; # read till EOF
my @errlines = <HIS_ERR>; # XXX: block potential if massive
print "STDOUT: ", @outlines, "\n";
print "STDERR: ", @errlines, "\n";
waitpid( $pid, 0 );
my $child_exit_status = $? >> 8;
print "child_exit_status: $child_exit_status\n";embperl非工作脚本
[-
use warnings;
use strict;
use IPC::Open3;
my $cmd = 'ls';
my $pid = open3(*HIS_IN, *HIS_OUT, *HIS_ERR, $cmd);
close(HIS_IN); # give end of file to kid, or feed him
my @outlines = <HIS_OUT>; # read till EOF
my @errlines = <HIS_ERR>; # XXX: block potential if massive
print OUT "STDOUT: ", @outlines, "\n";
print OUT "STDERR: ", @errlines, "\n";
waitpid( $pid, 0 );
my $child_exit_status = $? >> 8;
print OUT "child_exit_status: $child_exit_status\n";
-]这里是我收到的输出
STDERR: ls: write error: Bad file descriptor
child_exit_status: 2发布于 2014-05-21 23:42:50
open3重定向与STDOUT相关的文件描述符,但不包括fd 1 (您的exec将考虑STDOUT的程序)。但这不是1。它甚至没有与它相关的文件描述符!我认为这是open3中的一个bug。我认为你可以通过以下方式来解决这个问题:
local *STDOUT;
open(STDOUT, '>&=', 1) or die $!;
...open3...发布于 2014-05-22 21:24:23
非常感谢池格米!
下面是工作的embperl代码。STDIN也有类似的问题。我还不知道解决这个问题的办法,但我认为这是相似的。
[-
use warnings;
use strict;
use IPC::Open3;
use POSIX;
$http_headers_out{'Content-Type'} = "text/plain";
my $cmd = 'ls';
open(my $fh, '>', '/dev/null') or die $!;
dup2(fileno($fh), 1) or die $! if fileno($fh) != 1;
local *STDOUT;
open(STDOUT, '>&=', 1) or die $!;
my $pid = open3(*HIS_IN, *HIS_OUT, *HIS_ERR, $cmd);
close(HIS_IN); # give end of file to kid, or feed him
my @outlines = <HIS_OUT>; # read till EOF
my @errlines = <HIS_ERR>; # XXX: block potential if massive
print OUT "STDOUT: ", @outlines, "\n";
print OUT "STDERR: ", @errlines, "\n";
waitpid( $pid, 0 );
my $child_exit_status = $? >> 8;
print OUT "child_exit_status: $child_exit_status\n";
-]https://stackoverflow.com/questions/23770338
复制相似问题