title: How to share hash of hash in multithread perl
body: <p>One way to share a hash of hash is to declare every level of hashes like:</p>
  
  <pre><code>my %hash : shared;
  $hash{test} = &amp;share({});
  </code></pre>
  
  <p>but what if the first level keys' amount cannot be predicted or too much that you cannot declare every one.</p>
  
  <p>like the code below:</p>
  
  <pre><code>#!/usr/bin/perl -w
  use strict;
  use threads;
  use threads::shared;
  
  my %counts :shared;
  my $infile = $ARGV[0];
  open my $inf, "$infile";
  
  while(my $inline = &lt;$inf&gt;){
      chomp($inline);
      my @entry = split(/\t/,$inline);
      my $t = threads-&gt;create('WORK',@entry)-&gt;join;
  }
  
  foreach my $i(sort {$a cmp $b} keys %counts){
      foreach my $j(sort {$a cmp $b} keys %{$counts{$i}}){
          print "$i\t$j\t$counts{$i}{$j}\n";
      }
  }
  
  sub WORK{
      my @work = @_;
      $counts{$work[0]}{$work[1]}++;
  }
  </code></pre>
  
  <p>And the test set:</p>
  
  <pre><code>apple   pie
  banana  icecream
  orange  juice
  mango   juice
  mango   pie
  mango   pie
  ......
  </code></pre>
  
  <p>The script will be stopped by warning you that "Invalid value for shared scalar at SOME line". So is there a way I can share both %counts and %{$counts{KEY1}}? Suppose I don't know how many and what kinds of fruit I will observe of test set column one and the output should be like:</p>
  
  <pre><code>apple    pie      1
  banana   icecream 1
  mango    juice    1
  mango    pie      2
  orange   juice    1
  </code></pre>