sub genIterator { my( $root ) = @_; my @stack = ( $root, 0 ); return sub { while( @stack ) { my $index = pop @stack; my $tree = pop @stack; if( ref( $tree->[$index] ) ) { push( @stack, $tree, $index+1, $tree->[$index], 0 ); } elsif( $index < @$tree ) { push( @stack, $tree, $index+1 ); return $tree->[$index]; } } return; # empty stack } }