That probably depends on the external program you are calling.

Libc normally sets up a stream buffered, more precisely line buffered if it's connected to a terminal, full buffered otherwise. This means that unless the program you call will do explicitly otherwise, you'll get the output only after each newline if it's connected to a terminal, but the newline does not flush the buffer if it's connected to a pipe as in your case, so you'll get the output chunk by chunk. Some programs (like cat) are intelligent enought to smartly override this, but some programs just leave it as is (the most annoying one being gnu tr).

If the program you call is a perl script you have written, you can edit it to flush the output whenever you need it to appear (with use IO "Hanlder"; flush STDOUT; on newer perls). If it's a C program you wrote, you can flush the output with fflush(stdout), or force line buffering with setlinebuf(stdout); (I'm not sure in this one). If it's a program you cannot change, you probably can't do many things, except maybe to call if through a virtual terminal, which I don't know how to do from perl, and can have other effects.


In reply to Re^5: how can I redirect the output of a program and get the exit code too? by ambrus
in thread how can I redirect the output of a program and get the exit code too? by vikee

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.