in reply to Printing keys from a hash by value?

foreach my $value (%top_words)

I think you mean reverse( sort keys(%top_words)) there.

Also see perlfaq4 on How do I sort a hash (optionally by value instead of key)?. Your approach loses one of the keys whenever two keys have the same value.