Hello all. I have been playing around with Per::Critic on the command line and found to my dismay that some of my modules do not pass gentle. I realize that I can ignore the recommendations of Perl critic, but I would love for my modules to pass "gentle" before I begin ignoring things.
The most common issue is that I modify $_ in list functions. The following is a convenient and short one liner to put the lines of files into a list. I thought doing it this way was nice and slim.
my @list = map { chomp($_); $_ } <$fh>;
my @uc_list = map { chomp $_; [uc $_] } <$lc_fh>; # used only onc
+e
my @split_list = map { chomp $_; [ split(/\|/, $_) ] } <$piped_fh>;
+# used only once
In one subroutine, I have this three times.
my @array = map {
$_ =~ s/^[\*\+-] (.+)/$1/;
some_sub($_, $opt);
} @another_array;
Also, my idify subroutine is just modifying $_.
my @ids = map {
$_ =~ s/<.+?>//g;
$_ =~ s/^(\d+([stnrdh]{2}|))/NUMWORDS($1)/e;
$_ =~ s/(.)\.\w{2,5}?$/$1/;
$_ =~ s/&/and/g;
$_ =~ s/&/and/g;
$_ =~ s/Æ/Ae/g;
$_ =~ s/Ç/C/g;
$_ =~ s/Ü/U/g;
$_ =~ s/(è|é|ê)/e/g;
$_ =~ s/#/No/g;
$_ =~ s/ /_/g;
$_ =~ s/[^\w:.\-]//g;
$_;
} grep {defined($_)} @base;
And my Fancy::Map modifies $_ too.
sub fancy_map {
my ($opt, $list) = @_;
map {
if (ref($_)) {
fancy_map($opt, $_);
}
else {
my $before = $opt->{'before'} ? $opt->{'before'}.' ' : '';
my $after = $opt->{'after'} ? ' '.$opt->{'after'} : '';
$_ = $before.$_.$after;
}
} @{$list};
}
If this is something ignored usually, I will ignore it, but I would like to know how to make it better. Should I just assign $_ to a variable and then use the variable?
Another thing that I am confused by is why the expression form of eval is discouraged?
if ($raw_value =~ /\*/) { # multiplication
$total_value = eval($raw_value);
($amount, $base_value) = split(/\*/,$raw_value);
}
elsif ($raw_value =~ /\//) { # division
$base_value = eval($raw_value);
($total_value, $amount) = split(/\//, $raw_value);
}
The rest of the gentle issues are me being lazy with conditionals. While writing I was thinking my $var = "foo" if 'some condition', but Perl critic does not like it, but it is fixed easily.
NOTE: Please see my update. Most are fixed, but the eval problem remains.
My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.8.8 on web host.
No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|