1

I'm writing a Perl wrapper for another application.

I need to pipe STDIN and some STDOUT.

Perl code

#!/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;

Output

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: 

I want to achieve this 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: 

So STDOUT must be filtered and I haven't any idea how do this.

I tried with $stdout =~ s/....//g but it's not working.

Remember that it must send STDIN from parent to child too after filtering STDOUT

Borodin
  • 126,100
  • 9
  • 70
  • 144
J. Doe
  • 71
  • 1
  • 6

1 Answers1

0

You can specify a sub instead of a file handle for stdout, it will get each line of the output as an argument:

#!/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
};
choroba
  • 231,213
  • 25
  • 204
  • 289
  • Its not working correctly. 1. printing output only after i press any key (exit program). 2. dont sending STDIN i think. here my terminal `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` I must press 2 to see output – J. Doe Mar 18 '18 at 14:15
  • @J. Doe, `node` is probably buffering its output. Look at IPC::Run's `>pty>` to try to fool it. – ikegami Mar 19 '18 at 02:26