为什么在使用 ActivePerl 时必须指定带有备份扩展的 -i 开关? [英] Why do I have to specify the -i switch with a backup extension when using ActivePerl?
问题描述
除非我使用备份扩展指定它们,否则我无法在 ActivePerl 下就地编辑 Perl one-liners:
I cannot get in-place editing Perl one-liners running under ActivePerl to work unless I specify them with a backup extension:
C:> perl -i -ape "splice (@F, 2, 0, q(inserted text)); $_ = qq(@F
);" file1.txt
Can't do inplace edit without backup.
与 -i.bak
或 -i.orig
相同的命令可以工作,但在此过程中会创建不需要的备份文件.
The same command with -i.bak
or -i.orig
works a treat but creates an unwanted backup file in the process.
有没有办法解决这个问题?
Is there a way around this?
推荐答案
这是 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
这篇关于为什么在使用 ActivePerl 时必须指定带有备份扩展的 -i 开关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!