in reply to wait versus close on a pipe

Note that wait returns -1 if there are no more children, so this caught my eye as being a little unsafe:
... my $pid = wait; if ($pid == $pid1) { ... $pid1 = -1; # indicate child1 has been reaped } ...
If somehow you get out of sync with wait and it returns -1, you might interpret it incorrectly.

I would code the reaping loop something like this:

while (1) { my $pid = wait; last if $pid < 0; if ($pid == $pid1) { ... } elsif ($pid == $pid2) { ... } }
For a non-blocking version of wait, see perldoc -f waitpid.