in reply to Re: detecting changes in a localised variable
in thread detecting changes in a localised variable
Especially Aristotle's surprising remark that no Perl construct implicitly localizes $_ at all!)
Hmm. I understand Aristotle's point, but I think his assertion is wrong.
P:\test>perl -le"$_='old value';@a=1..10; for(@a){ $_++ }; print;" old value
Here, for is aliasing $_ to each of the values in @a, but it localises $_ first! Hence, after the loop, $_ regains it's former value.
Perl could implicitly localise $_ for the duration of a while loop in the same way giving this:
P:\test>type junk.dat|perl -e"$_='old value';{local$_; while(<>){print +};}print;" line 1 line 2 line 3; old value
instead of this:
P:\test>type junk.dat|perl -e"$_='old value';while(<>){ print};print;" line 1 line 2 line 3;
As for why it doesn't, that's a question for those with the historical perspective.
|
|---|