Skip to content

Commit 6b85796

Browse files
committed
Reduce usage of bareword file handles
Barewords in Perl are package level and may be interpreted as subroutine calls if a subroutine with a name colliding with the file handle's name is declared in the package.
1 parent 62ac34a commit 6b85796

File tree

13 files changed

+88
-101
lines changed

13 files changed

+88
-101
lines changed

Build.pm

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,9 @@ sub find_config_file {
336336

337337
sub slurp_config_file {
338338
my ($file, $seen) = @_;
339-
local *CONF;
340-
die("$file: $!\n") unless open(CONF, '<', $file);
341-
my @config = <CONF>;
342-
close CONF;
339+
die("$file: $!\n") unless open(my $CONF, '<', $file);
340+
my @config = <$CONF>;
341+
close $CONF;
343342
chomp @config;
344343
if (@config && $config[0] =~ /^#!PrependConfigFile:\s*([^\.\/][^\/]*?)\s*$/) {
345344
my $otherfile = $1;
@@ -379,10 +378,9 @@ sub read_config {
379378
if (ref($cfile)) {
380379
@config = @$cfile;
381380
} elsif (defined($cfile)) {
382-
local *CONF;
383-
return undef unless open(CONF, '<', $cfile);
384-
@config = <CONF>;
385-
close CONF;
381+
return undef unless open(my $CONF, '<', $cfile);
382+
@config = <$CONF>;
383+
close $CONF;
386384
chomp @config;
387385
}
388386
# create verbatim macro blobs

Build/Arch.pm

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ sub get_assets {
8787
sub parse {
8888
my ($config, $pkgbuild) = @_;
8989
my $ret;
90-
local *PKG;
91-
if (!open(PKG, '<', $pkgbuild)) {
90+
my $PKG;
91+
if (!open($PKG, '<', $pkgbuild)) {
9292
$ret->{'error'} = "$pkgbuild: $!";
9393
return $ret;
9494
}
9595
my %vars;
9696
my @ifs;
97-
while (<PKG>) {
97+
while (<$PKG>) {
9898
chomp;
9999
next if /^\s*$/;
100100
next if /^\s*#/;
@@ -127,7 +127,7 @@ sub parse {
127127
my $val = $4;
128128
if ($3) {
129129
while ($val !~ s/\)\s*(?:#.*)?$//s) {
130-
my $nextline = <PKG>;
130+
my $nextline = <$PKG>;
131131
last unless defined $nextline;
132132
chomp $nextline;
133133
$val .= ' ' . $nextline;
@@ -139,7 +139,7 @@ sub parse {
139139
$vars{$var} = [ unquotesplit($val, \%vars) ];
140140
}
141141
}
142-
close PKG;
142+
close $PKG;
143143
$ret->{'name'} = $vars{'pkgname'}->[0] if $vars{'pkgname'};
144144
$ret->{'version'} = $vars{'pkgver'}->[0] if $vars{'pkgver'};
145145
$ret->{'deps'} = [];
@@ -170,21 +170,19 @@ sub parse {
170170

171171
sub islzma {
172172
my ($fn) = @_;
173-
local *F;
174-
return 0 unless open(F, '<', $fn);
173+
return 0 unless open(my $F, '<', $fn);
175174
my $h;
176-
return 0 unless read(F, $h, 5) == 5;
177-
close F;
175+
return 0 unless read($F, $h, 5) == 5;
176+
close $F;
178177
return $h eq "\3757zXZ";
179178
}
180179

181180
sub iszstd {
182181
my ($fn) = @_;
183-
local *F;
184-
return 0 unless open(F, '<', $fn);
182+
return 0 unless open(my $F, '<', $fn);
185183
my $h;
186-
return 0 unless read(F, $h, 4) == 4;
187-
close F;
184+
return 0 unless read($F, $h, 4) == 4;
185+
close $F;
188186
return $h eq "(\265\057\375";
189187
}
190188

@@ -367,17 +365,15 @@ sub queryinstalled {
367365
my ($root, %opts) = @_;
368366

369367
$root = '' if !defined($root) || $root eq '/';
370-
local *D;
371-
local *F;
372-
opendir(D, "$root/var/lib/pacman/local") || return [];
373-
my @pn = sort(grep {!/^\./} readdir(D));
374-
closedir(D);
368+
opendir(my $D, "$root/var/lib/pacman/local") || return [];
369+
my @pn = sort(grep {!/^\./} readdir($D));
370+
closedir($D);
375371
my @pkgs;
376372
for my $pn (@pn) {
377-
next unless open(F, '<', "$root/var/lib/pacman/local/$pn/desc");
373+
next unless open(my $F, '<', "$root/var/lib/pacman/local/$pn/desc");
378374
my $data = '';
379-
1 while sysread(F, $data, 8192, length($data));
380-
close F;
375+
1 while sysread($F, $data, 8192, length($data));
376+
close $F;
381377
my $d = parserepodata(undef, $data);
382378
next unless defined $d->{'name'};
383379
my $q = {};

Build/Collax.pm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ sub parse {
2424
} elsif (ref($fn) ne "") {
2525
die "Unhandled ref type in collax";
2626
} else {
27-
local *FH;
28-
if (!open(FH, "<", $fn)) {
27+
my $FH;
28+
if (!open($FH, "<", $fn)) {
2929
return {"error" => "$fn: $!"};
3030
}
31-
@bscript = <FH>;
31+
@bscript = <$FH>;
3232
chomp(@bscript);
33-
close(FH);
33+
close($FH);
3434
}
3535

3636
my $ret = {"deps" => []};

Build/Deb.pm

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,9 @@ sub parse {
8181
if (ref($fn) eq 'ARRAY') {
8282
@control = @$fn;
8383
} else {
84-
local *F;
85-
return { 'error' => "$fn: $!" } unless open(F, '<', $fn);
86-
@control = <F>;
87-
close F;
84+
return { 'error' => "$fn: $!" } unless open(my $F, '<', $fn);
85+
@control = <$F>;
86+
close $F;
8887
chomp @control;
8988
}
9089
splice(@control, 0, 3) if @control > 3 && $control[0] =~ /^-----BEGIN/;

Build/Docker.pm

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ sub gettargetarch {
4141

4242
sub slurp {
4343
my ($fn) = @_;
44-
local *F;
45-
return undef unless open(F, '<', $fn);
44+
return undef unless open(my $F, '<', $fn);
4645
local $/ = undef; # Perl slurp mode
47-
my $content = <F>;
48-
close F;
46+
my $content = <$F>;
47+
close $F;
4948
return $content;
5049
}
5150

PBuild/BuildResult.pm

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ sub read_bininfo {
4141
my ($dir, $withid) = @_;
4242
my $bininfo;
4343
my @bininfo_s;
44-
local *BI;
45-
if (open(BI, '<', "$dir/.bininfo")) {
46-
@bininfo_s = stat(BI);
47-
$bininfo = PBuild::Util::retrieve(\*BI, 1) if @bininfo_s && $bininfo_s[7];
48-
close BI;
44+
if (open(my $BI, '<', "$dir/.bininfo")) {
45+
@bininfo_s = stat($BI);
46+
$bininfo = PBuild::Util::retrieve($BI, 1) if @bininfo_s && $bininfo_s[7];
47+
close $BI;
4948
if ($bininfo) {
5049
$bininfo->{'.bininfo'} = {'id' => "$bininfo_s[9]/$bininfo_s[7]/$bininfo_s[1]"} if $withid;
5150
return $bininfo;
@@ -65,13 +64,12 @@ sub read_bininfo {
6564
my $r = {%$d, 'filename' => $file, 'id' => "$s[9]/$s[7]/$s[1]"};
6665
$bininfo->{$file} = $r;
6766
} elsif ($file =~ /[-.]appdata\.xml$/) {
68-
local *F;
69-
open(F, '<', "$dir/$file") || next;
70-
my @s = stat(F);
67+
open(my $F, '<', "$dir/$file") || next;
68+
my @s = stat($F);
7169
next unless @s;
7270
my $ctx = Digest::MD5->new;
73-
$ctx->addfile(*F);
74-
close F;
71+
$ctx->addfile($F);
72+
close $F;
7573
$bininfo->{$file} = {'md5sum' => $ctx->hexdigest(), 'filename' => $file, 'id' => "$s[9]/$s[7]/$s[1]"};
7674
}
7775
next;

PBuild/Checker.pm

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -598,10 +598,10 @@ sub check {
598598
my $mylastcheck = $lastcheck->{$packid};
599599
my @meta;
600600
if (!@meta_s || !$mylastcheck || substr($mylastcheck, 96) ne "$meta_s[9]/$meta_s[7]/$meta_s[1]") {
601-
if (open(F, '<', "$dst/_meta")) {
602-
@meta_s = stat F;
603-
@meta = <F>;
604-
close F;
601+
if (open(my $F, '<', "$dst/_meta")) {
602+
@meta_s = stat $F;
603+
@meta = <$F>;
604+
close $F;
605605
chomp @meta;
606606
$mylastcheck = substr($meta[0], 0, 32);
607607
if (@meta == 2 && $meta[1] =~ /^fake/) {
@@ -661,9 +661,9 @@ sub check {
661661
goto relsynccheck;
662662
}
663663
# something changed, read in old meta (if not already done)
664-
if (!@meta && open(F, '<', "$dst/_meta")) {
665-
@meta = <F>;
666-
close F;
664+
if (!@meta && open(my $F, '<', "$dst/_meta")) {
665+
@meta = <$F>;
666+
close $F;
667667
chomp @meta;
668668
}
669669
if ($incycle == 1) {

PBuild/Container.pm

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,16 @@ sub containerinfo2obsbinlnk {
5555
}
5656
eval { PBuild::Verify::verify_nevraquery($lnk); PBuild::Verify::verify_filename($d->{'file'}) };
5757
return undef if $@;
58-
local *F;
5958
if ($d->{'tar_md5sum'}) {
6059
# this is a normalized container
6160
$lnk->{'hdrmd5'} = $d->{'tar_md5sum'};
6261
$lnk->{'lnk'} = $d->{'file'};
6362
return $lnk;
6463
}
65-
return undef unless open(F, '<', "$dir/$d->{'file'}");
64+
return undef unless open(my $F, '<', "$dir/$d->{'file'}");
6665
my $ctx = Digest::MD5->new;
67-
$ctx->addfile(*F);
68-
close F;
66+
$ctx->addfile($F);
67+
close $F;
6968
$lnk->{'hdrmd5'} = $ctx->hexdigest();
7069
$lnk->{'lnk'} = $d->{'file'};
7170
return $lnk;

createdirdeps

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,20 @@ sub queryfromfilename {
4848
my ($opts, @args) = Build::Options::parse_options($options, @ARGV);
4949

5050
my %old;
51-
if (defined($opts->{'oldfile'}) && open(F, '<', $opts->{'oldfile'})) {
52-
while (<F>) {
51+
if (defined($opts->{'oldfile'}) && open(my $F, '<', $opts->{'oldfile'})) {
52+
while (<$F>) {
5353
chomp;
5454
$old{$1} = $_ if /^([PRrCOI]:[^ ]+): /;
5555
}
56-
close F;
56+
close $F;
5757
}
5858

5959
my %seen;
6060

6161
for my $dir (@args) {
6262
my $cmd = "find $dir -follow -type f \\( -name \"*.rpm\" -o -name \"*.deb\" -o -name \"*.pkg.tar.gz\" -o -name \"*.pkg.tar.xz\" \\) -a ! -name \"*src.rpm\" -printf '\%T@/\%s/\%i \%p\\n'";
63-
open(F, '-|', $cmd) or next;
64-
while (<F>) {
63+
open(my $F, '-|', $cmd) or next;
64+
while (<$F>) {
6565
chomp;
6666
next unless /^([\d\.]+\/\d+\/\d+) (.*)$/;
6767
my $id = $1;
@@ -94,6 +94,6 @@ for my $dir (@args) {
9494
$q->{'location'} = $path;
9595
Build::writedeps(\*STDOUT, $q);
9696
}
97-
close F;
97+
close $F;
9898
}
9999

killchroot

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ my %pids;
6565
my $pid;
6666
my $path;
6767

68-
opendir(D, "/proc") || die("/proc: $!\n");
69-
for $pid (readdir(D)) {
68+
opendir(my $D, "/proc") || die("/proc: $!\n");
69+
for $pid (readdir($D)) {
7070
next unless $pid =~ /^\d+$/;
7171
$path = readlink("/proc/$pid/root");
7272
next unless defined $path;
@@ -78,7 +78,7 @@ for $pid (readdir(D)) {
7878
$pids{$pid} = 1;
7979
}
8080
}
81-
closedir(D);
81+
closedir($D);
8282

8383
my @pids = sort keys %pids;
8484
exit 0 unless @pids;
@@ -88,10 +88,10 @@ print "$msg\n" if $msg ne '';
8888
if ($verbose) {
8989
my @pidsv = ();
9090
for $pid (@pids) {
91-
open(F, "</proc/$pid/cmdline");
91+
open(my $F, "</proc/$pid/cmdline");
9292
my $cmd = '';
93-
sysread(F, $cmd, 128);
94-
close F;
93+
sysread($F, $cmd, 128);
94+
close $F;
9595
$cmd =~ s/\0.*//;
9696
#$cmd =~ s/ .*//;
9797
push @pidsv, "$pid($cmd)";

0 commit comments

Comments
 (0)