This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Rephrase perlop for non-destructive substitution
[perl5.git] / regen_lib.pl
index 896a9ad..7d396c0 100644 (file)
@@ -1,36 +1,19 @@
 #!/usr/bin/perl -w
 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 vars qw($Needs_Write $Verbose @Changed);
+use File::Compare;
+use Symbol;
 
 # Common functions needed by the regen scripts
 
-$Is_W32 = $^O eq 'MSWin32';
-$Is_OS2 = $^O eq 'os2';
-$Is_Cygwin = $^O eq 'cygwin';
-$Is_NetWare = $Config{osname} eq 'NetWare';
-if ($Is_NetWare) {
-  $Is_W32 = 0;
-}
-
-$Needs_Write = $Is_OS2 || $Is_W32 || $Is_Cygwin || $Is_NetWare;
+$Needs_Write = $^O eq 'cygwin' || $^O eq 'os2' || $^O eq 'MSWin32';
 
-eval "use Digest::MD5 'md5'; 1;"
-    or warn "Digest::MD5 unavailable, doing unconditional regen\n";
+$Verbose = 0;
+@ARGV = grep { not($_ eq '-q' and $Verbose = -1) }
+  grep { not($_ eq '-v' and $Verbose = 1) } @ARGV;
 
-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;
+END {
+  print STDOUT "Changed: @Changed\n" if @Changed;
 }
 
 sub safer_unlink {
@@ -56,23 +39,32 @@ 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) {
-      warn "no changes between '$from' & '$to'\n";
+  if (compare($from, $to) == 0) {
+      warn "no changes between '$from' & '$to'\n" if $Verbose > 0;
       safer_unlink($from);
       return;
   }
-  warn "changed '$from' to '$to'\n";
+  warn "changed '$from' to '$to'\n" if $Verbose > 0;
+  push @Changed, $to unless $Verbose < 0;
   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: $!";
+    *{$fh}->{SCALAR} = $name;
+    binmode $fh;
+    $fh;
+}
+
+sub safer_close {
+    my $fh = shift;
+    close $fh or die 'Error closing ' . *{$fh}->{SCALAR} . ": $!";
+}
+
 1;