use HTML::Template; my $thingy = [ { name => 'foo', id => 1, children => [ { name => 'bar', id => 2, children => [ { name => 'baz', id => 3, }, { name => 'qux', id => 4, }, ], }, { name => 'ducks', id => 5, }, ], }, { name => 'last', id => 6, }, ]; our $tmpl = HTML::Template->new( filehandle => \*DATA, die_on_bad_params => 0, ); our $output; our $tab_loop = []; recurse($thingy); print $output; sub recurse { my $thingy = shift; for (@$thingy) { $tmpl->param(tab_loop => $tab_loop, %$_); $output .= $tmpl->output; push @$tab_loop, { tab => "\t" }; recurse($_->{children}) if ref $_->{children}; pop @$tab_loop; } } __DATA__ -