http://qs321.pair.com?node_id=11119056

Lady_Aleena has asked for the wisdom of the Perl Monks concerning the following question:

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/&amp/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