in reply to $_ getting clobbered by inner loop.

It's actually quite simple: the diamond operator (in your example, "<TEST_FILE>" is the diamond operator with the filehandle TEST_FILE as its argument) reads one line from the filehandle supplied and puts it in $_, clobbering whatever was there. A bare "print;" then uses the implied $_ variable as its argument.

I see two options: either localize $_ by using "local $_;" in a scope outside your while loop but within your foreach loop, or use a lexical variable in your outer scope (e.g. "foreach my $file (@test_files)") and then substitute it for $_ in your outer loop. In my opinion the latter is much clearer and thus preferred.

I could also have suggested a replacement for the automatic use of $_ in the inner loop, but the benefits you gain from using the implied $_ variable here are greater than those you lose by not using it in your outer loop.

  • Comment on Re: $_ getting clobbered by inner loop.