Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Don't cargo cult "advice"

by Abigail-II (Bishop)
on May 03, 2004 at 15:04 UTC ( #350037=note: print w/replies, xml ) Need Help??

in reply to Re: Returning regexp pattern that was used to match
in thread Returning regexp pattern that was used to match

Don't use map in a void context, it throws away the output.
What output does it throw away? What about using assignment or print in void context? Not using print in void context because some "output" is thrown away?


Replies are listed 'Best First'.
Re: Don't cargo cult "advice"
by Ven'Tatsu (Deacon) on May 03, 2004 at 15:38 UTC
    Each $string .= ... returns the new contents of $string. map builds an array with the values of last statment execuded in it's block. This cost both time and memory.
    foreach does not keep the values it's block returns so it will run faster than map.
    The OP's code did not use any of the results a concatination assignment (other than the actual concationation assignment side effect) so keeping the results around is a waste.
    In the case of assignment or print your usualy not interested in the direct result, only the side effect. So storing the result is a waste of time and memory.

    See What's wrong with using grep or map in a void context?

    This was changed in 5.8.1 (map only, grep should still be avoided in void context), but as there are plenty of installs running perls prior to that it's still a bad idea.
      5.8.1 fixed a long standing bug. Celebrate that, and upgrade.


        Yes, because as a contract programer I have the right to dictate to every employer exactly what production environment they are allowed to run.
        I wish the world was that simple but it't not. One of my recent target systems is running 5.005, I recomended updgrading but the higher-ups felt the cost of rebuilding and regression testing all the moduals used was too significant for the current release cycle.
        One of the things I love about perl is how portable it is. I really hate the idea of making a perl program less porable (in this case to older versions of perl) when there is a no cost portable solution avalable. If there was a good reason not us use foreach I would agree with you, but it seems to me (and correct me if I'm wrong) that your argument rests on "Many other people spout out 'Don't use map in void context' therefor you shouldn't say it."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://350037]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2018-01-23 11:07 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (243 votes). Check out past polls.