1

我正在为另一个应用程序编写 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: 

我想实现这个标准输出:

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

4

1 回答 1

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
};
于 2018-03-18T13:38:43.733 回答