In Perl, is it appropriate to use map in void context instead of a foreach loop?

前端 未结 3 1930
遥遥无期
遥遥无期 2020-12-03 08:31

In Perl, if you have a loop like this:

foreach (@items) {
    perform_action($_);
}

you can replace it with a call to map in v

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 09:11

    Starting from Perl 5.8.1 map in void context is not expensive:

    map in void context is no longer expensive. map is now context aware, and will not construct a list if called in void context.

    But the postfix form of for may be more readable:

    perform_action($_) for @items;
    

提交回复
热议问题