in reply to Closures & aliases
Foreach Loops
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. This implicit localisation occurs only in a "foreach" loop.
$foo was bound to test() originally but then you slapped a localization over the binding so it was a separate variable being written to.
perl -le 'my $foo;print \$foo;for $foo(1){print \$foo}' SCALAR(0x8152348) SCALAR(0x81441c8)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Closures & aliases
by BrowserUk (Patriarch) on Jun 06, 2004 at 00:51 UTC | |
by QM (Parson) on Jun 06, 2004 at 02:47 UTC | |
by hv (Prior) on Jun 06, 2004 at 04:05 UTC | |
by diotalevi (Canon) on Jun 06, 2004 at 00:57 UTC | |
|
Re^2: Closures & aliases
by ambrus (Abbot) on Jun 06, 2004 at 18:13 UTC |