[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Non local variables not becoming global?
- From: Saptarshi Guha <saptarshi.guha@...>
- Date: Mon, 18 Oct 2010 22:23:24 -0700
Hello,
I have a question about locals for some constructs.
Suppose I have
f1 = function(a)
local x = 1
for i=1,3 do
y=2
end
end
f2=function(a)
f1(a)
print(y)
end
Then, when f2 is called, y is created in the global scope. To avoid
this i can write
local y
just below the 'local x=1' line.
Is there anyway to force chunks to create variable in the current context rather
than in global when local is not specified?
So that in the above, print(y) would display nil?
Thanks
S
(I'm using 5.1, does the _ENV (in 5.2) help in any way?)