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.


In reply to Re: $_ getting clobbered by inner loop. by bigmacbear
in thread $_ getting clobbered by inner loop. by dbanas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.