http://www.perlmonks.org?node_id=1043452


in reply to foreach argument modification inside loop, followed by return from loop

If I were to look at that piece of code, without comments, then I would finger it as almost-certainly “a bug,” because I can’t imagine why you would want to pop the last element off of an array if any element in it equals '2.'   I am going to presume, frankly, that you (whoever you were) didn’t write what you thought you wrote, and that you never exhaustively tested it.   Now, I’ve got to figure out what your code actually does, what you meant to write, and what else might be wrong ... nearby or far-away to this questionable subroutine.   (Where there’s one roach in the kitchen, there are many.)

These admonitions about what to do and what not to do with arrays and lists are really (IMHO) just as much having to do with clarity (and future-proofing as maintenance happens), as with what will or won’t cause the interpreter to behave unexpectedly.   When you are tasked with maintaining hundreds of thousands of lines of years-old smelly code that you didn’t write, “cleverness” is not your friend.

(Please note that my uses of the word “you” are entirely impersonal ... “you” could be anyone, anywhere, anytime.)