title: Perl subroutine - return
body: <p>The following code is to make a hash dynamically. If I don't give a <code>return</code> statement in the <code>else</code> part of the subroutine, I get </p>
  
  <pre><code>$VAR1 = {
      'fruit' =&gt; { 'apple' =&gt; 'skin' } };
  </code></pre>
  
  <p>but If give the <code>return</code> statement I get  </p>
  
  <pre><code>$VAR1 = {
      'fruit' =&gt; {
          'apple' =&gt; {
              'red' =&gt; 'skin'}
          }
      };  
  </code></pre>
  
  <p>which is what I want.<br>
  What makes this difference. Can someone please educate me.  </p>
  
  <pre><code>sub construct_hash{
      my ($hash, $value, $head, @tail ) = @_;
      if(@tail){
          $hash = { $head =&gt; construct_hash(\%{$hash}, $value, shift @tail, @tail)} ;
      }else{
          $hash-&gt;{$head} = $value;
          return $hash;
      }
  }
  my %h;
  my @keys = qw (fruit apple red);
  my $value = 'skin';
  print Dumper construct_hash(\%h, $value, shift @keys, @keys);
  </code></pre>