sub nested_foreach(&@) { my $code = shift; my @indices = map { 0 } @_; # First set of indices is all zeroes my @sizes = map { scalar @$_ } @_; # Cache array sizes (optional) my $k; do { # Determine the array elements corresponding to the current set # of indices, and pass them to the closure: $code->( map { $_[$_][$indices[$_]] } 0..$#_ ); # Determine the next set of indices: for ($k = $#_; $k >= 0; $k--) { $indices[$k]++; if ($indices[$k] < $sizes[$k]) { last; } else { $indices[$k] = 0; } } # If $k went out-of-bounds, there are no more valid iterations: } while ($k >= 0); } my @a = ...; my @b = ...; my @c = ...; nested_foreach { say join ' ', @_ } \@a, \@b, \@c;