in reply to Re^6: Hard syntax error or disambiguable parsing?
in thread Hard syntax error or disambiguable parsing?
What, exactly, does 'aliasing that $v' mean?
This may not be what you're asking, but... the mechanics of "aliasing" fall out of the way Perl manages values.
The name $s refers to a pointer to the value currently associated with that name. The array entry for $a[78] also refers to a pointer to the value currently associated with the entry. And so on. All values are accessed via one level of indirection.
To make $v into an alias for some array entry, Perl simply copies the array entry's pointer to $v's pointer... shazzam ! (Which would probably be curtains for the old value associated with $v, "garbage collection"-wise; but that's another story.)
Problem is clearly that on loop exit, left to its own devices, the loop variable would remain an alias -- aliases are magic enough when contained within the scope of the loop !
I now speculate: I suspect that the $v doesn't know it's an alias, the mechanism is so deeply buried in the way variables work -- but in any case, extra code would be required to copy the last value to $v. Besides, much of the time it's useful to have a local loop variable, and since that mechanism already existed my guess would be that looked like the simplest approach...
What is the significance of it using that variable? When does it make a difference?
The difference is that if there is a Global and a Lexical $i in existence at the same time, then it is the Lexical that is localized for the duration of the loop. As this illustrates:
I suppose it might have been clearer if my at the package layer declared package variables... but that horse left home at some speed many, many moons ago.use strict ; use warnings ; use vars '$i' ; $i = 'hello there' ; for $i (1..2) { print "$i: $main::i\n" ; # 1: 1 } ; # 2: 2 print "$i\n" ; # hello there for my $i (4..5) { print "$i: $main::i\n" ; # 4: hello there } ; # 5: hello there print "$i\n" ; # hello there my $i = '*' ; for $i (7..8) { print "$i: $main::i\n" ; # 7: hello there } ; # 8: hello there print "$i: $main::i\n" ; # *: hello there
|
|---|