First one quick note:

my $file = "file"; # why not... my $temp = "temp"; # makes sense if you later want to use # some other basename for your sequence of files foreach (1..5){ open $file, '>', "$temp$_"; # you just turned $file from a string in +to a filehandle... ... }

In other words your assignment in the first line does nothing since the value gets clobbered when you use open

On to your question. I quickly glanced at the node and the author is basically saying that sometimes it is unclear what $_ is supposed to represent. Whether or not this is the case depends on the context that you use it in. For instance:

foreach (@lines) { $_ = uc; # ALL CAPS WOOOOOO s/$/!!!/; # EVERY LINE IS AN EXCLAMATION!!! print; }
It's fairly obvious that each operation of the loop is performed on each line in @lines. However, this may not be the case if you have a longer, more complex loop, and will not work at all if you have nested loops:
my @timesTable; foreach my $row (0..12) { # foreach my $col (0..12) { @timesTable[$row][$col] = $row * $col; } }
There are also times where I will take advantage of $_ to perform a bunch of operations on a single variable in a sort of pseudo-loop:
for ($leetString) { # "loops" only once s/very/uber/g; tr/astloe/457103/; s!w!\\/\\/!g; }
You didn't hear it from me, but the best part about Perl is that once you understand how the lingo works, you can use it any way you damn please. Unless you work with people who won't understand what you're saying, and you want them to understand what you're saying. Then you have turn to these annoying things called "Best Practices".


$,=qq.\n.;print q.\/\/____\/.,q./\ \ / / \\.,q.    /_/__.,q..
Happy, sober, smart: pick two.

In reply to Re: foreach loop and creating files with "$_" by xyzzy
in thread foreach loop and creating files with "$_" by james28909

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.