The problem in the end appeared to be that perl expects STDOUT to be a tty (my some_application that I was exec()ing was in fact a perl app ... but I wanted a general solution that didn't need to think about that).

When STDOUT isn't a tty, it turns off line buffering. Hence my requirement to snarf STDOUT in real time was thwarted, until I figured the following out.

This code snipped forks and execs an application, using select to wait for available output on the stderr and stdout for the exec()ed application. The streams are not interleaved.

use IO::Pty; use IO::Select; my $pty = new IO::Pty; pipe($readerr, $writeerr); if (my $pid = fork) { close($writeerr); my $select = new IO::Select; $select->add($pty); $select->add($readerr); while (1) { foreach my $fh ($select->can_read) { my $buf; if (sysread($fh, $buf, 4096)) { print "Read ... $buf ...\n"; } } } } else { close($readerr); $pty->make_slave_controlling_terminal(); my $slave = $pty->slave(); close $pty; $slave->clone_winsize_from(\*STDIN); $slave->set_raw(); open(STDOUT, ">&" . $slave->fileno); open(STDERR, ">&" . $writeerr->fileno); close($slave); exec("/home/hagus/foo.pl"); }
--
Ash OS durbatulk, ash OS gimbatul,
Ash OS thrakatulk, agh burzum-ishi krimpatul!
Uzg-Microsoft-ishi amal fauthut burguuli.

In reply to SOLUTION by hagus
in thread Pipe, fork, exec and red-hot pokers. by hagus

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.