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: $!";
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|