Hi monks,

I want to launch a programm, parse STDOUT and STDERR an react on different outputs. Unfortunately I cannot get rid of buffering. Here is a simple example

Program I want to launch:

#!/usr/bin/perl print "Please press return\n"; my $line = <STDIN>; print "OK, exit now\n";

Main program

#!/usr/bin/perl use strict; use warnings; use IO::Handle; use IPC::Open3; my $fh_out = new IO::Handle; my $fh_in = new IO::Handle; my $fh_err = new IO::Handle; my $pid = open3($fh_in,$fh_out,$fh_err,'./skript_above.pl'); $fh_out->autoflush(1); #Putting these lines above open3 $fh_err->autoflush(1); #doesn't seem to work either while (!$fh_out->eof) { my $line = $fh_out->getline; print "TIME: ".localtime()." OUTPUT: ".$line; if ($line =~ m/^Please press return/ ) { $fh_in->print("\n"); #provide the desired input } } waitpid($pid,0);

In the real program I use IO::Select to read $fh_out and $fh_err and the called program is a binary file (sqlplus)

Problem: The second script justs hangs because the output "Please press return\n" from the first script seems to be buffered. As a result the first script is already waiting for some input but never gets one.

If I change the first skript to

#!/usr/bin/perl use IO::Handle; #new STDOUT->autoflush; #new print "Please press return\n"; my $line = <STDIN>; print "OK, exit now\n";

the second script works as aspected:

TIME: Wed Jan 6 23:03:34 2010 OUTPUT: Please press return TIME: Wed Jan 6 23:03:34 2010 OUTPUT: exit

Now here is my question: Is there a way to prevent buffering without changing the called program? Why is $fh_out->autoflush(1) not working?

-Stefan

In reply to IPC::Open3 buffering and autoflush by morpheus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.