in reply to Re^2: Efficiently create an IF/ELSE regex
in thread Efficiently create an IF/ELSE regex

Unless you want to modify default variable $_ & save the number of replacements, there is typo in your s/// application. In that case, op should be (to save the result of all of replacements in another string) ...

( my $fixed = $input ) =~ s/ search / replace /x;

For fun, try the proposed s/(\s)/\\$whitespace{$1}/g solution on string x y (a plain space in between).