Ahh, refactoring. It reminds me of the days of
Transport Tycoon when you're trying to find the right track combination that's both elegant and efficient. In this case, I think a whole lot of fuss can be trimmed right out, which in track terms means less money and faster transport. In programmatic terms, simpler can, to a point, mean fewer bugs, since the code is much more concise.
What about this idiomatic solution?
!$history{$_}++ && splice(@history, $i--, 1);
Or even better, ditch that awful for-loop:
my %seen;
@history = reverse grep { !$seen{$_}++ } reverse @history;