$last is used to remove dups. $last needs to be set to the $e of this pass to compare it to the $e of the next pass.
I suppose you could get rid of $last and do
@array = sort @array;
my $j = -1;
for my $e (@array) {
if ($j < 0 || $array[$j] ne $e) {
$array[++$j] = $e;
}
}
$#array = $j;