This isn't exactly the problem at hand, but I noticed you selected the filehandle instead of just supplying the filehandle to print. E.g. you did:
select OUT; print $text;
But it is also possible to do this:
print OUT $text;
Your method is more flexible because you simply need a different select to change were all of your print stmts route their output to. Mine is faster to code.