From perldoc 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 forprint()
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, no backup is made and the current file is overwritten.
[…]
Rephrased:
- The backup filename is determined from the value of the
-i
-switch, if one is given. - The original file is renamed to the new filename, and opened for the script. Renaming is atomic on most filesystems.
- A file with the name of the original file is opened for writing. The file will start with length zero, but is not identical to the original file (which has a different name now).
- After the script has finished, and if no explicit backup extension was provided, the backup file is deleted. The original file is then lost.
Should the system run out of drive space, then the new file is endangered, not the original file which was never copied or moved (at least on filesystems with an inode-like concept).