Factor out atomic file overwriting into its own function.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 19 Nov 2014 22:24:35 +0000 (23:24 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 19 Nov 2014 22:24:35 +0000 (23:24 +0100)
remoteglot.pl

index d82c20b..fb74e6e 100755 (executable)
@@ -742,11 +742,18 @@ sub output_json {
        }
        $json->{'refutation_lines'} = \%refutation_lines;
 
        }
        $json->{'refutation_lines'} = \%refutation_lines;
 
-       open my $fh, ">", $remoteglotconf::json_output . ".tmp"
+       my $encoded = JSON::XS::encode_json($json);
+       atomic_set_contents($remoteglotconf::json_output, $encoded);
+}
+
+sub atomic_set_contents {
+       my ($filename, $contents) = @_;
+
+       open my $fh, ">", $filename . ".tmp"
                or return;
                or return;
-       print $fh JSON::XS::encode_json($json);
+       print $fh $contents;
        close $fh;
        close $fh;
-       rename($remoteglotconf::json_output . ".tmp", $remoteglotconf::json_output);
+       rename($filename . ".tmp", $filename);
 }
 
 sub uciprint {
 }
 
 sub uciprint {