[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: RE: "require" succesful, but returns "nil"
- From: "Frantisek Fuka" <fuka@...>
- Date: Thu, 19 Jul 2007 14:22:20 +0200
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
Yes, I understand perfectly. However, according to the documentation
for "require" (http://www.lua.org/manual/5.1/manual.html#pdf-require),
after calling require ("foo") while the "foo" file begins with
"module(bar)", the value of package.loaded["foo"] ("foo", not "bar"!)
should be set to true, which it isn't. It's nil!
Unless that's what you want (there are special cases where you might
want that seemingly odd behaviour)
Yes, that's what I want. I want my module's name to be independent of
its filename. It works exactly as I intended (it's some advance
dynamic module loading/debugging stuff) and the only reason why I
wrote my post is that documentation seems to be wrong because it
implies that "require" always returns the pointer to the loaded
module. The Lua Book even suggests that I can use the following
command to keep the reference to the loaded module like this:
local just_loaded_module = require ("filename")
The fact that THIS DOES NOT WORK when the "module" command inside the
module has other parameter than "..." is never mentioned and made me
scratch my head for a while.
I quote from the documentation: "Once a loader is found, require calls
the loader with a single argument, modname. If the loader returns any
value, require assigns the returned value to package.loaded[modname].
If the loader returns no value and has not assigned any value to
package.loaded[modname], then require assigns true to this entry. In
any case, require returns the final value of package.loaded[modname]."
Because this doesn't mention that the "modname" value could change
somewhere during this process, this clearly implies that the final
returned value of require("foo") is always either the module itself or
"true", without any further conditions and caveats. However, in my
example, the returned value is nil.
Again, I have no problems with "why doesn't this work, what should I
do?" I just think the documentation is unclear in what ramifications
the usage of module name in "module" command has.
Frantisek Fuka (yes, that IS my real name)
(and it's pronounced "Fran-tjee-shek Foo-kah")
My E-Mail: email@example.com
My Homepage: http://www.fuxoft.cz
My blog: http://blog.fuxoft.cz
My Jabber/GoogleTalk ID: firstname.lastname@example.org