2

我有 2 种格式 perl 和 embperl 的示例程序

perl 版本作为 CGI 工作,但 embperl 版本不工作。

任何建议或解决方案的指针将不胜感激

操作系统:Linux 版本 2.6.35.6-48.fc14.i686.PAE (...) (gcc 版本 4.5.1 20100924 (Red Hat 4.5.1-4) (GCC)) #1 SMP Fri Oct 22 15:27: 53 世界标准时间 2010

注意:我最初将此问题发布到 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";

emberl 非工作脚本

[-
  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
4

2 回答 2

1

open3重定向与 STDOUT 关联的文件描述符,但它是 fd 1(您exec将考虑为 STDOUT 的程序)。但事实并非如此1。它甚至没有与之关联的文件描述符!我认为这是open3. 我认为您可以按如下方式解决它:

local *STDOUT;
open(STDOUT, '>&=', 1) or die $!;
...open3...
于 2014-05-21T23:42:50.907 回答
0

非常感谢ikegami!!!!

这是有效的embperl代码。PS 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";
-]
于 2014-05-22T21:24:23.653 回答