in reply to Re: loop surprise
in thread loop surprise
It is very common for various implementation quirks to be used with regard to loops, in the very-important name of efficiency.
Rather than efficiency, I think reliability (or perhaps one should better say coherence) is the key concern. If a topicalized (i.e., localized and aliased) Perl-style loop iterator were left un-de-localized upon exit from the loop, to what would it be aliased? An arbitrary element of some named or referenced array? An item in a temporary list, perhaps a literal (i.e., something unwritable), or the (writeble) return value of a function call? (This point has been touched upon in other replies.) Such a state of affairs seems like a recipe for some very perplexing bugs.
IOW, if not de-localized, exactly what is the nature of the thing to which $_ would remain aliased after loop exit in this code:
And why would one want to do that?c:\@Work\Perl\monks>perl -wMstrict -le "sub F { return 4; } sub G { return 5; } sub H { return 6; } ;; for (F(), G(), H()) { ++$_; printf qq{$_ }; } " 5 6 7
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: loop surprise
by RonW (Parson) on Apr 05, 2018 at 20:43 UTC | |
by Your Mother (Archbishop) on Apr 05, 2018 at 21:00 UTC | |
|
Re^3: loop surprise
by Anonymous Monk on Apr 04, 2018 at 10:14 UTC |