perl -i
boils down to the following core:
open(my $fh_in, '<', $qfn);
unlink($qfn);
open(my $fh_out, '>', $qfn);
$fh_in
is called an anonymous file handle. Notice how the associated file from which it reads no longer exists in the directory.
The problem is that Windows doesn't support anonymous file handles. When you delete a file that's already open, the directory entry remains until the file is closed. This prevents Perl from creating an output file with the same name. That's why -i
(without an extension) isn't supported on Windows.
>perl -i -ne"..." file
Can't do inplace edit without backup.
But you aren't using Windows. You're using cygwin, an environment that emulates unix. That's why using -i
(without an extension) didn't give you an error. However, cygwin is still limited to the capabilities of the host OS, so it can't create anonymous files[1]. As such, Perl is programmed to behave as if -i.bak
was provided when -i
is provided to a cygwin build of Perl.
It seems to be able to emulate them, but that feature isn't being used for some reason.
$ cat foo
abc
def
$ perl -E'
open(my $fh, "<", "foo") or die $!;
system("ls -1");
say "--";
unlink("foo") or warn $!;
system("ls -1");
say "--";
print while <$fh>;
'
foo
--
--
abc
def