Perl从外部程序发送STDIN并过滤STDOUT

时间:2018-03-18 12:59:29

标签: perl perl-ipc-run

我正在为另一个应用程序编写一个Perl包装器。

我需要管道STDIN和一些STDOUT。

Perl代码

#!/usr/bin/perl -w

use strict;

use IPC::Run3 qw(run3);

my $stdout;

local $| = 1;

run3 ['node','gekko',"-b","-c","BNB-XLM-Doktor_v1-5-144-config.js"],  undef, $stdout;

输出

2018-03-18 13:50:10 (DEBUG):    Available 142534
2018-03-18 13:50:10 (DEBUG):    Optimal 144240
2018-03-18 13:50:10 (INFO): The database has 1707 candles missing, Figuring out which ones...
2018-03-18 13:50:11 (INFO): Gekko detected multiple dateranges in the locally stored history. Please pick the daterange you are interested in testing:
2018-03-18 13:50:11 (INFO):          OPTION 1:
2018-03-18 13:50:11 (INFO):      from: 2017-12-08 07:04:00
2018-03-18 13:50:11 (INFO):      to: 2018-03-14 19:04:00
2018-03-18 13:50:11 (INFO):          OPTION 2:
2018-03-18 13:50:11 (INFO):      from: 2018-03-16 00:04:00
2018-03-18 13:50:11 (INFO):      to: 2018-03-18 10:04:00
prompt: option: 

我想实现这个STDOUT:

OPTION 1:
from: 2017-12-08 07:04:00
to: 2018-03-14 19:04:00
OPTION 2:
from: 2018-03-16 00:04:00
to: 2018-03-18 10:04:00
prompt: option: 

所以必须过滤STDOUT,我不知道怎么做。

我尝试使用$stdout =~ s/....//g,但它无效。

请记住,在过滤STDOUT

之后,它也必须从父级发送STDIN到子级

1 个答案:

答案 0 :(得分:0)

您可以为stdout指定sub而不是文件句柄,它会将输出的每一行作为参数:

#!/usr/bin/perl
use warnings;
use strict;

use IPC::Run3 qw(run3);

my $stdout;
local $| = 1;
run3 ['command'], undef, sub {
    $_ = shift;
    return unless /(?:OPTION \d+|from|to|option):\s+/;

    s/.*INFO\):\s+//;
    print
};