lua-users home
lua-l archive

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


Duck wrote:
> If I use:
> 
>  module(...,whatever)
> 
> correctly at the top of any packages I might create, does:
> 
> 
>  loadfile(modulename ..'.lua')(modulename)
> 
> become equivalent in side-effects to calling:
> 
>  require(modulename)
> 
> ?

As you can see in [1], once found and loaded (the equivalent of
loadfile), if the module has assigned something to
package.loaded[modname] (which 'module' do), 'require' does nothing more
than returning the content of that variable. So the answer is yes.

> In other words, can I simulate a require-style loader directly with
> loadfile, and have the correct side-effects to _G, package.loaded,
> and so forth? And if so, could I do this:  
> 
>  loadstring(modulecode)(modulename)
> 
> as well?

Yes again for the same reasons.

An alternative would be to replace:

  loadstring(modulecode)(modulename)

by:

  package.preload[modulename] = loadstring(modulecode)
  require(modulename)

Just out of curiosity, why do you want to use loadfile/loadstring rather
than require ? require is highly configurable, and if you just want to
alter the way the modules are located and loaded it's very easy to do in
pure Lua.

[1] http://www.lua.org/manual/5.1/manual.html#pdf-require