[oak perl] Can't modify map iterator in scalar assignment?
Belden Lyman
blyman at iii.com
Thu Dec 9 11:29:45 CST 2004
On Thu, 2004-12-09 at 08:50, Kester Allen wrote:
> Hi Eric--
>
> Are you trying to make $p an array reference? If so, you should use
> $p = [ qw ( 1 3 4 ) ];
Yes.
> or
> $p = { 1, 3, 4 };
No, please don't do this. It's misleading to the next programmer;
it makes $p look like a hashref, not an arrayref.
Plus if *you* "use warnings" you'll get the error
Odd number of elements in anonymous hash
;)
> instead of what you're using. Your statement assigns the value "4" to
> $p. If you "use warnings" (always a good idea) you'll get an error
> "Useless use of a constant in void context at <filename> line
> <linenum>"
>
> Also, you're using map in a void context, which is generally frowned
> upon. A better phrasing might be:
>
I've heard mixed messages on this. For example, "perldoc -q void"
says
What's wrong with using grep or map in a void context?
The problem is that both grep and map build a return list,
regardless of the context. This means you're making Perl go to
the trouble of building a list that you then just throw away.
If the list is large, you waste both time and space. If your
intent is to iterate over the list then use a for loop for this
purpose.
However, I've heard that recent versions of perl have fixed that,
so void context with map isn't the pitfall it used to be. For
example, "map in void context" at http://tinyurl.com/6twpz
Efficiency (or lack thereof) aside, there are style issues here
too. I must've read http://tinyurl.com/596fb early on in my Perl
programming, since those are the reasons I cite when avoiding
map in void context.
Belden, in coffee context
More information about the Oakland
mailing list