in reply to Re^6: Hard syntax error or disambiguable parsing?
in thread Hard syntax error or disambiguable parsing?
I think the way it came about it that the foreach version of for each requires that the loop variable be aliased to the items in the list, and (at least historically) Perl's aliasing is achieved through globs.
So, pre-lexicals, the localisation was done by doing local *loopvar; under the covers.
Once lexicals came to pass, the quick fix for dealing with aliasing a lexical was to do the (rough?) equivalent of:
$_ = 'fred'; my $i = 123; { local *_ = \$i; for $i ( 1 .. 5 ) { print $i; } } print "\$_:$_ \$i:$i" 1 2 3 4 5 $_:fred $i:123
Which use *_ as the glob (per an implicit loop var loop), but allows the programmer to refer to the temporary variable by name within the loop body, whilst ensuring that both *_ and $i get restored afterward.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: Hard syntax error or disambiguable parsing?
by rovf (Priest) on Jan 30, 2009 at 08:52 UTC | |
by BrowserUk (Patriarch) on Jan 30, 2009 at 09:19 UTC | |
by shmem (Chancellor) on Jan 30, 2009 at 14:21 UTC |