in reply to Why is it uninitialized?
The foreach loop iterates over a normal list value and sets the scalar 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 localization occurs only in a foreach loop.The foreach keyword is actually a synonym for the for keyword, so you can use either. If VAR is omitted, $_ is set to each value.
Update: added some formatting in the above quote.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Why is it uninitialized?
by Eily (Monsignor) on Dec 20, 2017 at 21:17 UTC | |
by Laurent_R (Canon) on Dec 20, 2017 at 22:14 UTC | |
by Eily (Monsignor) on Dec 21, 2017 at 09:15 UTC | |
|
Re^2: Why is it uninitialized?
by poj (Abbot) on Dec 20, 2017 at 19:48 UTC |