perl5 icon indicating copy to clipboard operation
perl5 copied to clipboard

warn if '=>' is used in "map EXPR,LIST'

Open djerius opened this issue 9 months ago • 4 comments

Description

It would be very helpful if a warning were issued when the => operator is used in the map EXPR,LIST syntax, in order to avoid writing this code:

map $_ => false, @subplot

which is definitely not the same as

map { $_ => false} @subplot

which is what I really meant.

djerius avatar Mar 14 '25 23:03 djerius

People like you also enjoyed: Perl::Critic::Policy::BuiltinFunctions::RequireBlockMap

guest20 avatar Mar 14 '25 23:03 guest20

People like you also enjoyed: Perl::Critic::Policy::BuiltinFunctions::RequireBlockMap

Not as much as I enjoy block-less map!

djerius avatar Mar 15 '25 00:03 djerius

Block-less map fans like us must learn the subtle differences between the lists returned by these:

map    $_ => false  , @subplot    # false, @subplot
map  { $_ => false }  @subplot    # @subplot elements alternating with false
map +( $_ => false ), @subplot    # @subplot elements alternating with false
map +{ $_ => false }, @subplot    # hashrefs with key = a @subplot item, value = false
map  { $_ => false }, @subplot    # syntax error

Even though I love the expression form for its conciseness, I'll readily admit the block form is the safe and consistent one.

book avatar Mar 15 '25 09:03 book

People like you also enjoyed: Perl::Critic::Policy::BuiltinFunctions::RequireBlockMap

Not as much as I enjoy block-less map!

I just noticed that perlfunc actually gives one example where you get to use the EXPR form! (Been overlooking this somehow, well.)

 my %hash = map +( $_ => false ), @subplot; # this is EXPR and works!
 my %hash = map  { $_ => false }  @subplot; # equivalent to...

The + is necessary, otherwise it is parsed as map $_, false followed by @subplot:

 my %hash = map  ( $_ => false ), @subplot; # false => undef
    # (@subplot consumed by void context, which warnings will catch)

(I was curious when it got added so I did a git-blame... here.)

rapidcow avatar Jun 16 '25 09:06 rapidcow