From b09f4f7cedee63dd9f8e68be92997d5b6a9ecb09 Mon Sep 17 00:00:00 2001 From: Andinus Date: Sun, 10 May 2020 02:22:05 +0530 Subject: Use die instead of warn & exit, simplify the script Using die makes the program shorter & simpler, initial reason for not using die was that it exits with 255 code which generally means: "We died, dunno why" & I thought I knew why so I should exit with something else. After talking to a few people looks like it doesn't matter much, I myself have never written a program that checks for a specific exit code so I switched to using die. - print $line =~ s/\t/: /r and - $total_acronyms++ if - ($line =~ /^\Q${term}\E\t/i); + if ($line =~ /^\Q${term}\E\t/i) { + print $line =~ s/\t/: /r; + $total_acronyms++; + } This change was made because the previous version didn't do what I think it did. I could simply replace and with a comma & it would be equivalent to the current version but then why not use the current version? This looks better imo & will be easier to understand later. --- pictor.pl | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/pictor.pl b/pictor.pl index fec65d4..ec59229 100755 --- a/pictor.pl +++ b/pictor.pl @@ -14,18 +14,17 @@ pledge( qw( stdio rpath unveil ) ) or my $term; # User must pass at least one argument. -if (@ARGV < 1) { - say STDERR "usage: wtf.pl term"; - exit 1; -} else { - $term = $ARGV[0]; - - # User can rename this program to "wtf" & run "wtf is wtf" too - # instead of "wtf wtf" or "pictor wtf", "wtf is term" looks - # better. - $term = $ARGV[1] if - ($ARGV[0] eq "is") -} +die "usage: pictor term\n" if + @ARGV < 1; + +# Assume first argument to be term. +$term = $ARGV[0]; + +# User can rename this program to "wtf" & run "wtf is wtf" instead of +# "wtf wtf", "wtf is term" looks better so we use $ARGV[1] as $term if +# $ARGV[0] is "is". +$term = $ARGV[1] if + $ARGV[0] eq "is"; # files contains list of all files to search for acronyms. my @files = ( @@ -57,7 +56,7 @@ foreach my $fn (@files) { # The program should continue if the file doesn't exist but # warn the user about it. do { - warn "Unable to open $fn: $!"; + warn "Unable to open $fn: $!\n"; next; }; @@ -66,9 +65,10 @@ foreach my $fn (@files) { # mess with \t. This regex matches when $line starts with # "$term\t". We replace \t with ": " before printing to make # the input neat. - print $line =~ s/\t/: /r and - $total_acronyms++ if - ($line =~ /^\Q${term}\E\t/i); + if ($line =~ /^\Q${term}\E\t/i) { + print $line =~ s/\t/: /r; + $total_acronyms++; + } } } @@ -76,9 +76,8 @@ foreach my $fn (@files) { pledge( qw( stdio ) ) or die "Unable to pledge: $!"; -# Print an error message if we don't find any match. -say STDERR "I don't know what '$term' means!" and - exit 1 unless +# Die if we don't find any match. +die "I don't know what '$term' means!\n" unless $total_acronyms; # Drop pledge permissions. -- cgit 1.4.1-2-gfad0