lua-users home
lua-l archive

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


>>  I should have put the extra requirement that I need this in 3.2.
>
>You can do something similar, but instead of changing the table of globals,
>you will have to copy all globals to a new table, and then erase all globals.

Or you can set up a "setglobal" tag method to redirect global definitions to
a table. Something like

 function import(f)
  local t={}
  local g=settagmethod(tag(nil),"setglobal",function (n,o,v) %t[n]=v end)
  dofile(f..".lua")
  settagmethod(tag(nil),"setglobal",g)
  return t
 end

If your module use (=read) global variables defined in the module, then you
must also set up a "getglobal" tag method to read first from t and then fro
the real globals (with rawgetglobal).
--lhf