in reply to Regexp variable substitution of special variable

Write your replacement this way instead: s/$search/eval qq["$replace"]/e. The key is the /e and the eval using a bit of runtime constructed perl code.

Thanks to davido for noticing a bug that would prevent my original suggestion from working.

Replies are listed 'Best First'.
Re^2: Regexp variable substitution of special variable
by Tanktalus (Canon) on Jul 01, 2005 at 18:01 UTC

    Another minor comment ... e's stack. s/$search/qq["$replace"]/ee is exactly the same as yours. Which is better is a matter of opinion. :-)

      I think its better to make the second eval explicit and very visible because it is a very different sort of eval from the first /e. So I really, strongly, prefer my version. They're functionally equivalent but you can see that something really, highly unusual is happening in mine. It isn't so visible in yours.