Lua's global keyword isn't what you wanted.
Lua is a small embeddable programming language. Probably since it's inception, people have whinged about one feature of the language: global by default.
By default, Lua puts everything into an _ENV table, which by default is _G. You can actually use metaprogramming to prevent the runtime creation of new globals via overloading the __newidex metatmethod.
Probably to quell crit ism over it's global-by-defaultness, Lua 5.5 has added the global keyword. Lots of people have been praising the change, but it is a semantically perplexing addition. Lots of people seem to think it declares global variables. When actually it restricts what globals a scope will use.
If you don't actually read the manual on how global works, it seems like a major bone. But it's behavior is confusing. The only utility it has is the illusion of safety. For example, global variables can still be changed via accessing _ENV. The Lua 5.5 manual provides the following example of that:
global X <const>, _G X = 1 -- ERROR _ENV.X = 1 -- OK
Additionally, global declarations can be changed by other scopes. This makes global <const> a complete illusion. It prevents nothing and only makes you "feel safe". For example:
global <const> *
local function f()
global x
x = 100
end
f()
print(x)
This will print 100. global <const> has *zero* binding power. Additionally, imported modules act with an implicit global * for their scope. This means a bad module can still polute your global <const> namespace.
global <const> * require "libs.foobar" -- libs.foobar could have added dozens of global variables -- despite the declarations of global <const>, it -- does not protect you across modules. It is just -- fluff.
Whatever Makes You Happy I Guess
It's a little frustrating see the global keyword wasted on such a confusing feature. I could think of dozens of ways to have done this better. In my honest opinion, this make Lua a more confusing language. If PUC-Rio truly wanted to "fix" Lua's global-by-default behavior, they should have just broke the language and released Lua 6.0.
I guess I can see why Mike Paul has such a sour opinion of PUC-Rio Lua. Cause what the fuck is this? If Lua's global-by-default nature turns you off, this addition is absolutely not what you wanted. I think this actually makes the global-by-default behavior worse cause it creates bold-face lies in your code about controlling it.
Why Are You Cheering?
I just had to write this down cause I see so many people just cheering this on without actually looking at how it works. It's just decoration that creates semantic illusions. And nobody seems to actually be looking beyond "oh yeah, you can declare globals!". When it's 1) the exact opposite (you restrict globals) and 2) has literally no binding power. JavaScript's "use strict" annotation is more powerful, simply because there is no "use unstrict" to disable it again.