use strict;
use warnings;
use HTML::Template;
use strict;
use warnings;
use Data::Dump::Streamer;
my %hash;
while () {
chomp;
my @elements = split /\s*\/\s*/;
next if ! @elements;
$hash{$elements[0]} = {} if ! exists $hash{$elements[0]};
my $subHash = $hash{shift @elements};
for (@elements) {
$subHash->{$_} = {} unless exists $subHash->{$_};
$subHash = $subHash->{$_};
};
}
my @tree;
push @tree, buildFamily (\%hash, $_) for keys %hash;
my $template = <<'HTML';
Test Template
Parent:
Child:
Grandchild:
HTML
my $t = HTML::Template->new (scalarref => \$template);
$t->param (Parents => \@tree);
print $t->output ();
sub buildFamily {
my ($hash, $parent) = @_;
my %root = (name => $parent);
if (keys %{$hash->{$parent}}) {
my @children;
push @children, buildFamily ($hash->{$parent}, $_)
for keys %{$hash->{$parent}};
$root{children} = \@children;
}
return \%root;
}
__DATA__
one
foo / bar
foo / baz
foo / qux / two
foo / qux / three
foo / qux / four
five
####
Test Template
Parent: five
Parent: one
Parent: foo
Child: bar
Child: baz
Child: qux
Grandchild: three
Grandchild: two
Grandchild: four