morpheus has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: IPC::Open3 buffering and autoflush
by chromatic (Archbishop) on Jan 06, 2010 at 22:24 UTC | |
|
Re: IPC::Open3 buffering and autoflush
by almut (Canon) on Jan 07, 2010 at 02:37 UTC | |
|
Re: IPC::Open3 buffering and autoflush
by urthwrm (Novice) on Jan 07, 2010 at 04:15 UTC | |
by morpheus (Initiate) on Jan 07, 2010 at 13:55 UTC | |
|
Re: IPC::Open3 buffering and autoflush
by zentara (Cardinal) on Jan 07, 2010 at 12:21 UTC | |
|
Re: IPC::Open3 buffering and autoflush
by puravida (Initiate) on Jul 03, 2014 at 23:19 UTC |