title: Perl variables scoping
body: <p>I wanted to remove duplicate values from an array with <a href="https://stackoverflow.com/a/4004912/188264">this</a> approach. The duplicates removal have to be executed inside a loop.
  Here is a minimal example that demonstrates the problem that I encountered:</p>
  
  <pre><code>use strict;
  
  for (0..1){
      my %seen;
      sub testUnique{
          return !$seen{shift}++;
      }
  
      my $res = testUnique(1);
  
      if($res){
          print "unique\n";
      }else{
          print "non-unique\n";
      }
  }
  </code></pre>
  
  <p>I define the <code>%seen</code> hash inside a loop, so I would expect it to be defined only during a single iteration of the loop.
  The result of the above code is however:</p>
  
  <pre><code>unique
  non-unique
  </code></pre>
  
  <p>With some debug prints, I found out that the value of <code>%seen</code> is preserved from one iteration to another.</p>
  
  <p>I tried a trivial</p>
  
  <pre><code>for (0..1){
      my %seen;
      $seen{1}++;
      print "$seen{1}\n";
  }
  </code></pre>
  
  <p>And this one worked as expected. It printed:</p>
  
  <pre><code>1
  1
  </code></pre>
  
  <p>So, I guess the problem is with inner function <code>testUnique</code>.
  Can somebody explain me what is going on here?</p>