.. is the range operator. It can be used in two rather different ways, depending on the context in which it occurs. In this case, you are using the list context, in which case your code
for (0 .. $#array - 2)
is similar to
for (local $_ = 0; $_ <= $#array - 2; $_++)
In other words, it creates a range of numbers, from 0 to $#array - 2.
The range operator can also be used in scalar context, in which case it keeps track of a state. But that is another question.
This is different from
foreach(@array -2)
Note that in this expression, the array @array is put in scalar context by the - operator, and will return its size. It will only create one value for the for loop, and that is the size of the array minus 2. Say the list has 10 elements, then you get:
foreach (8)
Which is just a loop over a list of one item (the number 8), and not very useful. So the difference is quite big, and not meaningful to explain, really. I assume you meant:
foreach (@array[0 .. $#array - 2])
Which is a loop using an array slice. In this case, you are looping over the values of the array, rather than over the indexes. E.g.
for (0 .. $#array) {
print $array[$_];
}
vs
for (@array) {
print $_;
}
You might also note that for and foreach mean exactly the same thing in Perl. They are aliases of each other, and there is no difference in how they work.
foreach(0 .. @array-2)? If@arrayhas 6 elements,$#array-2(highest index minus 2) is3and@array-2(number of elements minus 2) is4.