in reply to Re: Adventures in Debugging a Perl/Tk Game
in thread Adventures in Debugging a Perl/Tk Game

Thanks for the feedback.

But I'm comfortable using map, and I don't see anything wrong with using it in void context.

Here's a node I would refer you to:  is the use of map in a void context deprecated ?, which elaborates further.  In it, liz says:

...I seem to recall that map was just rewritten to check for void context and avoid the extra work if possible. That is correct. And it's in Perl 5.8.1! From the 5.8.1. perldelta: "map" in void context is no longer expensive. "map" is now context aware, and will not construct a list if called in void context.

And I particularly like Abigail-II's reply:  "Think for yourself.", as well as Abigail-II's many successive comments in the thread.

Update::  I also have to mention BrowserUk's final word in the thread, which links to this:

Larry Wall (<larry {at} wall.org>) Re: grep/map in void context perl.porters-gw The argument against using an operator for other than its primary purpose strikes me the same as the old argument that you shouldn't have sex for other than procreational purposes. Sometimes side effects are more enjoyable than the originally intended effect.

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^3: Adventures in Debugging a Perl/Tk Game
by kyle (Abbot) on Jan 16, 2008 at 17:43 UTC

    Thanks for the references. I understand Abigail-II's arguments, but I find that I agree with tilly's reply and demerphq's reply to the reply. When I see map in void context, it makes me wonder if the programmer meant something different than what's written. Was that line an assignment before? Was the returned list being used for something? Did the programmer forget something?

    Not long ago, I encountered "(my $x, $y) = f()", which is equivalent to "my $x;($x, $y) = f()". As it turned out, that is what the programmer intended, but I spent quite a while finding that out, because at first glance I wondered if it was supposed to be "my ($x, $y) = f()".

    (Back to map.) Given that the equivalent code (with for) is just as easy to write, the map stands out. If you're using it to get a list context (as mentioned in Re: Think for yourself.), then I think that would make an excellent comment right above.

    (As a semi-side remark, in Re: Think for yourself., Abigail-II says that a familiarity with map (from experience in other languages) that predates Perl's map is some part of the reason for seeing it as just another looping construct. I, too, was using maps in Scheme before I ever saw it in Perl, so I find it interesting that I don't share that view of it.)

    Thanks again for the references. It really is fascinating reading, and I recommend it to any other monk reading this.