Why do I have to specify the -i switch with a backup extension when using ActivePerl?

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

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

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

Leave a Comment