in reply to right way to reopen STDOUT in a child

You could save/dup STDOUT before closing it, and then reopen it when required:

open my $saved_stdout, ">&STDOUT"; close STDOUT; # ... open STDOUT, ">&", $saved_stdout or die "can't reopen STDOUT: $!"; my $kidpid = open my $fh, "-|"; # ...

Note that you need to reopen STDOUT before doing the forking pipe open (i.e. not in the child), otherwise you'd get in conflict with Perl implicitly redirecting STDOUT to the pipe...

Update: actually, you could also just reopen it to anything, say /dev/null (then you wouldn't need to save it first). As STDOUT is being redirected anyway in the child, it doesn't matter much... as long as it's open when you open the pipe.