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

Hi Monks,

I have 3 programs. 1 perl script and a java program which are the same. Another main perl script which uses one of the above. The main perl script is shown below.

open(sense,"java Listen -port /dev/ttyUSB0:115200 |")|| die "Failed to + listen"; while(<sense>) { $currentline= $_; print ("Line $currentline"); }

This works fine. Now the second perl script, I had written on my own. I am printing the data collected over the serial port with

print stdout "$data";
This also works fine standalone. But when I replace the java and put the new script on the main (as shown below), it doesn't work. The output is delayed and comes with around 20 lines of data at once.

open(sense,"./read_serial.pl |")|| die "Failed to listen";
I also tried running
system ("./read_serial &"); open(sense,">- |") || die "Failed to listen";
I am not sure if the last one is correct or not. I think I should be reading the STDIN, but when I put "-" it dies off. Any help is appreciated. Thanks Thanks

Replies are listed 'Best First'.
Re: Pipe Input delayed
by ikegami (Patriarch) on Jun 28, 2011 at 19:29 UTC

    The java programUnlike the java program, read_serial.pl is buffering its output.

    Possible solutions:

    • If it provides such an option, you could configure it so it doesn't buffer its output.

    • You could change it so doesn't buffer its output. (e.g. By adding $|=1;)

    • You should be able to fool it by using a pseudo-tty instead of a pipe. See IPC::Run.

    Update: Ah! Read it backwards! It's when using a Perl child that it didn't work.

      Thanks ikegami. I disabled buffering on my perl script with
      select((select(STDOUT), $| = 1)[0]);

        That doesn't disable buffering, it just enables auto-flush on the selected filehandle.

        Telling Perl not to buffer STDOUT is not useful since the problem is that the java program is buffering its output.

        Update: Ah! Read it backwards! It's when using a Perl child that it didn't work.

Re: Pipe Input delayed
by ReturnOfThelonious (Beadle) on Jun 28, 2011 at 21:00 UTC