【问题标题】:Perl send STDIN and filter STDOUT from external programPerl 发送 STDIN 并从外部程序过滤 STDOUT
【发布时间】:2018-03-18 12:59:29
【问题描述】:

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

【问题讨论】:

    标签: perl perl-ipc-run


    【解决方案1】:

    你可以为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
    };
    

    【讨论】:

    • 它无法正常工作。 1.仅在我按任意键(退出程序)后打印输出。 2. 我认为不要发送 STDIN。这是我的终端 perl "ipc.pl" 2 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 我必须按 2 才能看到输出
    • @J. Doe,node 可能正在缓冲其输出。看看 IPC::Run 的>pty> 试图愚弄它。
    猜你喜欢
    • 2011-08-01
    • 2019-06-18
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多