http://www.perlmonks.org?node_id=496090


in reply to Re^5: modify the contents of an array
in thread modify the contents of an array

briefly, map and grep are to be used for their return value. Or else a for loop will do. Even the perlstyle page mentions that...

Replies are listed 'Best First'.
Re^7: modify the contents of an array
by Anonymous Monk on Sep 29, 2005 at 13:35 UTC
    Eh, no, you completely misunderstand the purpose of perlstyle. Perlstyle lists Larry's preferences of how he codes. And there's only one thing he strongly cares about - which isn't the map issue. The remark you quote is listed under Here are some other more substantive style issues to think about.

    It's telling you to think about map/grep in void context - not ordering you to avoid it. But what's more important is the reasoning - it's referring to the fact that map in void context is expensive. And it was - at the time perlstyle was written. But perlstyle was written many years ago, long before the map issue was fixed. It's fixed now, so there's nothing left over from the reasoning in perlstyle.

      uh, seriously, wtf?
      map { s/// } @array; # ... s/// for @array;
      They're the same, except one is more confusing to read, because most people expect it's return type to be used.

      Just because larry likes something is not a reason everyone should be following him (we should all be blindingly following Perl Best Practices instead ;-), but map in void context has no advantage over for, but does add an element of confusion (did the author mean to assign to a result array? did the author not know about for? am I reading this badly? let me go up one line and see if it's a multi line assignment statement).

      Optimizing for readability is universal good style.

      -nuffin
      zz zZ Z Z #!perl