When possible I try to assign and use something like $i instead of $_. Is this a bad habit?

This is, IMHO, a very good habit! I tend to write all my block-structured for-loops in the form

for my $loop_iterator_variable (@whatever) { ... do_something_with($loop_iterator_variable); ... }

However, for statement-modifier forms of the for-loop and things like map and grep statements, I'm quite comfortable using  $_ in
    do_something_with($_) for @whatever;
or
    my @new_array = map  { do_something_with($_) } @whatever;
    my @new_array = grep { do_something_with($_) } @whatever;
and similar, fairly simple statements, and see no harm; the idiom seems quite clear and useful in these cases. Indeed, it's a bit awkward to use a named variable here:
    do { my $n = $_;  do_something_with($n) } for @whatever;

But rest assured that the use of  $_ in these cases is perfectly safe and does what you expect — as long as you expect that  $_ is an alias, and assigning to it assigns to items of the list or array over which you are iterating! (Of course, the same is true of the iterator variable, named or default, in a block-structured for-loop.)


Give a man a fish:  <%-{-{-{-<


In reply to Re^5: 36 Conditions in If Statement [sendhelp]] by AnomalousMonk
in thread 36 Conditions in If Statement [sendhelp]] by UpTide

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.