lua-users home
lua-l archive

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


Frantisek Fuka wrote:
> In the main file, I have the following:
> 
> local mod=require("xml")
> 
> This includes the module from my file "xml.lua" which starts with:
> 
> module("MyModules.XML", package.seeall)
> 
> After this, MyModules.XML points to the module and the module itself
> works as expected as expected. However, the "require" command returns
> nil and "mod" variable is set to nil! Of course I can manually do
> "local xml=MyModules.XML" afterwards, but is this intended? It seems
> at odds with what is presented in the documentation.    

As stated by Patrick Donnelly, your problem come from the different
module names. require("foo") will return what is in
package.loaded["foo"], while module("bar") will create your module in
package.loaded["bar"].

Unless that's what you want (there are special cases where you might
want that seemingly odd behaviour), you should make sure that the same
name is used in both function calls. The easiest way to do that is to
use the name passed to the module itself when it is loaded. Replace your
module first line with:

module(..., package.seeall)

and everything should be fine. If you want your module to be in
MyModules.XML, then you should call require with that string, and make
sure the module loader will find it (ie call your module file
"MyModules/XML.lua").

If you absolutely want the module name to be different from the module
filename, you have to do some magic with the loaders. Just ask if that's
the case :-)