title: can't read a public scalar inside a sub
body: <p>the perl code is like followed : the problem is that I can not read $key inside sub tweak_server{} ....</p>
  
  <pre><code>my $key;
  my %hash = ( flintstones =&gt; [ "C:/Users1/f1.xml", "C:/Users1/f2.xml" ], 
              jetsons =&gt; [ "C:/Users2/f1.xml" ], 
              simpsons =&gt;    [ "C:/Users3/f1.xml", "C:/Users3/f1.xml", "C:/Users3/f1.xml" ], ); 
  
  foreach $key (keys%hash){
  if (scalar@{$hash{$key}}&gt;1){
      foreach my $path (@{$hash{$key}}){
          my $filehandle;
          open($filehandle, "+&lt;$path") or die "cannot open out file out_file:$!";
          my $roots = { TAG =&gt; 1 };
          my $handlers = { 'ROOT/TAG' =&gt; \&amp;tweak_server,
                      };
          my $twig = new XML::Twig(TwigRoots =&gt; $roots,
                               TwigHandlers =&gt; $handlers,
                               twig_print_outside_roots =&gt; \*$filehandle);
          $twig-&gt;parsefile($path);
          say $key;#could read key
          sub tweak_server {
              my ($twig, $root) = @_;
              my $tag2=$root-&gt;first_child_text('TAG2'); 
              say $key;# could not read
              if ($tag2=~/$key/){
              #BLABLA
              }
              $twig-&gt;flush( $filehandle, pretty_print =&gt; 'indented');
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>as i state, the $key can be read outside the sub, but not inside..the error appeared:Use of uninitialized value $key</p>
  
  <p>and then i tried a simple situation, just like </p>
  
  <pre><code>my $a="aaa";
  open( $filehandle, "+&lt;$path") or die "cannot open out file out_file:$!";
  my $roots = { TAG =&gt; 1 };
  my $handlers = { 'ROOT/TAG' =&gt; \&amp;tweak_server,
              };
  my $twig = new XML::Twig(TwigRoots =&gt; $roots,
                       TwigHandlers =&gt; $handlers,
                       twig_print_outside_roots =&gt; \*$filehandle
                       );
  $twig-&gt;parsefile($path);
  sub tweak_server {
      say $a;
      my ($twig, $root) = @_;
      my $tags=$root-&gt;first_child_text('TAG2');
      my $str="204B";
      if ($tag2=~m/$str/){
      foreach my $b(1...6){
                      say $a;             }
  }
  $twig-&gt;flush( $filehandle, pretty_print =&gt; 'indented');
  
  }
  </code></pre>
  
  <p>in this code, the $a can be read....
  i spent one day on this, but still can't fix it...crazy now
  thank in advance!!</p>