本文介绍了的Perl / embperl - IPC :: Open3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在2格式的perl和放大器的样本程序; embperl

Perl的版本可以作为一个CGI但embperl版本无法正常工作。

任何建议或指针的解决方案将是AP preciated

操作系统:Linux版本2.6.35.6-48.fc14.i686.PAE(...)(GCC 4.5.1版20100924(红帽4.5.1-4)(GCC))#1 SMP周五15年10月22日:27:53 UTC 2010

请注意:我最初发布这个问题perlmonks 和embperl邮件列表但没有得到解决。

的Perl脚本工作

 #!的/ usr / bin中/ perl的
使用警告;
使用严格的;
使用IPC :: Open3;打印内容类型:文本/纯\\ n \\ n;我的$ CMD ='ls'的;我的$ PID = open3(* HIS_IN,* HIS_OUT,* HIS_ERR,$ CMD);
关闭(HIS_IN); #给文件的结束小子,或喂他
我@outlines =< HIS_OUT取代; #读取直到EOF
我@errlines =< HIS_ERR取代; #XXX:如果块状潜力
打印标准输出,@outlines,\\ n;
打印STDERR:@errlines,\\ n;waitpid函数($ PID,0);
我的$ child_exit_status = $? >> 8;打印child_exit_status:$ child_exit_status \\ N的;

embperl非工作脚本

  [ - 
  使用警告;
  使用严格的;
  使用IPC :: Open3;  我的$ CMD ='ls'的;  我的$ PID = open3(* HIS_IN,* HIS_OUT,* HIS_ERR,$ CMD);  关闭(HIS_IN); #给文件的结束小子,或喂他  我@outlines =< HIS_OUT取代; #读取直到EOF
  我@errlines =< HIS_ERR取代; #XXX:如果块状潜力
  打印出标准输出,@outlines,\\ n;
  打印出STDERR:@errlines,\\ n;  waitpid函数($ PID,0);
  我的$ child_exit_status = $? >> 8;  打印出child_exit_status:$ child_exit_status \\ N的;
- ]

这是我收到的输出

  STDERR:LS:写入错误:错误的文件描述符child_exit_status:2


解决方案

open3 重定向与STDOUT相关的文件描述符,除它被的fd 1 (什么程序你 EXEC 将考虑STDOUT)。但它不是 1 。它甚至不具有与它相关的文件描述符!我认为这是 open3 的错误。我想你可以解决它,如下所示:

 本地* STDOUT;
打开(STDOUT,'>&安培; =',1)或死亡$ !;
... ... open3

I have a sample program in 2 formats perl & embperl

The perl version works as a CGI but the embperl version does not work.

Any suggestions or pointers to solutions would be appreciated

OS: Linux version 2.6.35.6-48.fc14.i686.PAE (...) (gcc version 4.5.1 20100924 (Red Hat 4.5.1-4) (GCC) ) #1 SMP Fri Oct 22 15:27:53 UTC 2010

NOTE: I originally posted this question to perlmonks [x] and the embperl mailing list [x] but didn't get a solution.

perl working script

#!/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 non-working script

[-
  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";
-]

Here is the output I receive

STDERR: ls: write error: Bad file descriptor

child_exit_status: 2
解决方案

open3 redirects the file descriptor associated with STDOUT, excepting it to be fd 1 (what the program you exec will consider STDOUT). But it's not 1. It doesn't even have a file descriptor associated with it! I consider this a bug in open3. I think you can work around it as follows:

local *STDOUT;
open(STDOUT, '>&=', 1) or die $!;
...open3...

这篇关于的Perl / embperl - IPC :: Open3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 09:55