|Perl: the Markov chain saw|
Re: Think for yourself.by Abigail-II (Bishop)
|on Oct 06, 2003 at 08:51 UTC||Need Help??|
What is the difference between what these lines should do?They should do the same thing. But to my eyes the second reads much more clearly, and I believe that the same holds true at virtually any level of Perl expertise.
Well, they don't do the same thing; or rather, you cannot deduce from this code fragment whether they will do the same thing or not. If you want to be sure they are the same, you have to write the latter line as:
Context matters. map gives list context to the expression, while the expression with the for-modifier is in void context.
And of course, there's another common way of writing map expressions:
Writing that in for-modifier style gives you:
Although if the context doesn't matter, you can get away with:
Alternatively, you can write it as for statements:
depending on whether context matters or not.
If there is a clear way and an unclear way of writing the same thing, with both taking similar effort and length, I call it bad style to deliberately use the unclear one (unless confusion is your goal).What is unclear and what is clear is very subjective. For a language that comes with opinions on what is good and what is bad style, see http://www.python.org. However, I find it difficult to believe there are people that find map in void context "unclear", "obfuscated" or "bad style". I can certainly understand people having problems with map. But I find it hard to believe there are seasoned Perl programmers that have no problem with map if the map is on the right hand side of an assignment, but are suddenly getting confused if they assignment disappears.
What is your feeling if you see:
Utter confusion? Or is this ok, as long as it's not called 'map'?
This does not, of course, justify deriding someone who has picked up the meme of map in void context. But it does indicate gently pointing out that there are clearer ways to do the same thing.I disagree. It's ok to say that you have problems understanding the concept of map in void context, and that you prefer another style. But it's not ok to suggest that getting confused over a map in void context is something that all Perl programmers suffer from. Just point out it's your own personal preference.