The routine should not gobble up white space. This leads to all manner of bad side-effects, such as encoded words starting and ending with spaces where you'd want words surrounded by spaces, or encoding sequences of words instead of single words.
Fix: When constructing $especials, change the first line to read join( '|' => '\s', map { quotemeta( chr($_) ) } ---------------^^^^^