From the error in your reply, it looks like the input is not in UTF8 format at all, but an 8-bit character set. I think you are trying to escape out Latin-1. We covered this in a thread a couple days ago.
in reply to Problems with packing upper ASCII - differences across perl versions
Your Unicode behavior is going to see bytes or chars in split, and that's what's different between 5.6 and 5.8.
Try using foreach my $char (unpack ("C*", $foo)) (I think that's the right unpack syntax) to force byte seperation of the input, regardless of how the string is tagged (byte or utf8). Then use the if statement you have now as the body of the loop.