http://www.perlmonks.org?node_id=393312


in reply to How do you properly tty-ify a non-tty STDOUT?

Just for the record, here's the final code from the actual script it's being used in, which works correctly. This subroutine forks off an external command and returns a filehandle that you can read and write to, and convinces the external program that it's running in a terminal.
use IO::Handle; use IO::Pty; use POSIX; sub forkptycmd($) { my $cmd = shift; my $pty = new IO::Pty; my $pid = fork; if(!defined($pid)) { die "error forking: $!"; } if($pid==0) { POSIX::setsid(); my $slave = $pty->slave; close($pty); STDOUT->fdopen($slave,'>') || die $!; STDIN->fdopen($slave,'<') || die $!; STDERR->fdopen(\*STDOUT,'>') || die $!; close($slave); exec($cmd); } $pty->close_slave(); return $pty; }