title: Automatically get loop index in foreach loop in Perl
body: <p>If I have the following array in Perl:</p>
  
  <pre><code>@x = qw(a b c);
  </code></pre>
  
  <p>and I iterate over it with <code>foreach</code>, then <code>$_</code> will refer to the current <em>element</em> in the array:</p>
  
  <pre><code>foreach (@x) {
      print;
  }
  </code></pre>
  
  <p>will print:</p>
  
  <pre><code>abc
  </code></pre>
  
  <p>Is there a similar way to get the <em>index</em> of the current element, without manually updating a counter? Something such as:</p>
  
  <pre><code>foreach (@x) {
      print $index;
  }
  </code></pre>
  
  <p>where <code>$index</code> is updated like <code>$_</code> to yield the output:</p>
  
  <pre><code>012
  </code></pre>