in reply to Re^2: Script hangs when called via IPC::Open3
in thread Script hangs when called via IPC::Open3

Moving the wait is not enough

+------------------------+----------------------+ | Parent | Child | +------------------------+----------------------+ | - open3(...); | | | - <OUT> | - ... | | | - Waits for child to | - print STDERR 1027; | | | write to STDOUT or | - print STDERR 1028; | | | for child to end. | - print STDERR 1029; | T | | - print STDERR 1030; | i | | - print STDERR 1031; | m | | - Pipe full. | e | | Waits for parent | | | | to empty it. | | | | | v | Still waiting... | Still waiting... | +------------------------+----------------------+

With my snippet below, you don't have to store the stuff in memory. You can do whatever you want to with it when you read it it. For example, the following prints it out a line at a time, prefixed with the handle name:

my %lookup = ( \*OUT => [ \*OUT, 'STDOUT', '' ], \*ERR => [ \*ERR, 'STDERR', '' ], ); MAIN_LOOP: for (;;) { my @r = $r_sel->can_read(); foreach my $fh (@r) { last MAIN_LOOP if eof($fh); my $buf_ptr = $lookup{$fh}[2]; 1 while read($fh, $$buf_ptr, $BLOCK_SIZE, length($$buf_ptr)); print("$lookup{$fh}[1]: $1") while $$buf_ptr =~ s/^(.*\n)//; } } foreach my $fh (keys %lookup) { print("$lookup{$fh}[1]: $lookup{$fh}[2]\n") if length $lookup{$fh}[2]; }

Alternatively, you could use IPC::Run. Instead of a scalar, you can pass the address of a callback to be called when text is received from the child. (Search for \&out.)