Seems perfectly reasonable to me. If this routine works for the files you have to process, it is correct. Maybe a warn instead of the dies in the routine can be used to tag the "corrupt" files so that the program doesn't bail out if only 1 or 2 files of the hundreds are faulty. But this may not be appropriate for your purpose.