如何在反向语句中使用正则表达式

时间:2016-09-15 00:56:04

标签: perl while-loop reverse

open IN, "in.txt";
open OUT, ">out.txt";

print OUT unless m[^foo] while <IN>;

为什么上面的代码没有编译?

1 个答案:

答案 0 :(得分:6)

问题在于,允许您将if / unless / while作为后缀写入一行代码的符号只能在该行中使用一次。因此,您必须使用以下代码替换代码中的最后一行:

while (<IN>) {
    print OUT unless m[^foo];
}

这种情况的替代方案是使用or

m[^foo] or print OUT while <IN>;