It looks to me that you have some newline problem. You should must check the file for it. Mostly newlines aren't a universally accepted standard; they are different depending platform (OS) : For DOS / Windows it is \r\n whereas in Unix the newline is recognized by \n.
So trim your lines for \r ;
I am agree with Jit, newline is the common problem in this kind of cases. I have modified your trim subroutine to remove remove odd or bad newline values from your INFILE.
# Perl trim function to remove whitespace from the start and end of the string
Hi Jitendra Narayan. Your trim subroutine works perfect. Thanks a lot.
— Neel 4130 days ago