0

There is a Perl code line below where I get the message from perlcritic:

map { $total_ids += scalar @{$ids->{$_}} } @brands;

The message is:

"map" used in void context near 'map { $total_ids += scalar @{$ids->{$_}} } @brands;'

Can anyone help me to fix it?

toolic
  • 57,801
  • 17
  • 75
  • 117
Saash Tech
  • 35
  • 1
  • 3

1 Answers1

5

map returns a list, which in void context is thrown away.

As recommended by Perl::Critic::Policy::BuiltinFunctions::ProhibitVoidMap, turn your map into a foreach

 $total_ids += scalar @{$ids->{$_}} foreach @brands;
toolic
  • 57,801
  • 17
  • 75
  • 117
JGNI
  • 3,933
  • 11
  • 21
  • 4
    "`map` in void context is no longer expensive. `map` is now context aware, and will not construct a list if called in void context." - from [perl581delta](https://perldoc.perl.org/perl581delta#Miscellaneous-Enhancements). So that hasn't been true since 2003. – Dave Cross Jan 11 '21 at 16:50
  • 7
    Using `map` in void context is still a bad idea. But because it obfuscates your code, not because it has any actual detrimental effects. – Dave Cross Jan 11 '21 at 16:52
  • 1
    Or just: `$total_ids += @{$ids->{$_}} for @brands;` – ikegami Jan 11 '21 at 19:18