lua-users home
lua-l archive

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


If you use some form of linter to check for inadvertent global assignments, _G can be the way to explicitly assign a global. Of course, if you also wrap your globals table in metatable logic to catch assignments, then you need to do rawset( _G, "var", value ).

Mark

On May 7, 2012, at 12:34 AM, steve donovan <steve.j.donovan@gmail.com> wrote:

> On Mon, May 7, 2012 at 9:23 AM, Miles Bader <miles@gnu.org> wrote:
>> E.g. a common use is to list all global variables:
>> 
>>   for k,v in pairs(_G) do print (k, v) end
> 
> Or to make new global variables:
> 
> key = 'dog'
> _G[key] = 'barney'
> 
> There's nothing magic about _G; the magic is that the 'global
> environment' is _just a table_, and _G is a variable referencing it.
> 
> (_ENV is more subtle, since _setting it_ is meaningful)
> 
> steve d.
>