http://qs321.pair.com?node_id=1167076

fireblood has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, when I first learned perl I read that one needs to be careful to avoid creating $_ in nested loops because the inner one will clobber the outer one. But on a whim I just created a program called x which contains the following and found that it WORKS!

for (<>) { print "Current line is $_"; for (split) { print "Current token is $_\n"; } }

When I type cat file | x, each line from file is first displayed and then each token within that line is displayed immediately afterwards. I would have thought that the outer "for" would create a list of values that are popped off and assigned to $_ one at time for each pass of the outer "for" loop, but that the inner "for" would replace that list with a list of its own. But that is not happening, the outer "for" is working fine as though the inner "for" was not there. It looks as if each execution of the inner "for" creates and then exhausts its own list whose values it assigns to its own $_ and that this has no effect on the list and the $_ that the outer "for" uses. I could understand this if there were a local $_ within the first pair of braces, but that's not the case here.

Before I start relying on this behavior in production programs that I write I wanted to seek your wisdom in confirming that this is expected perl behavior that I can reliably count on. Thank you!