<?xml version="1.0" encoding="windows-1252"?>
<node id="296889" title="Re: Think for yourself." created="2003-10-06 04:51:18" updated="2005-06-05 05:15:05">
<type id="11">
note</type>
<author id="169744">
Abigail-II</author>
<data>
<field name="doctext">
&lt;blockquote&gt;
&lt;em&gt;What is the difference between what these lines should do?&lt;/em&gt; 
&lt;code&gt;
map do_something($_), @some_list;
do_something($_) for @some_list;
&lt;/code&gt;
&lt;em&gt;
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.
&lt;/em&gt; 
&lt;/blockquote&gt;
&lt;p&gt;
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:
&lt;code&gt;
() = do_something ($_) for @some_list;
&lt;/code&gt;
Context matters. map gives list context to the expression, while the
expression with the for-modifier is in void context.
&lt;p&gt;   
And of course, there's another common way of writing map expressions:
&lt;code&gt;
map {BLOCK} @some_list;
&lt;/code&gt;
Writing that in for-modifier style gives you:
&lt;code&gt;
() = do {BLOCK} for @some_list;
&lt;/code&gt;   
Although if the context doesn't matter, you can get away with:
&lt;code&gt; 
do {BLOCK} for @some_list;
&lt;/code&gt;
Alternatively, you can write it as for statements:
&lt;code&gt;
for (@some_list) {
    () = do {BLOCK};
}
&lt;/code&gt;
or
&lt;code&gt;
for (@some_list) {
    BLOCK;
}
&lt;/code&gt;
depending on whether context matters or not.
&lt;p&gt;
&lt;blockquote&gt;&lt;em&gt;
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).   
&lt;/em&gt;&lt;/blockquote&gt;
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.
&lt;p&gt;
What is your feeling if you see:
&lt;code&gt; 
user_defined_function {BLOCK} LIST;
&lt;/code&gt;
Utter confusion? Or is this ok, as long as it's not called 'map'?
&lt;blockquote&gt;&lt;em&gt;
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.
&lt;/em&gt;&lt;/blockquote&gt;
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.
&lt;p&gt;
Abigail
</field>
<field name="root_node">
296742</field>
<field name="parent_node">
296806</field>
</data>
</node>
