sub foo { my $a = shift @_; return [] unless $a; map { my $b = $_; map { [$_,@$b] } @$a } foo(@_); }