my %h2 = ( 'AAA' => 'aaa', 'BBB' => 'bbb', 'CCC' => { 'A1' => 'a1', 'B1' => 'b1', 'C1' => { 'A2' => 'a2', 'B2' => 'b2', 'C2' => 'c2END', }, }, 'DDD' => 'ddd', ); sub ddump { my $ref = shift; my $deep = shift||1; for my $k ( sort keys %$ref ) { if ( ref $ref->{$k} ) { print "\t" x $deep."$k =>\n"; ddump( $ref->{$k}, $deep+1 ); } else { print "\t" x ($deep)."$k => $ref->{$k}\n"; } } } ddump( \%h2 ); #### use CGI qw /:all -nph/; #on Win32 $|++; sub nest { my $it = shift; ref $it ? complex_table($it) : $it } sub complex_table { my $hr = shift; # could test here to ensure that $hr is indeed a hash ref. table( { -border => 2, -bordercolor => 'green', -cellspacing => '0', }, map Tr( td( {-valign=>'top'}, $_ ), td( nest( $hr->{$_} ))), sort keys %$hr ); } my %h1 = (); # feed your HoH here print start_html, complex_table( \%h1 ), end_html;