title: Perl, using variable from within While loop outside of the loop?
body: <p>This seems really simple but it is giving me a hard time figuring it out as I'm new to perl.. I've been looking through a lot of documentation now about loops and I am still stumped by this... I have a sub that contains a while loop and I want to use a variable value from within the loop outside of the loop (after the loop has run), however when I try to print out the variable, or return it out of the sub, it doesn't work, only when I print the variable from within the loop does it work.. I would appreciate any advice as to what I'm doing wrong.</p>
  
  <p>Doesn't work (doesn't print $test ):</p>
  
  <pre><code>sub testthis {    
      $i = 1;
      while ($i &lt;= 2) {    
          my $test = 'its working' ;    
          $i++ ;
      }
      print $test ;
  }
  
  &amp;testthis ;
  </code></pre>
  
  <p>Works, prints $test:</p>
  
  <pre><code>sub testthis {
      $i = 1;
      while ($i &lt;= 2) {
          my $test = 'its working' ;
          $i++ ;
          print $test ;
      }
  }
  
  &amp;testthis ;
  </code></pre>