lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> I think the best way to disallow global access would be to disable
> the environment by explicitly assigning nil to _ENV:
> 
> do
>   local oldenv = _ENV
>   local _ENV = nil --or not local if that's how it works
> 
>   a = 12345 --would result in "error: no environment"
> end

Repeating again: the point is not to disallow global access, but to
prevent unintended creation of globals. Changing the environment
prevents the unintended creation of globals, but at a high cost (no
more access to globals including the standard libraries):

do
  local _ENV = nil
  a = 12345    --would result in "error: no environment"; fine
  local a = 12 -- fine, 'a' now is local
  print(a)     -- oops; error...
end

-- Roberto