The problem is that whenever can_read() says there's something to read, perl will (under the hood) read everything into an internal buffer, from which you then get one line at a time via scalar <$reader>. This only matters if there's more than one line being written faster than you're reading on the other end — i.e. in your last case, where you're writing "foo\nbar\n" without a delay in between.

You could have discovered this using strace:

$ strace -etrace=select,read ./754685.pl >/dev/null ... select(16, [6 9], NULL, NULL, {0, 0}) = 1 (in [6], left {0, 0}) read(6, "1\n", 4096) = 2 select(16, [6 9], NULL, NULL, {0, 0}) = 0 (Timeout) select(16, [6 9], NULL, NULL, {0, 0}) = 1 (in [6], left {0, 0}) read(6, "2\n", 4096) = 2 select(16, [6 9], NULL, NULL, {0, 0}) = 0 (Timeout) select(16, [6 9], NULL, NULL, {0, 0}) = 1 (in [6], left {0, 0}) read(6, "3\n", 4096) = 2 select(16, [6 9], NULL, NULL, {0, 0}) = 0 (Timeout) --- SIGCHLD (Child exited) @ 0 (0) --- select(16, [6 9], NULL, NULL, {0, 0}) = 2 (in [6 9], left {0, 0}) read(6, "4\n", 4096) = 2 read(9, "0\n", 4096) = 2 select(16, [6 9], NULL, NULL, {0, 0}) = 0 (Timeout) --- SIGCHLD (Child exited) @ 0 (0) --- select(16, [6 9], NULL, NULL, {0, 0}) = 2 (in [6 9], left {0, 0}) read(6, "foo\nbar\n", 4096) = 8 <- +-- !! read(9, "0\n", 4096) = 2 select(16, [6 9], NULL, NULL, {0, 0}) = 0 (Timeout)

This works fine as long as there's more to read (in which case your loop would take care of reading everything from the internal buffer line by line), but if there's nothing more to read (after the last block (e.g. "foo\nbar\n") has been read under the hood, further select()s will just time out) you'll only read the first line of the block...

One workaround would be to use non-blocking read()s:

pipe $reader, $writer; $reader->blocking(0); ... if (fileno($fh) == fileno($reader)) { read $reader, my $line, 1e5; $output .= $line; } ...

In reply to Re: external command, progress bar and exit value by almut
in thread external command, progress bar and exit value by svenXY

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.