lua-users home
lua-l archive

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


On Monday 27, Patrick Mc(avery wrote:
> do
> dofile(library.lua)--library.lua has local table A
> maintable.A = A
> end

dofile() dosn't give the caller access to local variables in side of 
library.lua only globals and the return value, but then those globals will be 
public to all *.lua files.  library.lua can return the A table which will be 
returned by dofile()

Your localsCollector.lua file should look like:
local maintable = {}

maintable.A = require("library")
-- note require() will cache the results of library.lua and only run it once.
-- that is what you normally want to happen.

-- repeat adding more modules to maintable....

You would want to structure your library.lua like this:

local A = {} -- this will be the library.lua's module table.

-- these will be private to library.lua
local some_module_private_variable = 42
local function some_private_function()
   print(some_module_private_variable)
end

-- these will be the public interface of library.lua
function A.some_public_function()
  -- call a private function
  some_private_function()
end

function A:some_public_method()
  -- return some private data.
  return some_module_private_variable
end

-- here we return the module's public interface table A, which will
-- be returned by dofile() or require()
return A

-- 
Robert G. Jakabosky