lua-users home
lua-l archive

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



On 8-Feb-07, at 1:16 AM, gary ng wrote:


--- Rici Lake <lua@ricilake.net> wrote:
So you could modify that slightly by setting
     RunTests = "modulename"
instead of just to true. However, that means
hard-coding
the name of the module into the module-file, which I
wanted to avoid.

Thanks for the suggestion from both.

I am not quite understand the above though.

I took your approach and do it like this :

local __name__
if type(package.loaded["my_module"]) ~= "userdata"
then
__name__ = "__main__"
module("my_module")
...things I write

then at the end, the pythonic way:

if __name__ == "__main__" then <self test> end

Won't this also hardcode the module name in the module
file, right there in the module() call ?

Yes, that's one of the reasons I don't use module().

The actual code in my original message avoids hard-coding
the module name with the line:

local myname = ...

at the beginning. That picks up the first argument supplied
when the module chunk is called; if it is called by require(),
that will be the module name. If it is called by the stand-alone
interpreter, that will be the first command-line argument, if
there is one, but that won't hurt the package.loaded test
(in particular, t[nil] is nil for any table that doesn't have
an __index metamethod which checks for that condition.)

In other words, if the module is not being require()d, then
myname might be set to anything, but it's highly unlikely to
be set to the name of a module which is currently being
require()d at the point at which the file is being executed,
since at that point no require() is in progress.