lua-users home
lua-l archive

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


Diego Nehab wrote:
Hi,

There are two issues here. One is namespace polution. The
other is returning the namespace from require().

In most cases, require *will* return the namespace. Either
your package returns it directly, or it calls module()
(which sets the package.loaded table) and returns nil. In
either case, the namespace will be returned.

Ok, I see my mistake. I understand all the little bits of logic in ll_require and ll_module now! My problem was that I did a require "WalkerAI" and a module "ai", so they were not the same!!! That problem is solved. Thanks.


About the namespace problem, Mildred's solution does the
trick. If you don't want your module to polute the
namespace, set loaded[modname] to the namespace *before*
calling module().

This seems like a fair bit of hackery, especially the 'package.seeall' function, needed to make the hand-crafted table work like a module. Rewriting the module function would not be bad, except that it would break any module that depended on the global behavior.

Adding a package.noglobal function would be nice, then you could go:

    module("WalkerAI", package.noglobal)

This function would remove the namespace from the global table. It would require rewriting of the module function as well, to save the previous value of the namespace. Something like this might work:


    local _module = module
    function module(name, ...)
        local _G = _G
        local package = package
        local v = _G[name]
        _module(name,...)
        if package.loaded[name].__NOGLOBAL ~= nil then
            _G[name] = v
        end
    end

    function package.noglobal(m)
        m.__NOGLOBAL = true
    end

How does that look?

--
chris marrin                ,""$,
chris@marrin.com          b`    $                             ,,.
                        mP     b'                            , 1$'
        ,.`           ,b`    ,`                              :$$'
     ,|`             mP    ,`                                       ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`
"As a general rule,don't solve puzzles that open portals to Hell"'