From: Mark on
I am reading piped output from a command that produces a lot of
output. I would like to terminate reading the data before the end of
the output is reached.

The following sample program illustrates the issue. The program will
get stuck on the close(). Any help in dealing with this situation
will be appreciated.

I am having this issue on Windows XP.

use strict ;
use warnings ;

$| = 1 ;

# The following one line perl program produces the lines, "one",
"end", "two",
# continuously, forever.
# Note: You may need to change the quote characters for your command
shell.
my $GenTxtCmd = 'perl -le "while () {foreach (qw(one end two))
{print};}" ' ;

open my $FH, '-|', "$GenTxtCmd" or die "open failed: $!" ;

while (<$FH>) {
print "got $_" ;
last if /end/ ;
}

print "performing close()\n" ;
my $result = close($FH) ;
print "completed close(), result=$result\n" ;

--------
I get the following output:
got one
got end
performing close()

From: Ben Morrow on

Quoth Mark <google(a)markginsburg.com>:
> I am reading piped output from a command that produces a lot of
> output. I would like to terminate reading the data before the end of
> the output is reached.
>
> The following sample program illustrates the issue. The program will
> get stuck on the close(). Any help in dealing with this situation
> will be appreciated.
>
> I am having this issue on Windows XP.

This is probably your problem :). Try killing the process before closing
the pipe: open '-|' returns a pid, and kill TERM => $pid should do
something reasonable on Win32.

Otherwise try IPC::Run, or do it all by hand with Win32::Process.

Ben

From: Mark on
On Nov 27, 2:19 pm, Ben Morrow <b...(a)morrow.me.uk> wrote:
> This is probably your problem :). Try killing the process before closing
> the pipe: open '-|' returns a pid, and kill TERM => $pid should do
> something reasonable on Win32.
>
> Otherwise try IPC::Run, or do it all by hand with Win32::Process.

I tried killing the process from within my while(<$FH>) loop and that
did allowed the program to complete. However, I was looking for a
more elegant solution.

From: Ben Morrow on

Quoth Mark <google(a)markginsburg.com>:
> On Nov 27, 2:19 pm, Ben Morrow <b...(a)morrow.me.uk> wrote:
> > This is probably your problem :). Try killing the process before closing
> > the pipe: open '-|' returns a pid, and kill TERM => $pid should do
> > something reasonable on Win32.
> >
> > Otherwise try IPC::Run, or do it all by hand with Win32::Process.
>
> I tried killing the process from within my while(<$FH>) loop and that
> did allowed the program to complete. However, I was looking for a
> more elegant solution.

IPC::Run, as I said. It has a lot of options, but it makes the sort of
thing you are trying to do very simple.

Ben

From: Mark on
On Nov 27, 5:58 pm, Ben Morrow <b...(a)morrow.me.uk> wrote:
> Quoth Mark <goo...(a)markginsburg.com>:
>
> > On Nov 27, 2:19 pm, Ben Morrow <b...(a)morrow.me.uk> wrote:
> > > This is probably your problem :). Try killing the process before closing
> > > the pipe: open '-|' returns a pid, and kill TERM => $pid should do
> > > something reasonable on Win32.
>
> > > Otherwise try IPC::Run, or do it all by hand with Win32::Process.
>
> > I tried killing the process from within my while(<$FH>) loop and that
> > did allowed the program to complete. However, I was looking for a
> > more elegant solution.
>
> IPC::Run, as I said. It has a lot of options, but it makes the sort of
> thing you are trying to do very simple.
>
> Ben

Thanks Ben. I will check it out.