in reply to Idiom for a regex translation

Other replies have shown you how to do what you want, I'll just (try to attempt to) explain why it is.

You can't really say "Show me the hypothetical modification of this string if I substituted foo for bar". It doesn't work that way. s/// needs to operate on a string. In the process of carrying out what you ask it to do, it destroys the original contents.

If you want to keep the original contents around, you must take a copy of it, and modify the copy. You can shorten the lifetime of the copy down to a very small point in your code by enclosing its creation and use in a { ... } lexical scope.

- another intruder with the mooring in the heart of the Perl