3

When print(_ENV) is used in zerobrane studio on windows, it results into nil. Can we set _ENV variable to its expected use? As an example of code,

a = 15                      -- create a global variable
      _ENV = {g = _G}             -- change current environment
      a = 1                       -- create a field in _ENV
      g.print(_ENV.a, g.a)

This code throws the error in zerobrane studio on windows.

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
mathsbeauty
  • 364
  • 4
  • 13

1 Answers1

3

_ENV was introduced in Lua 5.2.

See https://www.lua.org/manual/5.2/manual.html#8.1

ZeroBrane runs the Lua 5.1 interpreter by default. You can pick another one via

main menu -> Project -> Lua Interpreter

In Lua 5.1 you could do something like this:

a = 15
setfenv(1, {g = _G})
_ENV = g.getfenv()
a = 1
g.print(_ENV.a, g.a)

I haven't spent too much thought on it. So probably it is not 100% equivalent.

Piglet
  • 27,501
  • 3
  • 20
  • 43