Re: "auto-dofile with args"
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: "auto-dofile with args"
- From: Asko Kauppi <asko.kauppi@...>
- Date: Thu, 4 Dec 2003 18:56:52 +0200
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()'?
-ak
Luiz 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: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