note
LanX
<I>> > Obviously Perl needs to escape the backslash inside a regex</I><P>
<I>> not after interpolation of variables like $bs</I><P>
I have to correct myself, it depends on the side of the substitution
<UL>
<LI> the right side is a simple doublequoted string, so after interpolation no escaping
<LI> but the left side is a regex with two levels of escaping
<ol>
<LI> string interpolation with escaping first, i.e. <C>\$bs</C> won't be interpolated
<LI> regex interpretation with escaping first, i.e. <C>/\*/</C> is not a quantifier but literal <C>*</C> same with <C>/$bs*/</C>
</ol>
</UL>
compare
<c>
d:\>perl -E"for $x (qw/C:\berrybrew\test C:\berrybrew /) {$_=$x; $bs=chr(92); s#$bs$bs#<${bs}>#g; say}"
C:<\>berrybrew<\>test
C:<\>berrybrew
d:\>
</c><P>
or to eliminate the Win command line<P>
<c>
use v5.12;
use warnings;
use Data::Dump;
my $bs=chr(92);
for my $x (qw/C:\berrybrew\test C:\berrybrew /) {
$_=$x;
s#$bs$bs#<${bs}>#g;
say;
#ddx $_;
}
$_='$bs';
s/\$bs/<$&>/;
say;
</c>
<c>
C:<\>berrybrew<\>test
C:<\>berrybrew
<$bs>
</c>
so this might be the OPs original problem<P>
see also<P>
[ID://1007141]<P>
<div class="pmsig"><div class="pmsig-708738">
<!--nowiki--><p>Cheers Rolf<br>
<sub>(addicted to the Perl Programming Language :)
<br> <i> [id://1153804|Wikisyntax for the Monastery]</i>
</sub>
<!--nowiki-->
</div></div><P>
<!-- Wiki2Monks {"version":1.161} -->
11134004
11134023