lua-users home
lua-l archive

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


It was thus said that the Great Dirk Laurie once stated:
> 2016-02-28 8:16 GMT+02:00 Sean Conner <sean@conman.org>:
> 
> > I've found that the less global variables I have, the easier it is to
> > reason about the code (and that's regardless of language).
> 
> I've been using the idiom of semi-global local variables for some
> time. Those are locals explicitly defined but not initialized right
> at the top of the file. All other locals appear only in do-end blocks.
> 
> One _could_ (but I don't) then write a routine that exploits the
> debug library to access them via strings that hold their names.

  So you've never written anything as evil as:

	local base64,clock,crc,env,errno,fsys,hash,iconv,math,net,pollset
	local process,signal,sys,syslog,tcc,cc,date,getopt,unix

	local function locals(s)
	  local function iter(s,v)
	    v = v + 1
	    local n,lv = debug.getlocal(s,v)
	    if n then
	      return v,n,lv
	    end
	  end  
	     
	  return iter,s,0
	end
   
	local function loadmods()
	  for i,name,value in locals(3) do
	    if not value then
	      debug.setlocal(2,i,require("org.conman." .. name))
	    end
	  end  
	end    
       
	loadmods()

	for i,name,value in locals(2) do
	  print(i,name,value)
	end

  -spc (No, neither have I)