Please look at the code below:

#!/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 ) { $master->print("2\n"); $master->print("4\n"); $master->print("6\n"); } } wait(); } else { $master->close(); $master->make_slave_controlling_terminal(); $slave->print("1\n"); $slave->print("3\n"); $slave->print("5\n"); }

and the result is:

$ perl  realtime5d.pl
TIME: Thu Jul  3 21:05:10 2014 OUTPUT: 1
TIME: Thu Jul  3 21:05:10 2014 OUTPUT: 3
TIME: Thu Jul  3 21:05:10 2014 OUTPUT: 2
TIME: Thu Jul  3 21:05:10 2014 OUTPUT: 4
TIME: Thu Jul  3 21:05:10 2014 OUTPUT: 6

Question 1: I expect 1, 3, 2, 4, 6, 5, why 5 written to $slave is lost? If you look at the picture from here: http://en.wikipedia.org/wiki/Pseudo_terminal, the traffic is bidirectional from master <=> slave, no?

Question 2: How do I turn off the echo? i.e. I do not want to read what I wrote myself, 2, 4, 6 in this case.

Question 3: Above 5 line output was from Perl 5.16 on Mac OS X 10.9.3. I did not think it is OS dependent for this simple script, but when I run with Perl 5.8 on Linux kernel 2.6, I got the following:

$ perl realtime5d.pl 
TIME: Fri Jul  4 10:08:50 2014 OUTPUT: 1
TIME: Fri Jul  4 10:08:50 2014 OUTPUT: 3

It looks that the echo is turned off, but once I write to the master, the is no more reading from slave. Why?

Question 4: The other very similar script http://www.perlmonks.org/?node_id=1092227 produces the same behavior on Perl 5.16 on Mac OS X 10.9.3 and on Perl 5.8 on Linux kernel 2.6, what is the real difference that these two scripts that causes the different behavior?

Thanks.


In reply to [IO::Pty] How to read from and write to slave? by puravida

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.