in reply to Re^2: 36 Conditions in If Statement [sendhelp]]
in thread 36 Conditions in If Statement [sendhelp]]

using $_ for (0 .. 2) scares me though

Be not afraid! This is a bedrock Perl idiom. The  $_ variable is completely localized (update: and aliased! — it can be used to assign to the for-loop list item if that item is an lvalue) (I believe the term is "topicalized") within the for statement or statement block. Do you have any specific concern about this usage?


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

Replies are listed 'Best First'.
Re^4: 36 Conditions in If Statement [sendhelp]]
by UpTide (Novice) on Feb 07, 2017 at 17:34 UTC

    My fear, be it rational or not, is that $_ is very generic and I have no idea what it means when I look back over my code. With $i, $j, $k, $l, etc. I always use as indexes so I know that's an index; I try to name everything else as something meaningful. My example isn't well named but It's still easier for me to remember what I'm trying to do with $input, $output, @mask, rather than $_.

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

      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:  <%-{-{-{-<

      Hi UpTide,

      $_ is very generic and I have no idea what it means when I look back over my code

      I would have recommended using $_ only over short pieces of code for exactly that reason. So for example, if you write print "$_\n" for 0..2; I personally think it's fine because you know that the scope of this $_ is only that one line. But there's nothing wrong with writing for my $i (0..2) { print "$i\n" } instead! In the same vein, I personally think that for (0..2) { ... } is fine if the loop body is only a few lines long, and I'd go with for my $i (0..2) { ... } if the loop body gets longer (there's a gray area on what is "too long"), but there's nothing wrong with always using the latter! It's all about what you and possible future maintainers would find most readable.

      Regards,
      -- Hauke D