Skip to content

Commit d7443ac

Browse files
committed
Remove IO::File and replace with safe usage of open()
2 parents 0c5925f + e96e5ac commit d7443ac

File tree

17 files changed

+71
-87
lines changed

17 files changed

+71
-87
lines changed

Build.PL

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ my $build = ModuleBuildBuilder->new(
5252
'ExtUtils::Manifest' => 0,
5353
'ExtUtils::Mkbootstrap' => 0,
5454
'ExtUtils::ParseXS' => 2.21, # various bug fixes
55-
'IO::File' => 0,
5655
'Cwd' => 0,
5756
'Text::Abbrev' => 0,
5857
'Text::ParseWords' => 0,
@@ -101,7 +100,6 @@ my $build = ModuleBuildBuilder->new(
101100
},
102101
PPM_support => {
103102
description => "Generate PPM files for distributions",
104-
requires => {'IO::File' => 1.13}, # binmode bug in older core IO::File
105103
},
106104
inc_bundling_support => {
107105
description => "Bundle Module::Build in inc/",

inc/Module/Metadata.pm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ $VERSION = '1.000002';
1515
$VERSION = eval $VERSION;
1616

1717
use File::Spec;
18-
use IO::File;
1918
use version 0.87;
2019
BEGIN {
2120
if ($INC{'Log/Contextual.pm'}) {
@@ -379,7 +378,7 @@ sub _parse_file {
379378
my $self = shift;
380379

381380
my $filename = $self->{filename};
382-
my $fh = IO::File->new( $filename )
381+
open( my $fh, '<', $filename )
383382
or die( "Can't open '$filename': $!" );
384383

385384
$self->_parse_fh($fh);

lib/Module/Build/Base.pm

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use File::Basename ();
1919
use File::Spec 0.82 ();
2020
use File::Compare ();
2121
use Module::Build::Dumper ();
22-
use IO::File ();
2322
use Text::ParseWords ();
2423

2524
use Module::Build::ModuleInfo;
@@ -1082,7 +1081,7 @@ sub subclass {
10821081
File::Path::mkpath($filedir);
10831082
die "Can't create directory $filedir: $!" unless -d $filedir;
10841083

1085-
my $fh = IO::File->new("> $filename") or die "Can't create $filename: $!";
1084+
open(my $fh, '>', $filename) or die "Can't create $filename: $!";
10861085
print $fh <<EOF;
10871086
package $opts{class};
10881087
use $pack;
@@ -1229,7 +1228,7 @@ sub _pod_parse {
12291228

12301229
my $docfile = $self->_main_docfile
12311230
or return;
1232-
my $fh = IO::File->new($docfile)
1231+
open(my $fh, '<', $docfile)
12331232
or return;
12341233

12351234
require Module::Build::PodParser;
@@ -1289,13 +1288,13 @@ sub read_config {
12891288

12901289
my $file = $self->config_file('build_params')
12911290
or die "Can't find 'build_params' in " . $self->config_dir;
1292-
my $fh = IO::File->new($file) or die "Can't read '$file': $!";
1291+
open(my $fh, '<', $file) or die "Can't read '$file': $!";
12931292
my $ref = eval do {local $/; <$fh>};
12941293
die if $@;
1294+
close $fh;
12951295
my $c;
12961296
($self->{args}, $c, $self->{properties}) = @$ref;
12971297
$self->{config} = Module::Build::Config->new(values => $c);
1298-
close $fh;
12991298
}
13001299

13011300
sub has_config_data {
@@ -1307,13 +1306,14 @@ sub _write_data {
13071306
my ($self, $filename, $data) = @_;
13081307

13091308
my $file = $self->config_file($filename);
1310-
my $fh = IO::File->new("> $file") or die "Can't create '$file': $!";
1309+
open(my $fh, '>', $file) or die "Can't create '$file': $!";
13111310
unless (ref($data)) { # e.g. magicnum
13121311
print $fh $data;
13131312
return;
13141313
}
13151314

13161315
print {$fh} Module::Build::Dumper->_data_dump($data);
1316+
close $fh;
13171317
}
13181318

13191319
sub write_config {
@@ -1832,10 +1832,10 @@ use File::Spec;
18321832
18331833
sub magic_number_matches {
18341834
return 0 unless -e '$q{magic_numfile}';
1835-
local *FH;
1836-
open FH, '$q{magic_numfile}' or return 0;
1837-
my \$filenum = <FH>;
1838-
close FH;
1835+
my \$FH;
1836+
open \$FH, '<','$q{magic_numfile}' or return 0;
1837+
my \$filenum = <\$FH>;
1838+
close \$FH;
18391839
return \$filenum == $magic_number;
18401840
}
18411841
@@ -1972,7 +1972,7 @@ sub create_build_script {
19721972

19731973
$self->log_info("Creating new '$build_script' script for ",
19741974
"'$dist_name' version '$dist_version'\n");
1975-
my $fh = IO::File->new(">$build_script") or die "Can't create '$build_script': $!";
1975+
open(my $fh, '>', $build_script) or die "Can't create '$build_script': $!";
19761976
$self->print_build_script($fh);
19771977
close $fh;
19781978

@@ -2340,7 +2340,7 @@ sub read_modulebuildrc {
23402340
return () unless $modulebuildrc;
23412341
}
23422342

2343-
my $fh = IO::File->new( $modulebuildrc )
2343+
open(my $fh, '<', $modulebuildrc )
23442344
or die "Can't open $modulebuildrc: $!";
23452345

23462346
my %options; my $buffer = '';
@@ -2461,7 +2461,7 @@ sub get_action_docs {
24612461
(my $file = $class) =~ s{::}{/}g;
24622462
# NOTE: silently skipping relative paths if any chdir() happened
24632463
$file = $INC{$file . '.pm'} or next;
2464-
my $fh = IO::File->new("< $file") or next;
2464+
open(my $fh, '<', $file) or next;
24652465
$files_found++;
24662466

24672467
# Code below modified from /usr/bin/perldoc
@@ -3104,7 +3104,7 @@ sub fix_shebang_line { # Adapted from fixin() in ExtUtils::MM_Unix 1.35
31043104

31053105
my ($does_shbang) = $c->get('sharpbang') =~ /^\s*\#\!/;
31063106
for my $file (@files) {
3107-
my $FIXIN = IO::File->new($file) or die "Can't process '$file': $!";
3107+
open(my $FIXIN, '<', $file) or die "Can't process '$file': $!";
31083108
local $/ = "\n";
31093109
chomp(my $line = <$FIXIN>);
31103110
next unless $line =~ s/^\s*\#!\s*//; # Not a shebang file.
@@ -3124,7 +3124,7 @@ eval 'exec $interpreter $arg -S \$0 \${1+"\$\@"}'
31243124
if 0; # not running under some shell
31253125
} unless $self->is_windowsish; # this won't work on win32, so don't
31263126

3127-
my $FIXOUT = IO::File->new(">$file.new")
3127+
open(my $FIXOUT, '>', "$file.new")
31283128
or die "Can't create new $file: $!\n";
31293129

31303130
# Print out the new #! line (or equivalent).
@@ -3321,7 +3321,7 @@ sub contains_pod {
33213321
my ($self, $file) = @_;
33223322
return '' unless -T $file; # Only look at text files
33233323

3324-
my $fh = IO::File->new( $file ) or die "Can't open $file: $!";
3324+
open(my $fh, '<', $file ) or die "Can't open $file: $!";
33253325
while (my $line = <$fh>) {
33263326
return 1 if $line =~ /^\=(?:head|pod|item)/;
33273327
}
@@ -3438,7 +3438,7 @@ sub htmlify_pods {
34383438
join(", ", map { "q{$_} => q{$opts{$_}}" } (keys %opts)) . ") failed: $@");
34393439
} else {
34403440
my $path2root = File::Spec->catdir((File::Spec->updir) x @dirs);
3441-
my $fh = IO::File->new($infile) or die "Can't read $infile: $!";
3441+
open(my $fh, '<', $infile) or die "Can't read $infile: $!";
34423442
my $abstract = Module::Build::PodParser->new(fh => $fh)->get_abstract();
34433443

34443444
my $title = join( '::', (@dirs, $name) );
@@ -3477,9 +3477,9 @@ sub htmlify_pods {
34773477
$errors++;
34783478
next POD;
34793479
}
3480-
my $fh = IO::File->new($tmpfile) or die "Can't read $tmpfile: $!";
3480+
open(my $fh, '<', $tmpfile) or die "Can't read $tmpfile: $!";
34813481
my $html = join('',<$fh>);
3482-
$fh->close;
3482+
close $fh;
34833483
if (!$self->_is_ActivePerl) {
34843484
# These fixups are already done by AP::DT:P:pod2html
34853485
# The output from pod2html is NOT XHTML!
@@ -3494,9 +3494,9 @@ sub htmlify_pods {
34943494
# Fixup links that point to our temp blib
34953495
$html =~ s/\Q$blibdir\E//g;
34963496

3497-
$fh = IO::File->new(">$outfile") or die "Can't write $outfile: $!";
3497+
open($fh, '>', $outfile) or die "Can't write $outfile: $!";
34983498
print $fh $html;
3499-
$fh->close;
3499+
close $fh;
35003500
unlink($tmpfile);
35013501
}
35023502

@@ -3865,12 +3865,12 @@ sub _add_to_manifest {
38653865
my $mode = (stat $manifest)[2];
38663866
chmod($mode | oct(222), $manifest) or die "Can't make $manifest writable: $!";
38673867

3868-
my $fh = IO::File->new("< $manifest") or die "Can't read $manifest: $!";
3868+
open(my $fh, '<', $manifest) or die "Can't read $manifest: $!";
38693869
my $last_line = (<$fh>)[-1] || "\n";
38703870
my $has_newline = $last_line =~ /\n$/;
3871-
$fh->close;
3871+
close $fh;
38723872

3873-
$fh = IO::File->new(">> $manifest") or die "Can't write to $manifest: $!";
3873+
open($fh, '>>', $manifest) or die "Can't write to $manifest: $!";
38743874
print $fh "\n" unless $has_newline;
38753875
print $fh map "$_\n", @$lines;
38763876
close $fh;
@@ -3966,7 +3966,7 @@ HERE
39663966

39673967
$self->delete_filetree('LICENSE');
39683968

3969-
my $fh = IO::File->new('> LICENSE')
3969+
open(my $fh, '>', 'LICENSE')
39703970
or die "Can't write LICENSE file: $!";
39713971
print $fh $license->fulltext;
39723972
close $fh;
@@ -3998,8 +3998,7 @@ EOF
39983998
} elsif ( eval {require Pod::Text; 1} ) {
39993999
$self->log_info("Creating README using Pod::Text\n");
40004000

4001-
my $fh = IO::File->new('> README');
4002-
if ( defined($fh) ) {
4001+
if ( open(my $fh, '>', 'README') ) {
40034002
local $^W = 0;
40044003
no strict "refs";
40054004

@@ -4020,7 +4019,7 @@ EOF
40204019

40214020
Pod::Text::pod2text( $docfile, $fh );
40224021

4023-
$fh->close;
4022+
close $fh;
40244023
} else {
40254024
$self->log_warn(
40264025
"Cannot create 'README' file: Can't open file for writing\n" );
@@ -4214,17 +4213,17 @@ sub _append_maniskip {
42144213
my $skip = shift;
42154214
my $file = shift || 'MANIFEST.SKIP';
42164215
return unless defined $skip && length $skip;
4217-
my $fh = IO::File->new(">> $file")
4216+
open(my $fh, '>>', $file)
42184217
or die "Can't open $file: $!";
42194218

42204219
print $fh "$skip\n";
4221-
$fh->close();
4220+
close $fh;
42224221
}
42234222

42244223
sub _write_default_maniskip {
42254224
my $self = shift;
42264225
my $file = shift || 'MANIFEST.SKIP';
4227-
my $fh = IO::File->new("> $file")
4226+
open(my $fh, '>', $file)
42284227
or die "Can't open $file: $!";
42294228

42304229
my $content = $self->_eumanifest_has_include ? "#!include_default\n"
@@ -4250,6 +4249,8 @@ EOF
42504249
$content .= '\b'.$self->dist_name.'-[\d\.\_]+'."\n";
42514250

42524251
print $fh $content;
4252+
4253+
close $fh;
42534254

42544255
return;
42554256
}
@@ -5413,7 +5414,7 @@ sub compile_xs {
54135414
@typemaps, $file);
54145415

54155416
$self->log_info("@command\n");
5416-
my $fh = IO::File->new("> $args{outfile}") or die "Couldn't write $args{outfile}: $!";
5417+
open(my $fh, '>', $args{outfile}) or die "Couldn't write $args{outfile}: $!";
54175418
print {$fh} $self->_backticks(@command);
54185419
close $fh;
54195420
}
@@ -5542,7 +5543,7 @@ sub process_xs {
55425543
require ExtUtils::Mkbootstrap;
55435544
$self->log_info("ExtUtils::Mkbootstrap::Mkbootstrap('$spec->{bs_file}')\n");
55445545
ExtUtils::Mkbootstrap::Mkbootstrap($spec->{bs_file}); # Original had $BSLOADLIBS - what's that?
5545-
{my $fh = IO::File->new(">> $spec->{bs_file}")} # create
5546+
open(my $fh, '>>', $spec->{bs_file}); # create
55465547
utime((time)x2, $spec->{bs_file}); # touch
55475548
}
55485549

lib/Module/Build/Compat.pm

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ $VERSION = '0.4005';
66

77
use File::Basename ();
88
use File::Spec;
9-
use IO::File;
109
use Config;
1110
use Module::Build;
1211
use Module::Build::ModuleInfo;
@@ -123,7 +122,7 @@ HERE
123122
$args{file} ||= 'Makefile.PL';
124123
local $build->{properties}{quiet} = 1;
125124
$build->delete_filetree($args{file});
126-
$fh = IO::File->new("> $args{file}") or die "Can't write $args{file}: $!";
125+
open($fh, '>', "$args{file}") or die "Can't write $args{file}: $!";
127126
}
128127

129128
print {$fh} "# Note: this file was auto-generated by ", __PACKAGE__, " version $VERSION\n";
@@ -406,7 +405,7 @@ EOF
406405

407406
sub fake_prereqs {
408407
my $file = File::Spec->catfile('_build', 'prereqs');
409-
my $fh = IO::File->new("< $file") or die "Can't read $file: $!";
408+
open(my $fh, '<', "$file") or die "Can't read $file: $!";
410409
my $prereqs = eval do {local $/; <$fh>};
411410
close $fh;
412411

lib/Module/Build/Notes.pm

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use vars qw($VERSION);
77
$VERSION = '0.4005';
88
$VERSION = eval $VERSION;
99
use Data::Dumper;
10-
use IO::File;
1110
use Module::Build::Dumper;
1211

1312
sub new {
@@ -24,9 +23,10 @@ sub new {
2423
sub restore {
2524
my $self = shift;
2625

27-
my $fh = IO::File->new("< $self->{file}") or die "Can't read $self->{file}: $!";
26+
open(my $fh, '<', $self->{file}) or die "Can't read $self->{file}: $!";
2827
$self->{disk} = eval do {local $/; <$fh>};
2928
die $@ if $@;
29+
close $fh;
3030
$self->{new} = {};
3131
}
3232

@@ -107,8 +107,9 @@ sub write {
107107
sub _dump {
108108
my ($self, $file, $data) = @_;
109109

110-
my $fh = IO::File->new("> $file") or die "Can't create '$file': $!";
110+
open(my $fh, '>', $file) or die "Can't create '$file': $!";
111111
print {$fh} Module::Build::Dumper->_data_dump($data);
112+
close $fh;
112113
}
113114

114115
my $orig_template = do { local $/; <DATA> };
@@ -127,11 +128,11 @@ sub write_config_data {
127128
# recognized for *this* source file
128129
$template =~ s{$_\n}{} for '=begin private', '=end private';
129130

130-
my $fh = IO::File->new("> $args{file}") or die "Can't create '$args{file}': $!";
131+
open(my $fh, '>', $args{file}) or die "Can't create '$args{file}': $!";
131132
print {$fh} $template;
132133
print {$fh} "\n__DATA__\n";
133134
print {$fh} Module::Build::Dumper->_data_dump([$args{config_data}, $args{feature}, $args{auto_features}]);
134-
135+
close $fh;
135136
}
136137

137138
1;
@@ -188,15 +189,14 @@ sub config_names { keys %$config }
188189
189190
sub write {
190191
my $me = __FILE__;
191-
require IO::File;
192192
193193
# Can't use Module::Build::Dumper here because M::B is only a
194194
# build-time prereq of this module
195195
require Data::Dumper;
196196
197197
my $mode_orig = (stat $me)[2] & 07777;
198198
chmod($mode_orig | 0222, $me); # Make it writeable
199-
my $fh = IO::File->new($me, 'r+') or die "Can't rewrite $me: $!";
199+
open(my $fh, '+<', $me) or die "Can't rewrite $me: $!";
200200
seek($fh, 0, 0);
201201
while (<$fh>) {
202202
last if /^__DATA__$/;
@@ -205,11 +205,11 @@ sub write {
205205
206206
seek($fh, tell($fh), 0);
207207
my $data = [$config, $features, $auto_features];
208-
$fh->print( 'do{ my '
208+
print($fh 'do{ my '
209209
. Data::Dumper->new([$data],['x'])->Purity(1)->Dump()
210210
. '$x; }' );
211211
truncate($fh, tell($fh));
212-
$fh->close;
212+
close $fh;
213213
214214
chmod($mode_orig, $me)
215215
or warn "Couldn't restore permissions on $me: $!";

0 commit comments

Comments
 (0)