in reply to Bug in perl?

Just in case you're running this on ms-windows, you might want to try adding  binmode INPUT2 just before the while loop. And in that case, you might also want to add something like this inside the while loop:
if ( $line =~ /\cZ/ ) { warn "found DOS end-of-text-file marker at line $.\n"; }
Just a guess...