I think you might be looking to change STDIN, STDOUT, STDERR so they point to the files you have opened. See
How do I dup() a filehandle in Perl?.
Update: Sorry, I didn't actually read the code, just the description. It looks like you've already read about duplicating filehandles.