|
I've extended the 'dofile()' functionality to be such, that arguments can be passed right there and then:
ret= dofile( "subfile.lua", 1, 2.345, "more.." )The new 'dofile' internally stores existing 'arg', replaces it with the ones from the call, calls original "dofile", and returns with the old 'arg' contents back.
This is simple, elegant (imho) and makes scripts much more readable. Note that i don't return a func, the subscripts may have "root-level" functionality just as any scripts.
Also, i'm setting 'arg[0]' to the name of the called script, which can be very useful at times.
Question: Why is this not the regular behaviour of 'dofile()'? -akLuiz Henrique de Figueiredo kirjoittaa torstaina, 4. joulukuuta 2003, kello 14:53:
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:1Note that the code in magic.lua caches auto-loaded files. If you want toload from disk everytime, then the code can be much simpler. --lhf