regex - perl 中有 $^I 的任何文档吗?

标签 regex perl file-io special-variables

我在 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/

相关文章:

javascript - 正则表达式在函数中是如何工作的?

python - 优化python中大文件的正则表达式和文件读取操作

perl - 如何将函数返回值从引用转换为数组?

c - 读入每行包含多个元素的文本文件

c++ - 为什么在 Windows 上读取文件到 std::string 的长度错误?

c++ - 搜索/替换 boost 正则表达式 C++

公司名称变化的正则表达式

windows - 在 Unix/Linux 平台上运行的 Perl 脚本是否可以创建 Windows 快捷方式?

perl - perl中的字符串数组排序问题

java - 在java中删除数据文件的部分内容