lua-users home
lua-l archive

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


>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

Note that the code in magic.lua caches auto-loaded files. If you want to
load from disk everytime, then the code can be much simpler.
--lhf