Pregunta

I have used $^I in a perl script to search and replace conditionally. I want to know whether $^I makes a copy of the file in the buffer and write backs entire buffer back to file after processing the buffer or it does in line editing.

The code is as follows

$^I = "";
my $flag = 0;

while ($line = <>) {
    if($line=~ m/some string/i) {
        $flag = 1;
    }
    if(!$flag) {
        $line =~ s/string/replace/g;
    } 
}

print $line;
¿Fue útil?

Solución

From perlvar

$INPLACE_EDIT

$^I

The current value of the inplace-edit extension. Use undef to disable inplace editing. Mnemonic: value of -i switch.

From 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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top