pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc/pkgtools/lintpkgsrc/files lintpkgsrc: cleanup



details:   https://anonhg.NetBSD.org/pkgsrc/rev/552520bc2fd7
branches:  trunk
changeset: 382980:552520bc2fd7
user:      rillig <rillig%pkgsrc.org@localhost>
date:      Wed Aug 10 07:12:52 2022 +0000

description:
lintpkgsrc: cleanup

Mostly remove redundant punctuation around 'push' and 'defined'.

diffstat:

 pkgtools/lintpkgsrc/files/lintpkgsrc.pl      |  252 ++++++++++++---------------
 pkgtools/lintpkgsrc/files/t/parse_makefile.t |   20 ++-
 2 files changed, 131 insertions(+), 141 deletions(-)

diffs (truncated from 652 to 300 lines):

diff -r 13c4e7dc8d56 -r 552520bc2fd7 pkgtools/lintpkgsrc/files/lintpkgsrc.pl
--- a/pkgtools/lintpkgsrc/files/lintpkgsrc.pl   Wed Aug 10 06:08:15 2022 +0000
+++ b/pkgtools/lintpkgsrc/files/lintpkgsrc.pl   Wed Aug 10 07:12:52 2022 +0000
@@ -1,6 +1,6 @@
 #!@PERL5@
 
-# $NetBSD: lintpkgsrc.pl,v 1.68 2022/08/10 06:08:15 rillig Exp $
+# $NetBSD: lintpkgsrc.pl,v 1.69 2022/08/10 07:12:52 rillig Exp $
 
 # Written by David Brownlee <abs%netbsd.org@localhost>.
 #
@@ -52,13 +52,13 @@
 }
 
 sub var($self, $key, $val = undef) {
-       (defined $val)
+       defined $val
            ? ($self->{vars}->{$key} = $val)
            : $self->{vars}->{$key};
 }
 
 sub vars($self) {
-       keys %{$self->{vars}};
+       keys $self->{vars}->%*;
 }
 
 # Pkgs collects all versions of a given PKGBASE, e.g. apache-1.3.27 and
@@ -82,7 +82,7 @@
 # Returns all available versions of the package, in decreasing
 # alphabetical(!) order.
 sub versions($self) {
-       return sort { $b cmp $a } keys %{$self->{pkgvers}};
+       reverse sort keys $self->{pkgvers}->%*;
 }
 
 sub add($self, $pkgbase, $pkgversion) {
@@ -95,10 +95,9 @@
 # pkgver($pkgversion) returns the PkgVer, or undef.
 sub pkgver($self, $pkgversion = undef) {
        my $pkgvers = $self->{pkgvers};
-       if (@_ > 1) {
-               return $pkgvers->{$pkgversion};
-       }
-       return sort { $b->pkgversion cmp $a->pkgversion } values %{$pkgvers};
+       defined $pkgversion
+           ? $pkgvers->{$pkgversion}
+           : sort { $b->pkgversion cmp $a->pkgversion } values %$pkgvers;
 }
 
 # XXX: Returns the alphabetically(!) highest PkgVer.
@@ -116,14 +115,12 @@
 }
 
 sub add($self, $pkgbase, $pkgversion) {
-       if (!$self->{$pkgbase}) {
-               $self->{$pkgbase} = Pkgs->new($pkgbase);
-       }
-       $self->{$pkgbase}->add($pkgbase, $pkgversion);
+       my $pkgs = ($self->{$pkgbase} ||= Pkgs->new($pkgbase));
+       $pkgs->add($pkgbase, $pkgversion);
 }
 
 sub numpkgver($self) {
-       scalar($self->pkgver);
+       scalar $self->pkgver;
 }
 
 # pkgver() returns all PkgVers, sorted by pkgbase, then by version in
@@ -137,29 +134,26 @@
        if (!defined $pkgbase) {
                my (@pkgvers);
                foreach my $pkg ($self->pkgs) {
-                       push(@pkgvers, $pkg->pkgver);
+                       push @pkgvers, $pkg->pkgver;
                }
                return @pkgvers;
        }
 
        my $pkgs = $self->{$pkgbase};
-       if (defined $pkgs) {
-               return defined $pkgversion
-                   ? $pkgs->pkgver($pkgversion)
-                   : $pkgs->pkgver();
-       }
-       return;
+       defined $pkgs && defined $pkgversion
+           ? $pkgs->pkgver($pkgversion)
+           : defined $pkgs
+           ? $pkgs->pkgver
+           : ();
 }
 
 # pkgs() returns all Pkgs, sorted by pkgbase.
 #
 # pkgs($pkgbase) returns the Pkgs, or undef.
 sub pkgs($self, $pkgbase = undef) {
-       if (defined $pkgbase) {
-               return $self->{$pkgbase};
-       } else {
-               return sort { $a->pkgbase cmp $b->pkgbase } values %$self;
-       }
+       defined $pkgbase
+           ? $self->{$pkgbase}
+           : sort { $a->pkgbase cmp $b->pkgbase } values %$self;
 }
 
 package main;
@@ -185,13 +179,11 @@
 my $magic_undefined = 'M_a_G_i_C_uNdEfInEd';
 
 sub debug(@) {
-       ($opt{D}) && print STDERR 'DEBUG: ', @_;
+       $opt{D} and print STDERR 'DEBUG: ', @_;
 }
 
 sub verbose(@) {
-       if (-t STDERR) {
-               print STDERR @_;
-       }
+       -t STDERR and print STDERR @_;
 }
 
 sub fail($msg) {
@@ -208,7 +200,7 @@
        my (@list, @thislist);
 
        $thisdir = $base;
-       if (defined($dir)) {
+       if (defined $dir) {
                $thisdir .= "/$dir";
                $dir .= '/';
        } else {
@@ -216,13 +208,13 @@
        }
 
        opendir(DIR, $thisdir) || fail("Unable to opendir($thisdir): $!");
-       @thislist = grep(substr($_, 0, 1) ne '.' && $_ ne 'CVS', readdir(DIR));
+       @thislist = grep { substr($_, 0, 1) ne '.' && $_ ne 'CVS' } readdir(DIR);
        closedir(DIR);
        foreach my $entry (@thislist) {
                if (-d "$thisdir/$entry") {
-                       push(@list, listdir($base, "$dir$entry"));
+                       push @list, listdir($base, "$dir$entry");
                } else {
-                       push(@list, "$dir$entry");
+                       push @list, "$dir$entry";
                }
        }
        @list;
@@ -239,22 +231,20 @@
        $pkgversion = lc($pkgversion);
 
        # See pkgtools/pkg_install/files/lib/dewey.c.
-       my (@temp);
-       push(@temp, ($pkgversion =~ s/nb(\d+)//) ? +$1 : 0);
+       my @temp = ($pkgversion =~ s/nb(\d+)//) ? +$1 : 0;
        foreach my $elem (split(/(pl|pre|rc|beta|alpha|\D)/, $pkgversion)) {
                if ($elem =~ /\d/) {
-                       push(@temp, +$elem);
+                       push @temp, +$elem;
                } elsif ($elem eq 'pl' || $elem eq '.' || $elem eq '_') {
-                       push(@temp, 0);
+                       push @temp, 0;
                } elsif ($elem eq 'pre' || $elem eq 'rc') {
-                       push(@temp, -1);
+                       push @temp, -1;
                } elsif ($elem eq 'beta') {
-                       push(@temp, -2);
+                       push @temp, -2;
                } elsif ($elem eq 'alpha') {
-                       push(@temp, -3);
+                       push @temp, -3;
                } elsif ('a' le $elem && $elem le 'z') {
-                       push(@temp, 0);
-                       push(@temp, ord($elem) - ord('a') + 1);
+                       push @temp, 0, ord($elem) - ord('a') + 1;
                }
        }
        @temp;
@@ -270,26 +260,19 @@
        }
        $cmp ||= $nb_a <=> $nb_b;
 
-       if ($op eq '<') {
-               $cmp < 0;
-       } elsif ($op eq '<=') {
-               $cmp <= 0;
-       } elsif ($op eq '>') {
-               $cmp > 0;
-       } else {
-               $cmp >= 0;
-       }
+       $op eq '<' ? $cmp < 0
+           : $op eq '<=' ? $cmp <= 0
+           : $op eq '>' ? $cmp > 0
+           : $cmp >= 0;
 }
 
 # Return a copy of $value in which trivial variable expressions are replaced
 # with their variable values.
 sub expand_var($value, $vars) {
        while ($value =~ /\$\{([-\w.]+)\}/) {
-               if (defined($vars->{$1})) {
-                       $value = $` . $vars->{$1} . $';
-               } else {
-                       $value = $` . $magic_undefined . $';
-               }
+               $value = defined $vars->{$1}
+                   ? "$`$vars->{$1}$'"
+                   : "$`$magic_undefined$'";
        }
        $value;
 }
@@ -305,7 +288,6 @@
 
        debug("conditional: $test\n");
 
-       # XXX Could do something with target
        while ($test =~ /(target|empty|make|defined|exists)\s*\(([^()]+)\)/) {
                my ($testname, $varname) = ($1, $2);
                my $var;
@@ -336,15 +318,16 @@
                }
 
                if ($testname eq 'exists') {
-                       $_ = (-e $varname) ? 1 : 0;
+                       $_ = -e $varname ? 1 : 0;
 
                } elsif ($testname eq 'defined') {
-                       $_ = defined($var) ? 1 : 0;
+                       $_ = defined $var ? 1 : 0;
 
                } elsif ($testname eq 'empty') {
-                       $_ = ((not defined($var) or (length($var) == 0)) ? 1 : 0);
+                       $_ = !defined $var || $var eq '' ? 1 : 0;
 
                } else {
+                       # XXX Could do something with target
                        $_ = 0;
                }
 
@@ -354,9 +337,9 @@
 
        while ($test =~ /([^\s()\|\&]+)\s+(!=|==)\s+([^\s()]+)/) {
                if ($2 eq '==') {
-                       $_ = ($1 eq $3) ? 1 : 0;
+                       $_ = $1 eq $3 ? 1 : 0;
                } else {
-                       $_ = ($1 ne $3) ? 1 : 0;
+                       $_ = $1 ne $3 ? 1 : 0;
                }
                $test =~ s/[^\s()\|\&]+\s+(!=|==)\s+[^\s()]+/$_/;
        }
@@ -427,12 +410,12 @@
 
        my $NEWCURDIR = $incfile;
        $NEWCURDIR =~ s#/[^/]*$##;
-       push(@$incdirs, $NEWCURDIR)
+       push @$incdirs, $NEWCURDIR
            unless grep { $_ eq $NEWCURDIR } @$incdirs;
-       unshift(@$lines, ".CURDIR=" . $vars->{'.CURDIR'});
+       unshift @$lines, ".CURDIR=" . $vars->{'.CURDIR'};
        chomp(my @inc_lines = <FILE>);
-       unshift(@$lines, @inc_lines);
-       unshift(@$lines, ".CURDIR=$NEWCURDIR");
+       unshift @$lines, @inc_lines;
+       unshift @$lines, ".CURDIR=$NEWCURDIR";
        close(FILE);
 }
 
@@ -472,8 +455,9 @@
 
        debug("$file: substitutelist $varname ($result) $subvar (@mods)\n");
        foreach (@mods) {
-               if (m#(U)(.*)#) {
-                       $result ||= $2;
+               # FIXME: Add '^' anchor.
+               if (m# (U) (.*) #x) {
+                       $result ||= "fallback:$2";
                } elsif (m# ([CS]) (.) ([^/\@]+) \2 ([^/\@]*) \2 ([1g]*) #x) {
                        # TODO: Use non-greedy repetitions above.
                        # TODO: Properly handle separators other than '/' and '@'.
@@ -525,8 +509,8 @@
        chomp(@lines = <FILE>);
        close(FILE);
 
-       push(@incdirs, '.');
-       push(@incdirs, dirname($file));
+       push @incdirs, '.';
+       push @incdirs, dirname($file);
 
        # Some Makefiles depend on these being set
        if ($file eq '/etc/mk.conf') {
@@ -544,7 +528,7 @@
                $vars{'.CURDIR'} = getcwd;
        }
 
-       push(@incdirs, $vars{'.CURDIR'});
+       push @incdirs, $vars{'.CURDIR'};
        if ($opt{L}) {



Home | Main Index | Thread Index | Old Index