summaryrefslogtreecommitdiff
path: root/regen_lib.pl
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2008-03-15 18:37:34 +0000
committerNicholas Clark <nick@ccl4.org>2008-03-15 18:37:34 +0000
commit424a4936e3f61f4e8db394f496a116e698cede85 (patch)
treee19475f64cd932d850b6975251ee284844aaf0d8 /regen_lib.pl
parentb6b9a09997c80269af874aff41936e014ed728f7 (diff)
downloadperl-424a4936e3f61f4e8db394f496a116e698cede85.tar.gz
Rename safer_rename() to rename_if_different(), to accurately describe
what it does. Use File::Compare rather than Digest::MD5, as the files are small enough to simply read in. (File::Compare dates from 5.004) Remove safer_rename_always(), which isn't used. DRY by replacing the cargo-culted "open or die" with a new function safer_open(), which uses Gensym (5.002) to create an anonymous file handle, and opens and binmodes the file, or dies. This necessitates replacing bareword file handles with lexicals in all the callers. Correct the names of files in close or die constructions. p4raw-id: //depot/perl@33538
Diffstat (limited to 'regen_lib.pl')
-rw-r--r--regen_lib.pl42
1 files changed, 14 insertions, 28 deletions
diff --git a/regen_lib.pl b/regen_lib.pl
index 896a9ad0fe..824926554a 100644
--- a/regen_lib.pl
+++ b/regen_lib.pl
@@ -2,6 +2,8 @@
use strict;
use vars qw($Is_W32 $Is_OS2 $Is_Cygwin $Is_NetWare $Needs_Write);
use Config; # Remember, this is running using an existing perl
+use File::Compare;
+use Symbol;
# Common functions needed by the regen scripts
@@ -15,24 +17,6 @@ if ($Is_NetWare) {
$Needs_Write = $Is_OS2 || $Is_W32 || $Is_Cygwin || $Is_NetWare;
-eval "use Digest::MD5 'md5'; 1;"
- or warn "Digest::MD5 unavailable, doing unconditional regen\n";
-
-sub cksum {
- my $pl = shift;
- my ($buf, $cksum);
- local *FH;
- if (open(FH, $pl)) {
- local $/;
- $buf = <FH>;
- $cksum = defined &md5 ? md5($buf) : 0;
- close FH;
- } else {
- warn "$0: $pl: $!\n";
- }
- return $cksum;
-}
-
sub safer_unlink {
my @names = @_;
my $cnt = 0;
@@ -56,18 +40,10 @@ sub safer_rename_silent {
rename $from, $to;
}
-sub safer_rename_always {
- my ($from, $to) = @_;
- safer_rename_silent($from, $to) or die "renaming $from to $to: $!";
-}
-
-sub safer_rename {
+sub rename_if_different {
my ($from, $to) = @_;
- my $fc = cksum($from);
- my $tc = cksum($to);
-
- if ($fc and $fc eq $tc) {
+ if (compare($from, $to) == 0) {
warn "no changes between '$from' & '$to'\n";
safer_unlink($from);
return;
@@ -75,4 +51,14 @@ sub safer_rename {
warn "changed '$from' to '$to'\n";
safer_rename_silent($from, $to) or die "renaming $from to $to: $!";
}
+
+# Saf*er*, but not totally safe. And assumes always open for output.
+sub safer_open {
+ my $name = shift;
+ my $fh = gensym;
+ open $fh, ">$name" or die "Can't create $name: $!";
+ binmode $fh;
+ $fh;
+}
+
1;