in reply to IPC::Open3 buffering and autoflush
As many has pointed out IO::Pty is the right module to use. The buffer comes from the external program, not the IPC::Open3. Posted below is the complete program to demonstrate how this is done with IO::Pty.
#!/usr/bin/env perl -w use strict; use IO::Pty; my $master = new IO::Pty; my $slave = $master->slave(); my $pid = fork(); die "Couldn't fork: $!" unless defined $pid; if ($pid) { $master->close_slave(); while ( !$master->eof ) { my $line = $master->getline; print "TIME: " . localtime() . " OUTPUT: " . $line; if ( $line =~ m/^Please press return/ ) { $master->print("abc\n"); } } wait(); exit $? >> 8; } else { $master->close(); $master->make_slave_controlling_terminal(); open( STDIN, ">&", $slave ) or die "Couldn't dup stdin: $!"; open( STDOUT, ">&", $slave ) or die "Couldn't dup stdout: $!"; open( STDERR, ">&", $slave ) or die "Couldn't dup stderr: $!"; # External program with buffered IO. system q( perl -e ' print "Please press return\n"; my $line = <STDIN>; print "OK, $line, exit now\n"; exit 14; ' ); }
If you run the program, you will get this:
TIME: Thu Jul 3 19:30:47 2014 OUTPUT: Please press return TIME: Thu Jul 3 19:30:47 2014 OUTPUT: abc TIME: Thu Jul 3 19:30:47 2014 OUTPUT: OK, abc TIME: Thu Jul 3 19:30:47 2014 OUTPUT: , exit now
The second line comes from echo to the $master, how do I turn off echo?
|
|---|