in reply to search and replace weird output

Every line you read (into $_) has a newline ("\n") at the end of it. You're using a newline to represent the "fred" -> "Wilma" replacement while the "wilma" -> "Fred" replacement is happening, so when it comes time to replace the newline with "Wilma", there's always one at the end of the input.

I suspect that your earlier solution had a chomp at the beginning of the loop to remove the newline, and then the newline was added again around the time of the print. Like this:

chomp; $_ =~ s/fred/\n/i; $_ =~ s/wilma/Fred/i; $_ =~ s/\n/Wilma/; print; print "\n";