In vanilla Lua 5.2, I have a module that contains:
Two local functions, A and B: B will always call A, A will sometimes call B and sometimes will call the functions stored in C;
C: a table (local). It contains tables which contains tables which can contain tables... which will at the end contain functions. These functions may call either A or B;
Then there's the return function, D, which will be returned when my module is loaded using
require
. It will call A.
At the end, it looks quite like this:
--don't pay attention to what the functions do:
--I am only writing them down to explain how they interact with each other
local A, B, C
C = {
...
{
function(a)
B(a)
end
}
...
}
A = function(a)
...
if (...) then
B(a)
end
...
if (...) then
C[...]...[...](a)
end
...
end
B = function(a)
A(a)
end
return function(s) -- we called this one D
A(s)
end
Now, my problem is this: the declaration of C uses its own local variables, metatables and all that stuff, to the point I enclosed its declaration in a do ... end
block.
It also is - with all those tables inside tables and newlines for each curly brace and indentation and so on - quite long. So I wanted to put it in its own module, but then it couldn't access B.
So, my question is: is there a way to pass B and maybe even A to the file where C is declared while loading it? I mean something like this, if it were possible:
--in the original module
local A, B, C
C = require("c", A, B)
...
And then, in c.lua:
local A, B = select(1, ...), select(2, ...)
C = {
...
{
function(a)
B(a)
end
}
...
}
I really have no idea on how to do it.
Is there a way to pass variables from the requiring file to the required file which doesn't involve the variables being inserted in the global namespace?