[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Redefine the module() function
- From: Mildred <ml.mildred593@...>
- Date: Tue, 15 Jan 2008 13:28:29 +0100
Le Mon 14/01/2008 à 12:03 Aladdin Lampé à écrit:
> "testmod.lua":
> local package = require "package"
> local original_module = module
>
> function module(modname, ...)
> original_module(modname, ...)
> package.loaded[modname].hello = "hello you"
> return m
> end
>
> require 'mymod'
> mymod.hi()
>
> "mymod.lua":
> module 'mymod'
> function hi() print(hello) end
You are not forced to redefine the module function to do that. You may
do like that:
-- testmod.lua
function testmod(_M)
package.loaded[_M._NAME].hello = "hello you"
-- This line also works:
-- _M.hello = "hello you"
return _M
end
require 'mymod'
mymod.hi()
-- mymod.lua
module('mymod', package.seeall, testmod)
function hi()
print(hello)
end
-------
And I get
$ lua testmod.lua
hello you
$
--
Mildred Ki'lya
E-Mail: mildred593(at)online.fr
Site: <http://mildred632.free.fr/>
XMPP: <mildred@jabber.fr> (GoogleTalk, Jabber)
GPG: 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]