in reply to Re: Hard syntax error or disambiguable parsing?
in thread Hard syntax error or disambiguable parsing?
A foreach loop absolutely wants to create a new local scalar variable
Somehow I forgot about the scoping rules within foreach, so I reread perlsyn:
The "foreach" loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn. If the variable is preceded with the keyword "my", then it is lexically scoped, and is therefore visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop. If the variable was previously declared with "my", it uses that variable instead of the global one, but it's still localized to the loop.
If I understand this right, we have 4 cases:
In case 1, $v is a lexical variable where the lifetime ends with the end of the loop. In case 2, $v is in the global symbol table, but localized, in the sense we usually get by local $v;; the localization ends at the end of the loop. As for case 3, I don't really understand the text of the man page; what does localizing a lexical variable mean? As for case 4, the man page doesn't say anything explicitly, so it must be in effect the same as case 1.{ foreach my $v (...) {} } # 1 { foreach $v (...) {} } # 2 { my $v; foreach $v (...) {} } # 3 { my $v; foreach my $v (...) {} } # 4
Is this correct, and could you please give some explication about case 3?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Hard syntax error or disambiguable parsing?
by shmem (Chancellor) on Jan 29, 2009 at 10:43 UTC | |
by ack (Deacon) on Jan 29, 2009 at 16:44 UTC | |
by shmem (Chancellor) on Jan 29, 2009 at 21:32 UTC | |
by ig (Vicar) on Jan 30, 2009 at 00:41 UTC | |
by BrowserUk (Patriarch) on Jan 30, 2009 at 02:54 UTC | |
| |
by shmem (Chancellor) on Jan 30, 2009 at 13:59 UTC | |
by gone2015 (Deacon) on Jan 30, 2009 at 11:09 UTC | |
|
Re^3: Hard syntax error or disambiguable parsing?
by ack (Deacon) on Jan 29, 2009 at 16:26 UTC |