...or, for that matter, why we have:
for ($i=0; $i<53; $i++) {
}
loops, as well. Even though perl's list-iterating for-loops are incredibly sweet, and replace 99% of for-loops written in other languages, there's still that 1% of iterating loops that aren't list-iterating loops... and that's why perl still has for( ; ; ) loops as well.
The real reason why the value is localized within the loop is that it is not the variable (by the same name) outside of the loop. Period. What it is, though, is an alias to the item in the list being iterated over. That is why you can do things like:
foreach my $item (@list) {
$item = mutate $item;
}
and it produces the same result as:
@list = map { mutate $_ } @list;
That is... in the nth iteration of the loop, the list-iterator variable IS the nth item in the array. They are one and the same.
With that context in mind, ask yourself how much sense it would make that your list iterator variable was not localized.
------------
:Wq
Not an editor command: Wq
|