fix perlcritic's warning
authorHuang Hao <hao.h.huang@intel.com>
Tue, 25 Sep 2012 08:05:22 +0000 (16:05 +0800)
committerHuang Hao <hao.h.huang@intel.com>
Thu, 27 Sep 2012 03:15:07 +0000 (11:15 +0800)
eliminate warning of serverity 5 reported by perlcritic

- code after "use strict"
- default value for $ENV{VIRTUAL_ENV}
- use lexical file handler instead of bareword file handler
- use three-arguments of open
- use "return" instead of "return undef", the latter could evalute
    to one-element list (undef) in list context

depanneur

index ce7371fafc281c8e3c77ad000a61b2441a8abd4a..8ebb3c3aaf446a492744304b13d11e04bee4ceab 100755 (executable)
--- a/depanneur
+++ b/depanneur
@@ -1,16 +1,18 @@
 #!/usr/bin/perl
 #
+use strict;
+use warnings;
+
 BEGIN {
   my ($wd) = $0 =~ m-(.*)/- ;
   $wd ||= '.';
+  my $virtual_env = $ENV{VIRTUAL_ENV} || '';
   unshift @INC,  "$wd/build";
   unshift @INC,  "$wd";
-  unshift @INC,  "$ENV{'VIRTUAL_ENV'}/usr/lib/build"
+  unshift @INC,  "$virtual_env/usr/lib/build"
 }
 
-use strict;
 use YAML qw(LoadFile);
-use warnings;
 use threads;
 use threads::shared;
 use File::Find ();
@@ -283,10 +285,10 @@ sub mkdir_p {
 
 if ( $exclude_from_file ne "" && -e $exclude_from_file ) {
     debug("using $exclude_from_file for package exclusion");
-    open FILE, "<", $exclude_from_file  or die $!;
-    @exclude = <FILE>;
+    open my $file, '<', $exclude_from_file  or die $!;
+    @exclude = <$file>;
     chomp(@exclude);
-    close(FILE);
+    close($file);
 }
 
 
@@ -397,34 +399,35 @@ sub prepare_git {
         }else{
             $commit_id = $commit;
         }
-        open(GIT,"git --git-dir $base/.git rev-parse  $commit_id|") || die "Failed: $!\n";
-        while (my $current_rev = <GIT>) {
+        open(my $git, '-|', "git --git-dir $base/.git rev-parse  $commit_id") || die "Failed: $!\n";
+        while (my $current_rev = <$git>) {
             chomp($current_rev);
             $cache_rev = $current_rev;
             if ( my_system("grep -rq $current_rev $cache_path") == 0 ) {
-               open(GREP,"grep -rl $current_rev $cache_path |") || die "Failed: $!\n";
-               while ( <GREP> ) {
+               open(my $grep, '-|', "grep -rl $current_rev $cache_path") || die "Failed: $!\n";
+               while ( <$grep> ) {
                     chomp;
                    $old_base = $_;
                }
-               close(GREP);
+               close($grep);
                $skip = 1;
             }
             my @lines = ();
+            my $rev;
             if ( -e "$cache_path/$pkg_name-$pkg_version-$pkg_release" ) {
-                open (REV, "< $cache_path/$pkg_name-$pkg_version-$pkg_release");
-                @lines = <REV>;
+                open($rev, '<', "$cache_path/$pkg_name-$pkg_version-$pkg_release");
+                @lines = <$rev>;
             } else {
-                open (REV, "> $cache_path/$pkg_name-$pkg_version-$pkg_release");
+                open($rev, '>', "$cache_path/$pkg_name-$pkg_version-$pkg_release");
             }
             foreach my $old_rev(@lines) {
                 if ( $current_rev eq $old_rev ) {
                     $skip = 1;
                 }
             }
-            close (REV);
+            close($rev);
         }
-        close(GIT);
+        close($git);
     } else {
         debug("not a git repo: $base/.git!!");
     }
@@ -466,9 +469,9 @@ sub prepare_git {
             }
             # Set cache_rev as 'include-all' if --include-all specified
             $cache_rev = "include-all" if ($includeall == 1);
-            open (REV1, "+> $cache_path/$pkg_name-$pkg_version-$pkg_release");
-            print REV1 $cache_rev . "\n";
-            close (REV1);
+            open(my $rev1, "+>", "$cache_path/$pkg_name-$pkg_version-$pkg_release");
+            print $rev1 $cache_rev . "\n";
+            close($rev1);
             push(@packs, "$pkg_path/$pkg_name-$pkg_version-$pkg_release/$spec_file");
         } else {
             unlink "$cache_path/$pkg_name-$pkg_version-$pkg_release";
@@ -518,10 +521,10 @@ sub expand_deps {
 
     my %packs_arch;
     my %packs_done;
-    open(F, '<', $rpmdeps) || die("$rpmdeps: $!\n");
+    open(my $fh, '<', $rpmdeps) || die("$rpmdeps: $!\n");
     # WARNING: the following code assumes that the 'I' tag comes last
     my ($pkgF, $pkgP, $pkgR);
-    while(<F>) {
+    while(<$fh>) {
       chomp;
       if (/^F:(.*?)-\d+\/\d+\/\d+: (.*)$/) {
         $pkgF = $2;
@@ -560,7 +563,7 @@ sub expand_deps {
         %packs_done = %ids;
       }
     }
-    close F;
+    close $fh;
 
     for my $arch (@archs) {
       $packs{$_} ||= "$_.$arch" for @{$packs_arch{$arch} || []};
@@ -686,7 +689,7 @@ sub source_of {
             return $x;
         }
     }
-    return undef;
+    return;
 }
 
 sub update_pkgdeps
@@ -949,10 +952,10 @@ info("parsing package data...");
 my %packs = parse_packs($config, @packs);
 
 if ($binarylist ne "" && -e $binarylist ) {
-    open FILE, "<", $binarylist or die $!;
-    my @bins = <FILE>;
+    open my $file, "<", $binarylist or die $!;
+    my @bins = <$file>;
     chomp(@bins);
-    close(FILE);
+    close($file);
     my @alldeps = ();
     foreach my $b (@bins) {
         next if $b eq "";
@@ -1003,7 +1006,7 @@ if ($binarylist ne "" && -e $binarylist ) {
     #print "\n";
     foreach (@allbins) {
         my $so = source_of($_, %packs);
-        if ( defined($so)) {
+        if (defined($so)) {
             push(@tobuild, $so);
         }
     }