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?
In reply to Re^2: Hard syntax error or disambiguable parsing?
by rovf
in thread Hard syntax error or disambiguable parsing?
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |