The reason it doesn't work is (of course) found in the Camel Book:
The right side of the substitution (between the second and third slashes) is mostly a funny kind of double-quoted string, which is why you can interpolate variables there, including backreference variables. (p.41)
As you know you only get one level of interpolation in Perl, i.e. your $repl gets replaced by whatever its value is at that moment and then interpolation stops and hands the regex to the regex-engine, which will find a literal $1 and not the value inside the backreference variable $1 you wished for.
The only solution is through the /e modifier als already pointed out.
CountZero
"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
In reply to Re: $1 in variable regex replacement string
by CountZero
in thread $1 in variable regex replacement string
by dvergin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |