in reply to Re: [IO::Pty] How to read from and write to slave?
in thread [IO::Pty] How to read from and write to slave?

Thanks Roland for the hint. I did further test and found that the sleep place at the end of slave process does not matter, it has to be placed precisely before master talks each time in terms of time. It appears as if the master and slave cannot talk at the same time, or some kind of chaos will happen. This is demonstrated with the code below, and commented therein. In this simple example, we know when the master talks and we can place a sleep there. But in real situation, we do not know when the master talks, and how long it will talk, we have to have a general mechanism of control. I thought the control is provided by IO::Pty, or otherwise why we should have a pair of master and slave terminals. If we have to provide the control, then we can just use master or slave, no? Thanks a lot.
#!/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; chomp $line; print "TIME: " . localtime() . " OUTPUT: " . $line . "\n"; if ( $line == 3 or $line == 7 ) { my $j = $line++; $master->print("$j\n"); } } wait(); } else { $master->close(); $master->make_slave_controlling_terminal(); $slave->set_raw(); $slave->print("1\n"); $slave->print("3\n"); sleep 1; $slave->print("5\n"); $slave->print("7\n"); sleep 2; $slave->print("9\n"); $slave->print("11\n"); # sleep 3; } __END__ (1) with $master->print(), and without sleeps: $master->print("$j\n"); # sleep 1; # sleep 2; # sleep 3; We have the output: TIME: Wed Jul 9 11:27:53 2014 OUTPUT: 1 TIME: Wed Jul 9 11:27:53 2014 OUTPUT: 3 (2) Without $master->print(), and without sleeps: # $master->print("$j\n"); # sleep 1; # sleep 2; # sleep 3; We have the output: TIME: Wed Jul 9 11:31:42 2014 OUTPUT: 1 TIME: Wed Jul 9 11:31:42 2014 OUTPUT: 3 TIME: Wed Jul 9 11:31:42 2014 OUTPUT: 5 TIME: Wed Jul 9 11:31:42 2014 OUTPUT: 7 TIME: Wed Jul 9 11:31:42 2014 OUTPUT: 9 TIME: Wed Jul 9 11:31:42 2014 OUTPUT: 11 (3) "sleep 3" at the end makes no difference in terms of output. (4) with $master->print(), and with "sleep 1": $master->print("$j\n"); sleep 1; # sleep 2; # sleep 3; We have the output: TIME: Wed Jul 9 11:41:05 2014 OUTPUT: 1 TIME: Wed Jul 9 11:41:05 2014 OUTPUT: 3 TIME: Wed Jul 9 11:41:06 2014 OUTPUT: 5 TIME: Wed Jul 9 11:41:06 2014 OUTPUT: 7 (5) with $master->print(), and with "sleep 2": $master->print("$j\n"); # sleep 1; sleep 2; # sleep 3; TIME: Wed Jul 9 11:42:52 2014 OUTPUT: 1 TIME: Wed Jul 9 11:42:52 2014 OUTPUT: 3 TIME: Wed Jul 9 11:42:54 2014 OUTPUT: 9 TIME: Wed Jul 9 11:42:54 2014 OUTPUT: 11 (6) with $master->print(), and with both "sleep 1" and "sleep 2": $master->print("$j\n"); sleep 1; sleep 2; # sleep 3; TIME: Wed Jul 9 11:43:31 2014 OUTPUT: 1 TIME: Wed Jul 9 11:43:31 2014 OUTPUT: 3 TIME: Wed Jul 9 11:43:32 2014 OUTPUT: 5 TIME: Wed Jul 9 11:43:32 2014 OUTPUT: 7 TIME: Wed Jul 9 11:43:34 2014 OUTPUT: 9 TIME: Wed Jul 9 11:43:34 2014 OUTPUT: 11 Conclusion: When master talks, the slave has to wait, otherwise what is written to slave is lost.