lua-users home
lua-l archive

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


On Thu, 4 Dec 2003 10:53:52 -0200
Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:

> >Is there a (better) way to do such a "auto-dofile with args"?
> 
> % cat magic.lua
> local LOADED={}
> 
> local function f(t,i)
>  local a=LOADED[i]
>  if a~=nil then return a end
>  a=loadfile(i..".lua")
>  if a==nil then return nil end
>  local b=function(...) ARG=arg a() end
>  LOADED[i]=b
>  return b
> end
> 
> setmetatable(getfenv(),{__index=f})
> 
> % lua -lmagic testfoo.lua
> MSG:foo.lua was called with ARGHi
> val:1

I had to tweak magic.lua to get the above result.  Without tweaking: 

sge:/home/sge/lang/lua:465$ lua -lmagic testfoo.lua 
lua -lmagic testfoo.lua 
MSG:foo.lua was called with ARGHi
lua: testfoo.lua:4: attempt to concatenate global `val' (a nil value)
stack traceback:
	testfoo.lua:4: in main chunk
	[C]: ?
sge:/home/sge/lang/lua:466$ 


To get the 2nd line of output I changed

   local b=function(...) ARG=arg a() end

to

   local b=function(...) ARG=arg return a() end
                                 ^^^^^^

and my output looked just like yours.


Steve