Just an aside: you don't use binmode (both on input and output filehandle). Perhaps it works for you now, but on Windows and with the newer Unicode related behavior of Perl (there's more than text and byte mode for file handles, nowadays), you should.