I'm having some interesting difficulties with those four little items.

My aim is to capture the stdout and stderr from an exec()ed program, but not just interleaved into one stream. So I'm doing something like:

pipe READOUT, WRITEOUT; if (my $pid = fork) { close(WRITEOUT); # try to read from READOUT, which fails. } else { close(STDERR); close(STDOUT); open(STDOUT, ">&WRITEOUT"); # This next line makes no difference. select(STDOUT); $|=1; exec("some_application"); }
The problem is that if some_application buffers stdout, I don't seem to get *anything* back on READOUT. I only get stuff back when I print heaps (ie. exceeding the 4096 buffer I guess). Newlines don't force a buffer flush. If I set $|=1 in some_application, everything is fine!

This is no good though, because I might not have access to the source if some_application is not a perl app. And I don't want to have to modify things like that. So what has failed here?

I've spent way too much time on this - but it's bugging me. It's as if I need a way to force some_application to not set its stdout to buffered mode, or in the parent block above, somehow post exec() set some_application's stdout to non-buffered.

Any hints?

--
Ash OS durbatulk, ash OS gimbatul,
Ash OS thrakatulk, agh burzum-ishi krimpatul!
Uzg-Microsoft-ishi amal fauthut burguuli.

Update 2002-04-08 by mirod: changed <pre> tags to <code> tags


In reply to 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.