为什么在使用ActivePerl时必须指定带有备份扩展名的-i开关? [英] Why do I have to specify the -i switch with a backup extension when using ActivePerl?
问题描述
除非在我使用备用扩展名指定它们的情况下,否则我无法在ActivePerl下运行的就地编辑Perl单行代码正常工作.
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\n);" 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
并同时保持打开的句柄,然后重新创建具有相同名称的文件.即使文件1.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屋!