Your bug is that you are setting $md5 on every single line of input. So when you hit an error line, you no longer know the md5 from the line before.
To fix it you need to define $md5 outside of the while loop that reads through the file, and then inside the file you need to set it only if the record matched. That way it will get set when you encounter that line, and it will still be set on the error line.
(Incidentally you left two closing braces off of your cut and paste of your script.)
Edit: JavaFan is correct. I'd missed paragraph mode which does fix that potential problem. The cause of the undef warnings is that $UID is not populated, because the UID line is on the second paragraph, not the first. If you add another <$input_fh>; that warning goes away. (This is very, very fragile logic. But it seems to work.)
If there is still no output, then I would advise checking whether it is looking for the files in the right place.