rinceWind has asked for the wisdom of the Perl Monks concerning the following question:

I have a need to run a command in a subprocess, posting a series of data lines, and reading back its stdout.

Reading the friendly manual gives an example with open2, but with some words of warning.

The problem with this is that Unix buffering is really going to ruin your day. Even though your Writer filehandle is auto-flushed, and the process on the other end will get your data in a timely manner, you can't usually do anything to force it to give it back to you in a similarly quick fashion. In this case, we could, because we gave cat a -u flag to make it unbuffered. But very few Unix commands are designed to operate over pipes, so this seldom works unless you yourself wrote the program on the other end of the double-ended pipe.
It then talks about a require module comm.pl, which uses pttys, then going on to state that this will be replaced with IPC::Chat (which does not exist - yet).

Has anyone done this kind of thing? The application I am forking is a third party program, and I have no control of how it buffers its stdout. It also has the nasty that it causes database timeouts and locking problems if the stdout is not read quickly enough.

Any help would be appreciated.

--rW

Replies are listed 'Best First'.
Re: Bi-directional IPC without tears
by synjoo (Initiate) on Aug 09, 2002 at 15:58 UTC
    What type of output is it, just standard to the screen output? You could try piping it to a file and reading that file back with perl doing whatever needs to be done to the data before printing it to the screen. Of course if it needs to be realtime then I think you may need a different solution. Sorry I cant be of more help without specifics
Re: Bi-directional IPC without tears
by rcaputo (Chaplain) on Aug 14, 2002 at 01:59 UTC