我在 perl 脚本中使用了 $^I
来有条件地搜索和替换。我想知道 $^I
是在缓冲区中复制文件并在处理缓冲区后将整个缓冲区写回文件还是在行编辑中。
代码如下
$^I = "";
my $flag = 0;
while ($line = <>) {
if($line=~ m/some string/i) {
$flag = 1;
}
if(!$flag) {
$line =~ s/string/replace/g;
}
}
print $line;
最佳答案
来自 perlvar
$INPLACE_EDIT
$^I
The current value of the inplace-edit extension. Use undef to disable inplace editing. Mnemonic: value of -i switch.
来自 perlrun :
-i[extension]
specifies that files processed by the <> construct are to be edited in-place. It does this by renaming the input file, opening the output file by the original name, and selecting that output file as the default for print() statements. The extension, if supplied, is used to modify the name of the old file to make a backup copy, following these rules:
If no extension is supplied, and your system supports it, the original file is kept open without a name while the output is redirected to a new file with the original filename. When perl exits, cleanly or not, the original file is unlinked.
关于regex - perl 中有 $^I 的任何文档吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20809425/