It is probably better to use Text::Balanced's extract_quotelike function rather than hand-rolling a substitution parser. (At least, the documentation suggests that it will handle s///'s correctly; and your code makes no effort to handle differently delimited patterns, among the (I assume) many other possible perversions.)