Re: piping from external program not working..




On Friday, June 4, 2004, at 04:14 AM, Syed Imran wrote:

It shows only the first line of output produced by the external program. I am having difficulty locating the problem. I've added the code to this mail.

your test program exits almost immediately after printing a couple of lines of output. the watch handler reads one line of input at a time.

fork child.
install handler.
child prints three lines of output and exits.
your io watch fires with the condition [ 'in', 'hup' ]
you read and process one line of input.
you close the handle.


this is something of a race condition. i could make your code print all the lines that come back by adding some pauses after each line of input. in a Real Program, you wouldn't be able to do that, and you'd want to read everything that comes back.

there are two ways to slurp in all data in Perl (of which *i* know -- there may be others. all you mongers listening, please feel free to correct me):

1) set $/ to undef, to read till end of file. this is undesirable, because your watch handler will hang (since the stream is not yet finished).

2) find out how many bytes are actually waiting, and read exactly that many. this requires the use of ioctl and sysread, and is immediately non-portable. since you're using fdisk, you probably don't care about that.


i got option 2 to work by replacing these lines:

                my $data = <$fh>;

with these:

                #require "sys/ioctl.ph";
                # my perl headers appear to be broken, so here it is, hardcoded.
                sub FIONREAD { 0x541B }
                my $count;
                my $ret = ioctl $fh, &FIONREAD, $count;
                $count = unpack "i", $count;
                my $data;
                sysread $fh, $data, $count;


this no longer works by lines, but that's ok, because the data on the input handle may include incomplete lines, anyway.

--
"that's it! you're a genius!" "yes. that's what i think. do you think i deserve a raise?"
        - dialogue from 'Godzilla versus Mothra', 1964




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]