lua-users home
lua-l archive

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



I really like require and module as they exist in 5.1 ... almost.

The trouble I have is the fact that the module function adds the module name to the global table. That pollutes the namespace, which really bothers me. What I want to do is this:

    local foo = require "foo"
    foo.bar()

Now, I can make my 'foo' module return a reference to itself and the above will work. But if I used the module function when initializing foo, there will also be a 'foo' in the global namespace.

Module used like this:

    module "foo"
    function bar() ... end

I can put the above into a file called foo.lua and the require function will load it. But if I did only this, calling foo.bar() as above would fail, because local foo would be nil, because I did not return anything from the implementation. In order to get foo.lua to work I would have to do this:

    local package = package
    module "foo"
    function bar() ... end
    return package.loaded.foo

Now, that's pretty lame. It would not be so bad if module actually returned the reference to the module. Then I could at least go:

    local foo = module "foo"
    function bar() ... end
    return foo

That's better, but 'foo' is still added to the global namespace.

Why do modules get put into the global namespace? And why doesn't module return a reference? Am I missing an easier way here?

--
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"'