use Getopt::Long qw(:config bundling no_auto_abbrev);
use Pod::Usage;
use Config;
+use Carp;
my @targets
= qw(config.sh config.h miniperl lib/Config.pm Fcntl perl test_prep);
# which updated to MakeMaker 3.7, which changed from using a hard coded ld
# in the Makefile to $(LD). On x86_64 Linux the "linker" is gcc.
+sub open_or_die {
+ my $file = shift;
+ my $mode = @_ ? shift : '<';
+ open my $fh, $mode, $file or croak("Can't open $file: $!");
+ ${*$fh{SCALAR}} = $file;
+ return $fh;
+}
+
+sub close_or_die {
+ my $fh = shift;
+ return if close $fh;
+ croak("Can't close: $!") unless ref $fh eq 'GLOB';
+ croak("Can't close ${*$fh{SCALAR}}: $!");
+}
+
sub extract_from_file {
my ($file, $rx, $default) = @_;
- open my $fh, '<', $file or die "Can't open $file: $!";
+ my $fh = open_or_die($file);
while (<$fh>) {
my @got = $_ =~ $rx;
return wantarray ? @got : $got[0]
sub edit_file {
my ($file, $munger) = @_;
local $/;
- open my $fh, '<', $file or die "Can't open $file: $!";
+ my $fh = open_or_die($file);
my $orig = <$fh>;
die "Can't read $file: $!" unless defined $orig && close $fh;
my $new = $munger->($orig);
return if $new eq $orig;
- open $fh, '>', $file or die "Can't open $file: $!";
+ $fh = open_or_die($file, '>');
print $fh $new or die "Can't print to $file: $!";
- close $fh or die "Can't close $file: $!";
+ close_or_die($fh);
}
sub apply_patch {
}
foreach my $file (@files) {
- open my $fh, '<', $file or die "Can't open $file: $!";
+ my $fh = open_or_die($file);
while (<$fh>) {
if ($_ =~ $re) {
++$matches;
}
}
}
- close $fh or die "Can't close $file: $!";
+ close_or_die($fh);
}
report_and_exit(!$matches,
$matches == 1 ? '1 match for' : "$matches matches for",
system 'git show f6527d0ef0c13ad4 | patch -p1'
and die;
} elsif(!extract_from_file('hints/linux.sh', qr/^sparc-linux\)$/)) {
- open my $fh, '>>', 'hints/linux.sh'
- or die "Can't open hints/linux.sh: $!";
+ my $fh = open_or_die('hints/linux.sh', '>>');
print $fh <<'EOT' or die $!;
case "`uname -m`" in
;;
esac
EOT
- close $fh or die "Can't close hints/linux.sh: $!";
+ close_or_die($fh);
}
}
}
my (@missing, @created_dirs);
if ($options{'force-manifest'}) {
- open my $fh, '<', 'MANIFEST'
- or die "Could not open MANIFEST: $!";
+ my $fh = open_or_die('MANIFEST');
while (<$fh>) {
next unless /^(\S+)/;
# -d is special case needed (at least) between 27332437a2ed1941 and
push @missing, $1
unless -f $1 || -d $1;
}
- close $fh or die "Can't close MANIFEST: $!";
+ close_or_die($fh);
foreach my $pathname (@missing) {
my @parts = split '/', $pathname;
mkdir $path, 0700 or die "Can't create $path: $!";
unshift @created_dirs, $path;
}
- open $fh, '>', $pathname or die "Can't open $pathname: $!";
- close $fh or die "Can't close $pathname: $!";
+ $fh = open_or_die($pathname, '>');
+ close_or_die($fh);
chmod 0, $pathname or die "Can't chmod 0 $pathname: $!";
}
}