in reply to How to change Open2 input buffering

You need to use a pseudo-tty to solve this problem. IO::Pty is a fairly easy way to solve this. Here's an example:
#!/usr/bin/perl -w use strict; use IO::Pty; my $pty = IO::Pty->new or die "Pty error: $!\n"; my $slave = $pty->slave or die "Pty slave error: $!\n"; # Run cat with stdout on the PTY. pipe(PIPEREAD,PIPEWRITE); if (fork()==0) { # Child close(PIPEWRITE); close($slave); close(STDOUT) or die "Couldn't close STDOUT: $!\n"; open(STDOUT,">&".$pty->fileno) or die "Couldn't re-open STDOUT: $!\n"; close(STDIN) or die "Couldn't close STDIN: $!\n"; open(STDIN,"<&PIPEREAD") or die "Couldn't re-open STDIN: $!\n"; exec("/bin/cat") or die "Couldn't exec /bin/cat: $!\n"; } # Parent close(PIPEREAD); close($pty); select(PIPEWRITE); $|=1; select(STDOUT); warn "Parent running\n"; foreach my $val (1..10) { print PIPEWRITE "$val\n"; $_ = <$slave>; print "$_"; }

Replies are listed 'Best First'.
Re: Re: How to change Open2 input buffering
by thecap (Initiate) on Oct 19, 2003 at 19:45 UTC
    Thank you tye, ptkdb, and sgifford for your excellent replies.

    The pseudo-tty code sgifford posted worked wonderfully in Linux, but not when I moved to my final target, SunOS. I eventually found a small C program called ircflush, part of the ircII distribution that does the same thing and seems to work in SunOS. I also looked into using IPC::Run but it does not connect file handles to pseudo-ttys.

      Huh. What version of SunOS?
        I tested your code again on SunOS and it worked. Since I don't admin the machines I'm not sure if the system has been changed since I first claimed it didn't work. Perhaps I had run into a more complex problem with IO::Pty but don't feel the need to try to reproduce it any longer.