【发布时间】:2016-02-11 05:13:19
【问题描述】:
所以我正在编写一个 perl 脚本,该脚本对输入执行替换命令,并且输入将带有反斜杠。所以只需用新文本替换一些文本
例如,如果输入是
"\text"
我可能会输出
"newText"
请注意,斜线已被删除。
这里有一些代码:
$oldText = "\\Text";
while (<STDIN>) {
$ln = $_;
$ln2 = $_;
$ln2 =~ s/\\Text/newText/;
$ln =~ s/$oldText/newText/;
}
print "$ln\n";
print "$ln2\n";
当输入是“文本”时,输出是
\newText #Incorrect because the \ is still there
newText #Correct
谁能解释为什么使用字符串而不是变量将输出更改为我想要的?我知道 \ 取消了以下字符的引用,这可能是问题的根源。但我无法理解为什么使用变量会改变输出。将变量 oldText 更改为 "\text" 不会改变输出。
【问题讨论】: