рекурсия - не самый удачный вариант, стека может не хватить.
вот вариант итеративный:
=====================================
#!/usr/local/bin/perluse strict;
# ====================================
sub load {
my($fname) = @_;
my(@vals, @res, $str);
open(FILE, $fname) || die "Can't open $fname!";
while (defined ($str = <FILE>)) {
@vals = split(/\s+/, $str);
push(@res, [0, $#vals, [@vals]]);
} # while
close(FILE);
return \@res;
} # sub
# ====================================
sub make_next {
my($input) = @_;
my($i, $m, $carry);
$carry = 1;
for ($i = $#{$input}; $i >= 0; $i--) {
$m = ${${$input}[$i]}[0] + $carry;
if ($m > ${${$input}[$i]}[1]) {
${${$input}[$i]}[0] = 0;
} else {
$carry = 0;
${${$input}[$i]}[0] = $m;
last;
} # else
} # for
return 1 if $carry == 0;
return 0;
} # sub
# ====================================
sub show {
my($input) = @_;
my($elem, @idxs, @vals, $src);
foreach $elem (@{$input}) {
push(@idxs, ${$elem}[0]);
push(@vals, ${${$elem} [2]}[${$elem}[0]]);
} # foreach
print '(' . join(', ', @idxs) . ') ';
print '(' . join(', ', @vals) . ")\n";
} # sub
# ====================================
sub produce {
my($input) = @_;
my($dims, @res, $subres, $elem, $subelem);
do {
show($input);
} while (make_next($input));
} # sub
# ====================================
my($fname, $input);
$fname = "/tmp/input.txt";
$input = load($fname);
produce($input);