my %seen; for (1 .. @history) { my $item = pop @history; unshift @history, $item unless $seen{$item}++; }