This line: foreach my $fd( keys %fd ) {
should be: foreach my $fd( values %fd ) {
and instead of reading it in line by line, it would be more efficient just to read a chunk of it at a time so instead of while( <IN> ) {
use while( read(IN, $_, 4096) ) {