考虑一下:
my $var = "${SOME_VAR}"
my $string "In this text ${SOME_VAR} will be replaced with X"
$string =~ s/$var/X/g;
print "\nprocessed string = <".$string.">";
返回
processed string = <In this text ${SOME_VAR} will be replaced with X>
即不发生替换。
但是,如果 $var 不包含保留字符 ($),则会发生替换:
my $var = "SOME_VAR"
/* everything else the same */
返回
processed string = <In this text ${X} will be replaced with X>
但是我不知道当保留字符包含在变量中时如何转义它们,即当搜索标记不是显式但参数化时。
任何想法?
谢谢