lua-users home
lua-l archive

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


On Tue, Nov 23, 2010 at 11:18 AM, Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
> If I say
>     dofile('xxx.lua')
> and xxx.lua is not in the current directory, it is an error.
>
> Is there a way of giving Lua a path in which dofile should look
> before giving up?

Try require"xxx". Then Lua will search package.path for xxx.lua.

Can require (or a derivative therof) be made to search a custom path?  I think that this is what Dirk was asking, and I'd like it as well.

The pseudocode for the Lua 5.1.4 require can be found section 15.1 of PIL (The print edition, not the older online one) reads:
function require (name)
  if not package.loaded[name] then
    local loader = findloader(name)
     if loader == nil then
       error ("unable to load module " .. name)
     end
     package.loaded[name] = true
     local res = loader(name)
     if res ~= nil then
       package.loaded[name] = res
     end
   end
   return package.loaded[name]
end

This appears to be good practice for a user-space require function, though it might be preferable to use a different table for package.loaded (dofile.loaded?).  Unfortunately, findloader and loader are abstract functions.  The real require function is implemented in lines 325-496 of loadlib.c (see especially lines 351ff, the findfile function) but I don't feel that I have the expertise to direct Dirk on how to make it work for the dofile function, and I'm a few weeks away from doing it myself. 

By the way, shouldn't that filename be lloadlib.c, like llimits and llex?
--
Kevin Vermeer