Если я правильно понимаю:
use Data::Dumper;
my ($x, $y, $z) = (1, 2, 3);
my @array = map [map [map 0, 1..$z], 1..$y], 1..$x;
print Dumper \@array;
Вывод:
$VAR1 = [
[
[
0,
0,
0
],
[
0,
0,
0
]
]
];
Однако нет необходимости предварительно создавать эту структуру, поскольку Perl создает ее для вас с помощью автовивификации (см. Ссылкудалее) при доступе к элементу во вложенной структуре:
use Data::Dumper;
my @array;
$array[0][0][2] = 3;
print Dumper \@array;
Вывод:
$VAR1 = [
[
[
undef,
undef,
3
]
]
];
С perlglossary :
autovivification
A Greco-Roman word meaning "to bring oneself to life". In Perl, storage locations
(lvalues) spontaneously generate themselves as needed, including the creation of
any hard reference values to point to the next level of storage. The assignment
$a[5][5][5][5][5] = "quintet" potentially creates five scalar storage locations,
plus four references (in the first four scalar locations) pointing to four new
anonymous arrays (to hold the last four scalar locations). But the point of
autovivification is that you don't have to worry about it.
Что касается цикла, если вам нужны индексы:
for my $i (0 .. $#array) {
for my $j (0 .. $#{$array[$i]}) {
for my $k (0 .. $#{$array[$i][$j]}) {
print "$i,$j,$k => $array[$i][$j][$k]\n";
}
}
}
В противном случае:
for (@array) {
for (@$_) {
for (@$_) {
print "$_\n";
}
}
}