lua-users home
lua-l archive

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

On 11/18/2011 03:35 AM, Oliver Mueller wrote:
Duncan's solution would work for exactly the scenario I described in
my post...but unfortunately this is not general enough for me. I don't
know the path of "scripts" in advance (depends on where the user
decides to store her scripts) so I cannot hard code it into the
script. I'd need to expand the load path with the path of the
directory from which the first lua file is loaded.

When you load the file, give it an environment with a version of 'require' that knows how to find the modules. Something like this,

local lua_require = require
local function build_require(path)
  path = path .. package.config:sub(1,1)
  return function(name)
           -- try to load
           -- or pass the name to lua_require
function doscript(filename)
  local env = setmetatable({}, {__index=_G})
  env.require = build_require(dirname(filename))
  return loadin(filename, env)

- tom