Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Don't cargo cult "advice"

by Ven'Tatsu (Deacon)
on May 03, 2004 at 15:38 UTC ( #350052=note: print w/ replies, xml ) Need Help??


in reply to Don't cargo cult "advice"
in thread Returning regexp pattern that was used to match

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.


Comment on Re: Don't cargo cult "advice"
Re: Don't cargo cult "advice"
by Abigail-II (Bishop) on May 03, 2004 at 15:40 UTC
    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."
        Just because you are a peon who's word doesn't carry weight, doesn't mean that everyone is in that situation. You made a blatant statement without qualifying it. You got critized because of it. Next time, phrase it better, saying that your advice is only relevant for old versions of Perl. Don't make general statements about things in the past as if they still apply.
        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."
        You're wrong. Just do a search on my name and map in void context on google's usenet archive.

        Abigail

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2014-07-23 04:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (133 votes), past polls