You could make a sub that does the counting and dirty work of opening new files, closing full files, printing the lines to the files and call that in place of print OUT "lala\n"; in your code...
-Waswas
Comment on Re: Tracking Lines Printed to a FileHandle