in reply to Looking for feedback on a script
die("Usage: $0 [-c] [-v] <-f pattern-file | subnet/netmask> file [file +...]\n") unless ( (defined $#ARGV && $#ARGV > -1) || (defined $opt_f +) ) ; ... if ( defined $#ARGV && $#ARGV > -1) {
The variable $#ARGV is ALWAYS defined! You probably want something like this instead:
die "Usage: $0 [-c] [-v] <-f pattern-file | subnet/netmask> file [file +...]\n" unless @ARGV || defined $opt_f; ... if ( @ARGV ) {
# remove non-ip-address characters, trim, break into words + to validate $IP_LINE =~ s/[^0-9.]+/ /g ; $IP_LINE =~ s/^ // ; $IP_LINE =~ s/ $// ; my @WORDS = split(/\s+/,$IP_LINE) ;
Would be better as:
# remove non-ip-address characters, trim, break into words + to validate $IP_LINE =~ tr/0-9./ /c; my @WORDS = split ' ', $IP_LINE;
# grep each file for my $FNAME (@ARGV) { if ( ! -r $FNAME ) { printf STDERR "Cannot read: $FNAME\n" ; next ; } open ($INFILE, "<$FNAME") ;
You have the possibility of a race condition where the file could become non-readable after the -r test. You should print the error message only if open fails and include the $! variable so you know why it fails.
# grep each file for my $FNAME (@ARGV) { open my $INFILE, '<', $FNAME or do { warn "Cannot open '$FNAME' because: $!"; next; };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Looking for feedback on a script
by systemj (Initiate) on Dec 23, 2010 at 18:17 UTC |