in reply to external command, progress bar and exit value
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; } ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: external command, progress bar and exit value
by svenXY (Deacon) on Apr 02, 2009 at 06:34 UTC |