I am trying to understand what happens when a variable OR backslash OR variable containing backlash is put into a regex substitution, for example s/$var1/$var2/ or s/abc\\/xyz\\/ , etc...
This is what I tried:
use Modern::Perl; no strict;
$bs_a = "\\_a";
$bs_b = "\\_b";
$str_to_substitute = "C:\\tmp\\_a";
### I'm gonna change this string to C:\tmp\_b by regex substitution
### pattern part: $bs_a, replacement part: $bs_b
### using the whole strings instead of just s/a/b/ because for example
### I may have many such string pairs for substitutions in an external file
#1
$result = $str_to_substitute =~ s/\\_a/\\_b/r;
say $result; # C:\tmp\_b ... OK
#2
$result = $str_to_substitute =~ s/\\_a/$bs_b/r; #
say $result; # C:\tmp\_b ... OK
#3
$result = $str_to_substitute =~ s/$bs_a/$bs_b/r; #
say $result; # C:\tmp\\_b ... why?? what is the difference between #1 and #3
#4
$result = $str_to_substitute =~ s'$bs_a'$bs_b'r;
say $result; # $bs_a is literally '$bs_a', $bs_b also is literal
#5
$result = $str_to_substitute =~ s/$bs_a/\\_b/r;
say $result; # C:\tmp\\_b ?? what is the difference between #1 and #5 (and #3)
What I was thinking is that the replacement doesn't do any escaping at all. (the pattern part does)
Now I am really confused and I don't understand why the results #1,#3 and the results #1,#5 are different.
- Does anybody know why?
- Does anybody know good easily understandable source :-) on what happens when I put variables and backlashes in the replacement part? (or in the pattern part as well but regarding substitution)