in reply to Re: pipes: killing the child?
in thread pipes: killing the child?

That form of open returns the child's pid...

Yikes! I did not know that.

Correction! It's much simpler than that. Closing the handle will close the pipe, which will result in the child killing itself (SIGPIPE) the next time it attempts to write to it.

I knew that but I didn't remember it.

my $pid = open(my $fr_chld, '-|', $^X, -le => '$|=1; { print ++$i; sleep 1; redo; }' );

I spent an hour trying to decipher that line, but I don't get it. As far as I can tell, '-|' forks the program, but then whey do you have to list $^X?

Replies are listed 'Best First'.
Re^3: pipes: killing the child?
by rubasov (Friar) on Jan 21, 2010 at 11:23 UTC
    As far as I can tell, that wants to be an example child process to demonstrate what will happen when you close a filehandle associated with a long-running, sporadically outputting process (without STDOUT buffering). As if your child were this:
    perl -le '$|=1; { print ++$i; sleep 1; redo; }'
    In the original post's terms this is similar to: open( my $fr_chld, "perl -le '...' |" )
Re^3: pipes: killing the child?
by ikegami (Patriarch) on Jan 21, 2010 at 16:17 UTC
    It's like
    system($^X, -le => '$|=1; { print ++$i; sleep 1; redo; }')

    but it doesn't wait for the child to end before continuing, and the child's STDOUT ends up in $fr_chld instead of being sent to the parent's STDOUT.

    As for the command itself, my demo program runs Perl ($^X) to execute an infinite loop ({ ...; redo; }) which outputs a number every second (print ++$i; sleep 1;).