I haven't looked at the documentation for the perl function or the source, but are items enumerated from 0? If so, you might need something like
Code:
$corpse->RemoveItem($corpse->CountItems() - 1);
to remove the last item (since item 1 is at index 0, last item would be at index -1).