http://www.perlmonks.org?node_id=857561

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there!

So I need to read the output of a command that outputs a lot of data... but I don't need all the data. I want to be able to stop after some point.

For just a single pipe to my script this works OK:

$pid = open (INPUT, "mycommand $somefile |") or die ...; while (<INPUT>) { if ( ... trigger to stop ... ) { kill 9, $pid; last; } } close INPUT;

At the point the kill is issued, mycommand is terminated, life is good and we are ready to move on and process the next file.

... by the way, I'm on Windows XP, ActiveState Perl 5.8.8.

I'm not even sure what signal I should be sending with the kill (I tried 1, 2 and 9... 9 worked so that's what I am using).

What I want to do is:

$pid = open (INPUT, "gzip -dc $somegzfile | mycommand |") or die ...; while (<INPUT>) { if ( ... trigger to stop ... ) { kill 9, $pid; last; } } close INPUT;

Notice the pipe to a pipe... "gzip ... | mycommand |"

This works in the sense that it gunzips the file on the fly, and passes the data to mycommand, and then I get what I want in the script.

BUT it does not kill the gzip or mycommand processes when it gets to the "kill 9, $pid". Then it just sits at the close INPUT until the gzip and mycommand are done (which takes a few minutes).

Any ideas on how I should do the piping so I can kill the processes properly? Also, any suggestions on what signals to use in windows?

Thanks!
David A.