The script is to run on Windows and various Unix flavours. I suppose I should test $^O to eliminate the unnecessary step on the Unices.
Update. Thanks to merlyn, I have improved my first attempt. On Unix, don't chmod a read-only file because renaming to a read-only file works fine there -- permissions on *directory* (not file) control rename (and delete) on Unix. Permissions now changed on temporary file, not original; this makes the rename the last (atomic) action, making the code 100% rerunnable on Unix, I think. On Windows, there is an extremely low chance of trouble: the rename is right after the chmod(...0200) so the worst that can happen is a read-only file may become writable.
if ($file_contents_changed) { my $bak = $fname . $$ . '.tmp'; -e $bak and (unlink($bak) or die "unlink $bak: $!"); open(my $fh, '>'.$bak) or die "create $bak: $!"; print $fh @lines or die "writing $bak: $!"; close($fh) or die "close $bak: $!"; defined(my $mode = (stat($fname))[2]) or die "stat $fname: $!"; chmod($mode, $bak) or die "chmod $bak: $!"; $^O eq 'MSWin32' && ! -w _ and (chmod($mode|0200, $fname) or die "chmod $fname: $!"); rename($bak, $fname) or die "rename $bak $fname: $!"; }
In reply to Re: •Re: Re-runnably editing a file in place
by eyepopslikeamosquito
in thread Re-runnably editing a file in place
by eyepopslikeamosquito
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |