This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update TOC for perl51310delta
[perl5.git] / regen / regen_lib.pl
CommitLineData
9ad884cb
JH
1#!/usr/bin/perl -w
2use strict;
3879ea51 3use vars qw($Needs_Write $Verbose @Changed $TAP);
424a4936
NC
4use File::Compare;
5use Symbol;
9ad884cb
JH
6
7# Common functions needed by the regen scripts
8
bb21036d 9$Needs_Write = $^O eq 'cygwin' || $^O eq 'os2' || $^O eq 'MSWin32';
9ad884cb 10
523b3031
NC
11$Verbose = 0;
12@ARGV = grep { not($_ eq '-q' and $Verbose = -1) }
3879ea51 13 grep { not($_ eq '--tap' and $TAP = 1) }
523b3031
NC
14 grep { not($_ eq '-v' and $Verbose = 1) } @ARGV;
15
16END {
17 print STDOUT "Changed: @Changed\n" if @Changed;
18}
95aa0565 19
9ad884cb
JH
20sub safer_unlink {
21 my @names = @_;
22 my $cnt = 0;
23
24 my $name;
25 foreach $name (@names) {
26 next unless -e $name;
27 chmod 0777, $name if $Needs_Write;
28 ( CORE::unlink($name) and ++$cnt
29 or warn "Couldn't unlink $name: $!\n" );
30 }
31 return $cnt;
32}
33
34sub safer_rename_silent {
35 my ($from, $to) = @_;
36
37 # Some dosish systems can't rename over an existing file:
38 safer_unlink $to;
39 chmod 0600, $from if $Needs_Write;
40 rename $from, $to;
41}
42
424a4936 43sub rename_if_different {
9ad884cb 44 my ($from, $to) = @_;
b6b9a099 45
3879ea51
NC
46 if ($TAP) {
47 my $not = compare($from, $to) ? 'not ' : '';
48 print STDOUT $not . "ok - $0 $to\n";
49 safer_unlink($from);
50 return;
51 }
424a4936 52 if (compare($from, $to) == 0) {
523b3031 53 warn "no changes between '$from' & '$to'\n" if $Verbose > 0;
b6b9a099
JC
54 safer_unlink($from);
55 return;
56 }
523b3031
NC
57 warn "changed '$from' to '$to'\n" if $Verbose > 0;
58 push @Changed, $to unless $Verbose < 0;
9ad884cb
JH
59 safer_rename_silent($from, $to) or die "renaming $from to $to: $!";
60}
424a4936
NC
61
62# Saf*er*, but not totally safe. And assumes always open for output.
63sub safer_open {
64 my $name = shift;
65 my $fh = gensym;
66 open $fh, ">$name" or die "Can't create $name: $!";
08858ed2 67 *{$fh}->{SCALAR} = $name;
424a4936
NC
68 binmode $fh;
69 $fh;
70}
71
08858ed2
NC
72sub safer_close {
73 my $fh = shift;
74 close $fh or die 'Error closing ' . *{$fh}->{SCALAR} . ": $!";
75}
76
9ad884cb 771;