I notice that you're using lc to lowercase the extensions. IIRC, a mac has (or can be configured to use) a case sensitive file system...so "foo.PDF" and "foo.pdf" are two different filenames. That might be (part of?) your problem.
I don't know if you're familiar with File::Spec or not, but it might do the trick for you. It provides a portable method to handle file names and paths. I've not used it much myself, so I can't give it an unqualified endorsement. But it may be worth your while to investigate it.