title: Does returning a ref to a hash declared in subroutine causes memory leak?
body: <p>Is using <strong>my %data;</strong> below okay or should I switch to <strong>my $data={};</strong> ?</p>
  
  <pre><code>#!/usr/bin/perl -wT
  use strict;
  use JSON;
  
  sub makeJson {
          my %data;
  
          $data{kib} = 1;
          $data{games} = {
                  0 =&gt; [],
                  1 =&gt; [qw(a b c d e)],
          };
  
          return \%data;
  }
  
  my $x = makeJson();
  print encode_json($x) . "\n";
  </code></pre>
  
  <p>I'm confused because -</p>
  
  <p>If the <strong>makeJson()</strong> is called several times - and the <strong>my %data;</strong> is allocated at stack and then that address is being returned - wouldn't that be a memory leak or maybe not a "leak", but a "problem"? Because there would be memory allocated at the stack, which is still referenced and thus can not be deallocated.</p>
  
  <p>And other way around: if I have a subroutine returning a hash reference, which is better to use <strong>my %data;</strong> (faster, because preallocated by compiler?) or <strong>my $data={};</strong> ?</p>