If an extension is provided:
open(my $fh_in, '<', $qfn); rename($qfn, "$qfn$ext"); open(my $fh_out, '>', $qfn);
This can be seen with strace .
$ strace perl -i~ -pe1 a ... open("a", O_RDONLY) = 3 rename("a", "a~") = 0 open("a", O_WRONLY|O_CREAT|O_EXCL, 0600) = 4 ...
If the extension is not provided:
open(my $fh_in, '<', $qfn); unlink($qfn); open(my $fh_out, '>', $qfn);
This can be seen with strace .
$ strace perl -i -pe1 a ... open("a", O_RDONLY) = 3 unlink("a") = 0 open("a", O_WRONLY|O_CREAT|O_EXCL, 0600) = 4 ...
Unix systems like Macs support anonymous files. Windows does not work, so -i requires an extension.
>perl -i.bak -pe1 a >perl -i -pe1 a Can't do inplace edit without backup.
If we integrate this knowledge into the code that you published, we get the following:
#!/usr/bin/perl use strict; use warnings; my $extension = '.orig'; my $oldargv = ''; my $backup; LINE: while (<>) { if ($ARGV ne $oldargv) { if (length($extension)) { if ($extension !~ /\*/) { $backup = $ARGV . $extension; } else { ($backup = $extension) =~ s/\*/$ARGV/g; } rename($ARGV, $backup); } else { die("Can't do inplace edit without backup.\n") if $^O eq 'MSWin32'; unlink($ARGV); } open(ARGVOUT, ">$ARGV"); select(ARGVOUT); $oldargv = $ARGV; }