Sur123
Sur123

Reputation: 35

Unable to read IPC::Open3 child's output

This code is used to read and write through the pipe, but it seems it is not working well.

use strict;
use IPC::Open2;


my $st1="String1\n";
my $st2="String2\n";
my $st3="String3\n";
my $st4="String4\n";
my $st5="String5\n";

my $joint=$st1.$st2.$st3.$st4.$st5;
my $r;
my $pid = open2(\*CHILD_IN, \*CHILD_OUT, 'java -Dfile.encoding=UTF8 -cp abc.jar:xxx.jar TestCode')
or die "open2() failed $!";
print CHILD_IN $joint;
$r=<CHILD_OUT>;
print "Got $r from child\n";
print "[OUTPUT]: $_" while (<CHILD_OUT>);

This code is only reading first line of the output that too which is stored in $r. Not going inside while loop. Although there is a lot of output by executing the command.

Upvotes: 0

Views: 90

Answers (1)

rubikonx9
rubikonx9

Reputation: 1413

You have a typo.

$r=<CHLD_OUT>;

and

print "[OUTPUT]: $_" while (<CHILD_OUT>);

It's CHLD_OUT versus CHILD_OUT.

It's probably better to always use

use warnings

It'd save you from the trouble, by showing a warning message:

readline() on unopened filehandle CHILD_OUT at x.pl line 19.

Upvotes: 1

Related Questions