It's hard. You need to avoid deadlocks. For example, you could be blocked reading from the child's STDOUT (pipe empty) when the child is blocked sending data to STDERR (pipe full).

You could use some help from the OS.

Unix uses a file-handle based system: IO::Select.

Windows uses a more general and flexible event based system: WaitForMultipleEvents. CreateFile can be told to signal an event when a handle needs servicing.

You could use threads if Perl is built with thread support.

You could make the handles non-blocking and poll them. You could still block writing to the child's STDIN, though.

You could use IPC::Run. IIRC, it has some support for this kind of I/O, but it's inefficient in Windows (since it attempts to model the unix system). It's interface is very simple if you know in advance what you want to send to the child (non-interactive), and IPC::Run3's is even simpler (but that's all it can do).


In reply to Re: Catching STDIN and STDERR on the fly by ikegami
in thread Catching STDIN and STDERR on the fly by szabgab

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.