in reply to removing repeats

if ($word eq $prevword) {
This will check for adjacent repeats, and print them.
Shouldnt you be doing
if ($word ne $prevword) {
instead??
Also there is no need to use $n seperately. The $. variable keeps track of the line numbers for you.

Manav