in reply to Evil Interview Questions
Nice questions.
Regarding the third question (for and map), I didn't know the answer you gave. However, there's another difference from the one you mentioned.
our @x = 1; sub foo { push @x, print "hi" }
The for loop will call the subroutine lots of times, whereas the map will call it only a single time.
The relevant part of documentation is in perlsyn section Foreach Loops:
If any part of LIST is an array, "foreach" will get very confused if you add or remove elements within the loop body, for example with "splice". So don't do that.
(This sentence always intrigued me, for "very confused" in the above sounds like it could include segfaults (like sort with a non-transitive comparision function used to in old perls), but I've no idea if it's indeed like that.)
|
|---|