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

The problem with your second example seems to be that the child process (running the while) still has the slave file descriptor opened. Closing the slave file descriptor it in the child process fixes it for me; I changed return $pty if $child; to:
if ($child) { $pty->close_slave(); return $pty; }
See this note in the IO::Pty documentation:
close_slave()
The slave filehandle will be closed and destroyed. This is necessary in the parent after forking to get rid of the open filehandle, otherwise the parent will not notice if the child exits. Subsequent calls of slave() will return a newly opened slave filehandle.

Also, it's customary to have the child start up a new process and have the parent continue the main flow of execution; your way will work, but isn't the normal way of doing things. And if you're using system(...); exit(0); in your actual program (and not just this sample code), consider using exec instead.

Update: Yup, I misread ph713's code; it forks in a perfectly normal way.

Replies are listed 'Best First'.
Re^2: How do you properly tty-ify a non-tty STDOUT?
by ph713 (Pilgrim) on Sep 22, 2004 at 17:47 UTC
    AHA! Thanks, you're right, the close_slave() thing was what I was missing. And no, the sample code looks nothing like the real script, this was just an isolated chunk of test code to work with this problem and make posting here easier :) Thanks again!
Re^2: How do you properly tty-ify a non-tty STDOUT?
by ph713 (Pilgrim) on Sep 22, 2004 at 17:54 UTC
    By the way (and I could be confused, my brain has been dragged through the mud today a few times) - the forking sample code actually does have the parent continuing the flow of execution, with the child doing the system();exit();. The return $pty if $child; line looks in english like it's the other way around, but the parent gets $child defined as a pid number, and the actual child gets $child returned as zero. Therefore the idiom $child = fork; XXX if $child; is very ugly, as the perl meaning is the opposite of the english meaning. Don't blame me, I stole it from an example in a web copy of Chapter 6 of "Network programming with Perl". :)