Side-effects are something to be avoided when programming.In Perl? So, you'd prefer to avoid assignment, print statements, regular expressions, and even fetching the value of a variable?
There are programming languages that are more suitable for avoiding side-effects. But programming in Perl while avoiding side-effects makes as much sense as writing English novels while avoiding vowels.
I also like my code to be as self-documenting as possible.Who doesn't? For me having code as much self-documenting as possible doesn't mean map in void context isn't.
I find map is less than optimal for both of these reasons.The map-in-void context equivalent would be$sum += $index{$_} for split(//, $word);
I see as many side-effects as the code using 'for', so your example doesn't prove your first reason. In fact, the 'side-effect' reason people often come up with doesn't make any sense. If you do map EXPR, LIST or map BLOCK LIST, the side-effects are in EXPR or BLOCK. The 'for' equivalents are EXPR for LIST and for (LIST) BLOCK. Same EXPR and BLOCK. Same side-effects.map $sum += $index{$_}, split(//, $word);
As for documentation (or clearness) issues, I see differences between:
andmap {BLOCK} LIST
and that's the order of BLOCK and LIST. If I find the action more important, I prefer map {BLOCK} LIST, as it lists the statement(s) first. If I want to put the focus on the data, I use for (LIST) {BLOCK}. (There's also a difference in context, map gives list context, for void context, but that's seldomly an issue).for (LIST) {BLOCK}
I hardly ever use the map EXPR, LIST construct though (regardless of context), as it doesn't clearly separate the action from the data.
In reply to Re^2: map in void context
by JavaFan
in thread map in void context
by dharanivasan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |