且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么在使用 ActivePerl 时必须指定带有备份扩展的 -i 开关?

更新时间:2021-11-23 22:33:08

这是 Windows/MS-DOS 的限制.根据 perldiag:

This is a Windows/MS-DOS limitation. According to perldiag:

您使用的是诸如 MS-DOS 之类的系统,如果您尝试从已删除(但仍处于打开状态)的文件中读取,则会感到困惑.你必须说 -i.bak 或类似的.

You're on a system such as MS-DOS that gets confused if you try reading from a deleted (but still opened) file. You have to say -i.bak, or some such.

Perl 的 -i 实现会导致它删除 file1.txt,同时保持它的打开句柄,然后重新创建具有相同名称的文件.这允许您读取"file1.txt,即使它已被删除并正在重新创建.不幸的是,Windows/MS-DOS 不允许您删除附加了打开句柄的文件,因此该机制不起作用.

Perl's -i implementation causes it to delete file1.txt while keeping an open handle to it, then re-create the file with the same name. This allows you to 'read' file1.txt even though it has been deleted and is being re-created. Unfortunately, Windows/MS-DOS does not allow you to delete a file that has an open handle attached to it, so this mechanism does not work.

***的办法是使用 -i.bak 然后删除备份文件.这至少为您提供了一些保护 - 例如,如果 perl 以非零退出代码退出,您可以选择不删除备份.类似的东西:

Your best shot is to use -i.bak and then delete the backup file. This at least gives you some protection - for example, you could opt not to delete the backup if perl exits with a non-zero exit code. Something like:

perl -i.bak -ape "splice...." file1.txt && del file1.bak