in reply to Perl::Critic says don't modify $_ in list functions and other things
I have updated my code to the following based on suggestions in this thread and chatterbox. With so much wondeful help, I didn't know who to reply to.
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
...has become...
my @list = map { chomp; $_ } <$fh>; my @uc_list = map { chomp; [uc $_] } <$lc_fh>; # used only once my @split_list = map { chomp; [ split(/\|/, $_) ] } <$piped_fh>; # u +sed only once
It appears that explicitely using chomp($_) is what was the problem.
The following code is now a lot simpler once I put the regex into the subroutine instead of munging it in the map.
my @array = map { $_ =~ s/^[\*\+-] (.+)/$1/; some_sub($_, $opt); } @another_array;
...has become...
<code> my @array = map { some_sub($_, $opt); } @another_array;
With the use of /r and a chain of regexen, I was able to change the map.
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;
...has become...
my @ids = map { s/<.+?>//gr =~ s/^(\d+([stnrdh]{2}|))/NUMWORDS($1)/er =~ s/(.)\.\w{2,5}?$/$1/r =~ s/&/and/gr =~ s/&/and/gr =~ s/Æ/Ae/gr =~ s/Ç/C/gr =~ s/Ü/U/gr =~ s/(è|é|ê)/e/gr =~ s/#/No/gr =~ s/ /_/gr =~ s/[^\w:.\-]//gr } grep { defined } @base;
And Fancy::Map got a complete overhaul with help.
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}; }
...has become...
sub fancy_map { my ($opt, $list) = @_; map { ref $_ ? fancy_map ($opt, $_) : do { my $before = $opt->{'before'} ? $opt->{'before'}.' ' : ''; my $after = $opt->{'after'} ? ' '.$opt->{'after'} : ''; $before.$_.$after; } } @{$list}; }
My only unfinished business with Perl::Critic set on gentle is my two eval(EXPR). I haven't figured out a fix for that at the moment. Does eval BLOCK act differently than eval EXPR?
Also, please note my updated signature! 8)
My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.16.3 or 5.30.0 on my web host depending on the shebang.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Perl::Critic says don't modify $_ in list functions and other things
by AnomalousMonk (Archbishop) on Jul 09, 2020 at 22:59 UTC | |
by Lady_Aleena (Priest) on Jul 09, 2020 at 23:09 UTC | |
by hippo (Bishop) on Jul 10, 2020 at 08:22 UTC |