in reply to Setting up 2 way pipe for a command

When it's up to me to implement this, I almost always reach for IPC::Run. The documentation is a bit unwieldy, but once you work through it the implementation is about as straightforward as it can be.
  • Comment on Re: Setting up 2 way pipe for a command

Replies are listed 'Best First'.
Re^2: Setting up 2 way pipe for a command
by gw1500se (Beadle) on Jun 30, 2008 at 22:32 UTC
    Thanks for the suggestion. I may have to switch to IPC::Run as I cannot get open2 to work. It seems to block when I try to read the output. I tried setting autoflush for the output but it doesn't seem to help. I should make it clear that I am trying to make this interactive so I need the output for each input (if there is one) while the command is running. I believe there is a 1:1 correspondence between a line into the command and a line out. The line in does what it is supposed to but at that point the script hangs waiting for a line on the output side so the next input line never goes. However, it is possible that there is not a 1:1 correspondence but until I can manage the blocking I won't really know. It appears like pump might do what I need but I have to read up on it more and see some examples.
Re^2: Setting up 2 way pipe for a command
by gw1500se (Beadle) on Jul 02, 2008 at 22:55 UTC
    I switched to IPC::Run but has not faired any better. I keep getting a premature end error even though there is input data. Here is the relevent code segment I have now after many various iterations:
    my $h=start(\@cmd,\$in,\$out,\$err); while (!eof(FILELIST)) { $thisline=readline(FILELIST); ($thisfile,$thisfilesize)=split(/\t/,$thisline +); $in="$thisfile\n"; $h->pump; while ($h->pumpable) { $h->pump; } if (length($out)>0) { putmsg($out); } $total+=$thisfilesize; $complete=sprintf("%.2f",$total/$dumpsize); putmsg("$complete%\n"); } finish $h;
    In one of my iterations I avoided the premature end message however, the script simply quits on the first call of 'pump'. No error message, nothing. Does anyone one see anything obvious? Thanks.
      Uh... "premature end error"? As in HTTP server error response? If this is indeed the case, try running the thing from the command line, or at least printing a text/plain header somewhere ahead of the block above.

      Maybe on that iteration that didn't puke, the application being run via IPC::Run didn't output anything?

        Thanks for the reply. Its the "process ended prematurely" error. Sorry I didn't take the time to look up the error rather than go from poor memory. Perhaps I am misunderstanding how this works but there indeed may not be any output on the first call to 'pump'. However, there is definitely input. I then try to drain any output in the 'pumpable' loop before going on to the next input string. Therefore the way I think it works this condition should never happen unless there is a bug in the 'pumpable' function. If I am misunderstanding, please correct me as that would be the solution to my problem.