In general, you shouldn't use grep or map for side-effects.
The purpose of grep is to filter a list into another (possibly shorter) list of elements of the original list. The grep block should really do nothing except evaluate to true or false.
The purpose of map is to transform a list into another list of the same length1, with the transformation being specified by the return value of the map block.
The purpose of 'for/foreach' is to loop over a list and perform an action (perhaps with side-effects such as printing) for each element.
All of these involve looping over the list and, since arbitrary code can be used in the grep or map block, all can be considered as variants on 'for', but with different return values. If you aren't using the return value, you should use for.
All of the 'should' and 'purpose' above relate to communicating your intent to other coders, of course the code would work whichever construct you used for your loop.
1OK, because perl flattens lists, you can change the number of list items by having your block evaluate to a list itself, which is useful to construct a hash from a list of keys, for example. |