sub Pooled_function { open(my $STDOUT_SAVE, ">&STDOUT"); # save current STDOUT while (<$parentfh>) { chomp; my ($pclass, $copy_to_console) = split(' ', $_, 2); my $file = ...; if ($copy_to_console) { open(STDOUT, ">&", $STDOUT_SAVE); open(OUT, "|-", "tee", $file); } else { open(OUT, ">", $file); } open(STDOUT, ">&OUT"); open(STDERR, ">&OUT"); # Do the stuff close(STDOUT); close(STDERR); close(OUT); } close($parentfh); }