Thinking outside the box - is it possible to modify whatever is generating this .pm (I'm assuming it's generated, otherwise you could easily fix the problem...) to check for duplicate keys up front and eliminate them or deal with them somehow? Seems overly complex to mess with them on the far end.