As you might know, I have been using Perl::Critic on my code over the last several days. After I finished checking my code on the gentle setting, I kicked it up a notch and used stern. Well, Perl::Critic set on stern gave me screens full of problems, and the biggest one is I use the expression form of map and grep. I looked around the web to find out why.
Here is my opinion that could be very wrong.
All of the examples I found of the expression form of map and grep would lead to inevitable problems, and I can see why the writers of those examples would jump on using the block form for both. However, the one thing all of the examples had in common is the disuse of parentheses. I feel that if the expression form of a map or grep is used with parentheses, it is contained within them.
The map below would lead to problems, since there is nothing containing the expression or on which list(s) the map is being applied.
my @colors = qw(red yellow green cyan blue magenta); my @grey_scale = qw(white grey black); my @list = map "$_ beads", @colors, @grey_scale;
However, I do not think this needs a block form to contain the map and list if parentheses are used.
my @list = map( "$_ beads", @colors ), @grey_scale;
Now the map is contained within parentheses, and @grey_scale does not get beads mapped to it. However, if one must use the block form, parentheses would still be needed to contain the mapped items.
my @list = ( map { "$_ beads" } @colors ), @grey_scale;
I think the expression form with parentheses is easier on the eyes, but it is just my opinion. I can understand using the block form if the map were more complex, however, I think I would write a separate subroutine instead of loading the block with more than two or three modifications and use the expression form.
sub make_beads { my $color = shift; if ($color =~ /red|green|blue/) { $color .= ' sparkley'; } else { $color .= ' shiny'; } $color .= ' beads'; return $color; } my @list = map( make_beads($_), @colors), @grey_scale;
Converting from expression to block form is also problematic as it is not always as simple as the above would suggest, especially with grep. I have also found the expression form of grep to be easier to use, in one case (I can not remember the specifics) I could not get the block form of grep to work.
If I use sort with map and/or grep on the same list, I will wrestle the block forms until I get the results I want, because in that case, it is easier on my eyes than trying to mix expression forms with the block of sort.
my @list = ( map { make_beads($_} } sort { $a cmp $b } grep { <something> } @colors ), @grey_scale;
I know I can ignore Perl::Critic's results on this and other issues, however, I do want code that is more acceptable by the community. So, I am trying to decide if I want to start wrestling with Perl on this or not. (I ran Perl::Critic on all of my modules, and it found 330 lines where I used the expression forms of map and grep, so this is fairly big to me.)
I hope I am not too wrong about this.
My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.16.3 or 5.30.0 on web host depending on the shebang.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Expression form of map or grep
by haukex (Archbishop) on Jul 10, 2020 at 20:00 UTC | |
Re: Expression form of map or grep
by tobyink (Canon) on Jul 10, 2020 at 21:11 UTC | |
Re: Expression form of map or grep
by ForgotPasswordAgain (Priest) on Jul 12, 2020 at 00:03 UTC | |
by AnomalousMonk (Archbishop) on Jul 12, 2020 at 01:56 UTC | |
by Lady_Aleena (Priest) on Jul 12, 2020 at 12:19 UTC | |
by parv (Parson) on Jul 13, 2020 at 05:53 UTC | |
by Lady_Aleena (Priest) on Jul 13, 2020 at 10:39 UTC | |
by parv (Parson) on Jul 14, 2020 at 04:24 UTC | |
| |
Re: Expression form of map or grep
by jcb (Parson) on Jul 12, 2020 at 03:37 UTC | |
Re: Expression form of map or grep ( .perlcriticrc )
by beech (Parson) on Jul 13, 2020 at 21:08 UTC | |
by Lady_Aleena (Priest) on Jul 15, 2020 at 10:01 UTC | |
Re: Expression form of map or grep
by karlgoethebier (Abbot) on Jul 13, 2020 at 07:38 UTC | |
Re: Expression form of map or grep
by karlgoethebier (Abbot) on Jul 12, 2020 at 16:23 UTC |