Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Perlplexation - foreach shoulda Known

by eyepopslikeamosquito (Canon)
on Apr 14, 2012 at 11:17 UTC ( #965055=note: print w/ replies, xml ) Need Help??


in reply to Perlplexation - foreach shoulda Known

The foreach (and for and map, etc) command can be used to modify the list that it is iterating over
I recommend the Effective Perl Programming book to you and your workmates, especially item 20, "Use foreach, map and grep as appropriate", which gives an excellent summary of when to use foreach, map and grep:
  • Use foreach to iterate read-only over each element of a list
  • Use map to create a list based on the contents of another list
  • Use foreach to modify elements of a list
  • Use grep to select elements in a list
I've often asked new Perl programmers at work to read this item when I find them writing inappropriate C-style for loops or discover they are confused about foreach and have never even used map or grep.

Note that Hall, McAdams and foy further caution against modifying a list via map:

"For efficiency, $_ is actually an alias for the current element in the iteration. If you modify $_ within the transform expression of a map, you modify the input data. This is generally considered to be bad style, and -- who knows? -- you may even wind up confusing yourself this way. If you want to modify the contents of a list, use foreach."


Comment on Re: Perlplexation - foreach shoulda Known
Select or Download Code
Re^2: Perlplexation - foreach shoulda Known
by JavaFan (Canon) on Apr 16, 2012 at 11:36 UTC
    This is generally considered to be bad style, and -- who knows? -- you may even wind up confusing yourself this way. If you want to modify the contents of a list, use foreach.
    You know, I can understand people who say "I find modifying list elements using an alias confusing", and I can understand people who say "I'm fine using all the features Perl gives me, including list aliasing". I don't know what to think of people who say "I'm fine with modifying list elements in a block, but only if the keyword is foreach, if the keyword is map, it confuses me". Those aren't the people I would want to hire.
Re^2: Perlplexation - foreach shoulda Known
by raybies (Chaplain) on Apr 16, 2012 at 16:34 UTC
    I've had Effective Perl Programming for a year now, and really enjoyed it, but for some reason missed the detail until just this last week... I was "whoah!" and decided to try out a bunch of examples. :)

    So are "aliases" used in other Perl commands besides these sorts of loops? Is that what $a and $b are considered to be in the sort command?

      So are "aliases" used in other Perl commands besides these sorts of loops? Is that what $a and $b are considered to be in the sort command?
      Yes and yes. Some places aliases are used:
      • $_ in foreach, map and grep
      • $a and $b in a sort block
      • $a and $b in List::Util's reduce function and List::MoreUtils's pairwise function
      • Each element of @_ for the actual arguments in a subroutine call
      • By packages importing symbols
      • By an our declaration, which creates a lexically scoped alias
      • You can explicitly create an alias via typeglobs

      See also "alias" in perlglossary and Item 118 "Access the symbol table with typeglobs" in Effective Perl Programming.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (14)
As of 2014-10-21 18:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (106 votes), past polls