From 746386b69b63799e3fd938c5b64fe3712fd717e1 Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Wed, 20 Mar 2024 20:51:58 +0800 Subject: [PATCH] ar: add strict (#510) * ar was mostly compliant with strict except for option variables * Instead of summing $opt_d, $opt_m etc (which might be undefined), count options using a list * Don't access $ARGV[0] if it doesn't exist * test1: "perl ar" --> invalid: need a major option to determine mode * test2: "perl ar x notfound" --> valid: equivalent to ar -x notfound * test3: "perl ar -x -m" --> invalid: -m and -x are not compatible --- bin/ar | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/bin/ar b/bin/ar index eafd1974..c52341b5 100755 --- a/bin/ar +++ b/bin/ar @@ -11,6 +11,8 @@ License: perl =cut +use strict; + use POSIX qw(strftime); use File::Basename; use FileHandle; @@ -18,15 +20,18 @@ use Getopt::Std qw(getopts); use constant MAGIC => "!\n"; +use vars qw($opt_d $opt_m $opt_p $opt_q $opt_r $opt_t $opt_x $opt_a $opt_b + $opt_c $opt_i $opt_o $opt_u $opt_v); + # allow the first arg to not have a '-'. -if ($ARGV[0] !~ /^\-/) { $ARGV[0] = '-' . $ARGV[0]; } +if (@ARGV && $ARGV[0] !~ /^\-/) { $ARGV[0] = '-' . $ARGV[0]; } getopts('dmpqrtxabciouv') or usage(); # take only one of the following major opts -if (($opt_d + $opt_m + $opt_p + $opt_q + $opt_r + $opt_t + $opt_x) != 1) { - usage(); -} +my @major = map { $_ || () } ($opt_d, $opt_m, $opt_p, $opt_q, $opt_r, + $opt_t, $opt_x); +usage() unless scalar(@major) == 1; # -i is the same as -b $opt_b |= $opt_i;