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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.