title: Why doesn't Perl's foreach require its variable to be declared with my?
body: <p>Today, I stumbled over something in Perl I was not aware of: it "localizes" the variable that the elements of the list iterated over is assigned to.</p>
  
  <p>This, of course, is documented in the Perl documentation - however I failed to remember or read it.</p>
  
  <p>The following script demonstrates what I mean:</p>
  
  <pre><code>use warnings;
  use strict;
  
  my $g = 99;
  
  foreach $g (1..5) {
    p($g);
  }
  
  sub p {
    my $l = shift;
    printf ("%2d %2d\n", $g, $l);
  }
  </code></pre>
  
  <p>The script prints</p>
  
  <pre><code>99  1
  99  2
  99  3
  99  4
  99  5
  </code></pre>
  
  <p>because <code>$g</code> is "localized" to the <code>foreach</code> loop.</p>
  
  <p>As far as I can tell there is no difference if I had added <code>my</code> to <code>$g</code> in the foreach loop:</p>
  
  <pre><code>foreach my $g (1..5) {
  </code></pre>
  
  <p>Actually, I ended up doing it because I feel it makes it clearer that the variable is local to the loop.</p>
  
  <p>My question is now: is there a scenario where my using <code>my</code> does make a difference (given that <code>$g</code> is already declared globally).</p>