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


in reply to Fear of Large Languages (was: "Would you use goto")
in thread Would you use 'goto' here?


Just as a note,

It seems to me that the grep example shouldn't be used anyway, because you're modifying $_ en passent. This can lead to all kinds of problems, not the least of which is a lower level programmer misunderstanding why this is bad.

Doing that in two lines seems to be a bit better, if more verbose:

s/foo/bar foreach @arr; return grep { /bar/ } @arr;
jynx

update: excuse my slow thinking

i first thought that clintp was demonstrating canonical use of grep, which is documented, but difficult to understand at first (akin to canonical use of map). i feel sheepish. It now behooves me to think that he was in fact directing his snippet at what i was trying to point out: a subtle feature that is documented and so able to be understood (and used in production code).

i feel there is still a point to be made however. While most of his other code snippets could be discovered plainly from documentation, this example is a bit more insidious. The student learning grep will probably not "get" what is being said in the documentation about this feature (i know i didn't the first few times through it). More to the point, while clintp's example is fine, a learning student would probably not notice the pitfall, since it doesn't directly follow; thinking they will is, sadly, overestimating their intelligence. Then later they could easily try altering $_ and get bizarre results.

In the end, it seems better to avoid this usage in code for maintainability, whereas the other examples seem perfectly valid. Maybe it's just me...