List perl如何将列表表单系统调用的STDOUT捕获为文件句柄

List perl如何将列表表单系统调用的STDOUT捕获为文件句柄,list,perl,filehandle,List,Perl,Filehandle,以下原型演示了如何将由boss脚本调用的worker脚本的STDOUT捕获为FILEHANDLE,并将FILEHANDLE的内容转换为可以在boss脚本中操作的数组。boss脚本junk.pl: 如果$worker不需要元素包含空格的数组作为参数,则可以使用文件句柄的open和definition。但是,如果需要这样一个复杂的参数,则必须以列表形式调用worker脚本,就像在boss脚本的底部一样。有关列表形式,请参阅 在这种情况下,如何将STDOUT捕获为文件句柄?如何修改boss脚本的底线来

以下原型演示了如何将由boss脚本调用的worker脚本的STDOUT捕获为FILEHANDLE,并将FILEHANDLE的内容转换为可以在boss脚本中操作的数组。boss脚本junk.pl:

如果$worker不需要元素包含空格的数组作为参数,则可以使用文件句柄的open和definition。但是,如果需要这样一个复杂的参数,则必须以列表形式调用worker脚本,就像在boss脚本的底部一样。有关列表形式,请参阅


在这种情况下,如何将STDOUT捕获为文件句柄?如何修改boss脚本的底线来实现这一点?

有一种更简单的方法,就是使用这样的简单代码

use strict;
use warnings;
use IPC::Run qw(run);
my $in = ""; # some data you want to send to the sub-script, keep it empty if there is none
# the data will be received from the STDIN filehandle in the called program
my $out = ""; # variable that will hold data writed to STDOUT
my $err;
run ["perl", "junk2.pl"], \$in, \$out, \$err;

有一种更简单的方法,就是使用这样一个简单的代码

use strict;
use warnings;
use IPC::Run qw(run);
my $in = ""; # some data you want to send to the sub-script, keep it empty if there is none
# the data will be received from the STDIN filehandle in the called program
my $out = ""; # variable that will hold data writed to STDOUT
my $err;
run ["perl", "junk2.pl"], \$in, \$out, \$err;
您可以按如下方式继续使用open:

open(my $child, "-|", $prog, @args)
   or die("Can't launch \"$prog\": $!\n");

my @lines = <$child>;

close($child);
die("$prog killed by signal ".( $? & 0x7F )."\n") if $? & 0x7F;
die("$prog exited with error ".( $? >> 8 )."\n") if $ ?>> 8;
还有一个提供了一个用于捕获STDOUT和STDERR的极简界面

请注意,您可以使用的shell_quote为sh生成命令

您可以按如下方式继续使用open:

open(my $child, "-|", $prog, @args)
   or die("Can't launch \"$prog\": $!\n");

my @lines = <$child>;

close($child);
die("$prog killed by signal ".( $? & 0x7F )."\n") if $? & 0x7F;
die("$prog exited with error ".( $? >> 8 )."\n") if $ ?>> 8;
还有一个提供了一个用于捕获STDOUT和STDERR的极简界面

请注意,您可以使用的shell_quote为sh生成命令

use IPC::Run qw( run );

run [ $prog, @args ],
   '>', \my $out;

die("$prog killed by signal ".( $? & 0x7F )."\n") if $? & 0x7F;
die("$prog exited with error ".( $? >> 8 )."\n") if $ ?>> 8;

my @lines = split(/^/m, $out);
use String::ShellQuote qw( shell_quote );

my $cmd = shell_quote("junk2.pl", @array);