in reply to •Re: Re-runnably editing a file in place
in thread Re-runnably editing a file in place
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: $!"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
•Re: Re: •Re: Re-runnably editing a file in place
by merlyn (Sage) on May 22, 2003 at 12:39 UTC | |
by BrowserUk (Patriarch) on May 22, 2003 at 13:12 UTC | |
by merlyn (Sage) on May 22, 2003 at 13:13 UTC | |
by BrowserUk (Patriarch) on May 22, 2003 at 13:41 UTC | |
by halley (Prior) on May 22, 2003 at 13:27 UTC | |
by eyepopslikeamosquito (Archbishop) on May 22, 2003 at 13:48 UTC |