Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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?

Abigail


Comment on Don't cargo cult "advice"
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.

      Abigail

        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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://350037]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-09-23 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls