in reply to Avoid run-time checking

Perhaps you could use simply a dispatch table which will tell you which subroutine reference to call repeatedly depending on what you've found in the first record. (You could also tamper with the symbol table, but that's seems a bit of an overkill.)

Or you might simply store your unpack template string into a variable. I've never tried that, but I think this should work without any problem. Then you just assign that variable in accordance with what you've found in the first record.