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

Oh ... um ... those were like ... other jeffas ... or something. Huh huh huh huh.

Big thanks for the compliments, L~R, and a big thanks to everyone else on this thread for providing great answers and work arounds. I believe i am simply going to remove the wait() call as i really don't need to query the exit status of the exec'ed script after all. I am trying to avoid storing STDOUT and STDERR into scalars as well, hoping to keep them out of memory any more than necessary. Unfortunately, it seems to me that using backticks and shell redirection is the best way to go with this ... i was trying to avoid that, but i am also likely being too pedantic.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
  • Comment on Re^2: Script hangs when called via IPC::Open3

Replies are listed 'Best First'.
Re^3: Script hangs when called via IPC::Open3
by ikegami (Patriarch) on Jun 28, 2006 at 15:49 UTC

    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.)