"module" tutorial

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

"module" tutorial

Stockdale, Daire, SOE
Hello

Can anyone provide a minimalist tutorial on correct use of the "require"
and "module" functions of 5.1 please, especially with reference to
creating a native Lua module? Or direct me to one?

Lots of thanks,

daire


 
========================================
DISCLAIMER

The contents of this e-mail and any attachments are confidential to the intended recipient and may also be legally privileged.  Unless you are the named addressee (or authorised to receive for the addressee) of this email you may not copy, disclose or distribute it to anyone else.  

If you have received this email in error, please notify us immediately by e-mail on [hidden email] and then delete the email and any copies.  The SEGA Group have made all reasonable efforts to ensure that this e-mail and any attached documents or software are free from software viruses, but it is the recipient's responsibility to confirm this.

========================================

Reply | Threaded
Open this post in threaded view
|

Re: "module" tutorial

陈果
// in your c file
static const luaL_Reg mylib[] = {
 /* add any global functions here */
 {NULL, NULL}
};

LUAELALIB_API int luaopen_mylib(lua_State *L) {
 luaL_register(L, "mylibname", mylib);
 /* The mylibname table is now on the stack; add any manipulation of that
table here. */
 return 1;
}
Build this file to produce your shared lib named " mylib.so(dll)" (It seems to have require("mylib") work, the name of the generated shared lib  must be the same as the name in luaopen_xxx)
Then in lua script file, write :
LUA_PATH="?.so(dll)" -- Don't need to change the source code of lua core lib
require("mylib")
mylibname.xxx  -- use the function registered with mylibname
 
Hope this can do help to you

2006/4/13, Stockdale, Daire, SOE <[hidden email]>:
Hello

Can anyone provide a minimalist tutorial on correct use of the "require"
and "module" functions of 5.1 please, especially with reference to
creating a native Lua module? Or direct me to one?

Lots of thanks,

daire



========================================
DISCLAIMER

The contents of this e-mail and any attachments are confidential to the intended recipient and may also be legally privileged.  Unless you are the named addressee (or authorised to receive for the addressee) of this email you may not copy, disclose or distribute it to anyone else.

If you have received this email in error, please notify us immediately by e-mail on [hidden email] and then delete the email and any copies.  The SEGA Group have made all reasonable efforts to ensure that this e-mail and any attached documents or software are free from software viruses, but it is the recipient's responsibility to confirm this.

========================================




--
Best Regards.

Chen guo
Tongji University, China
Mobile: 13817166264
MSN:    [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: "module" tutorial

Mildred Ki'Lya

On Thu, 13 Apr 2006 22:24:37 +0800 陈果 wrote:
> LUAELALIB_API int luaopen_mylib(lua_State *L) {
>  luaL_register(L, "mylibname", mylib);
>  /* The mylibname table is now on the stack; add any manipulation of
> that table here. */
>  return 1;
> }

Personnaly, I prefer :

LUAELALIB_API int luaopen_mylib(lua_State *L) {
        lua_newtable(L);
        luaL_register(L, NULL, mylib);
        return 1;
}

Then, if you profice others luaopen_* functions, you can easily change
the name of your module just by renaming the file.

You can also do tat in the lua modules because the lua module name is
passed as parameter.
        module((...), package.seeall)

Something Idon't like about the module() functiuon is that this
functiuon creates a global table that contains the module.
I prefer when no global table is created, then, the global environment
is always clean and you know what you put in there.
Any thoughts about that ?

--
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]
Reply | Threaded
Open this post in threaded view
|

Re: "module" tutorial

Thomas Lavergne
In reply to this post by Stockdale, Daire, SOE
--- foo.lua ---
module('foo', package.seeall)
function bip()
        print('bip')
end
---------------

--- bar.lua ---
require('foo')
foo.bip()
---------------

The most minimalist tutorial I can write ;-)

Hope this help you

Tom



On Thu, Apr 13, 2006 at 11:14:32AM +0100, Stockdale, Daire, SOE wrote:

> Hello
>
> Can anyone provide a minimalist tutorial on correct use of the "require"
> and "module" functions of 5.1 please, especially with reference to
> creating a native Lua module? Or direct me to one?
>
> Lots of thanks,
>
> daire
>
>
>  
> ========================================
> DISCLAIMER
>
> The contents of this e-mail and any attachments are confidential to the intended recipient and may also be legally privileged.  Unless you are the named addressee (or authorised to receive for the addressee) of this email you may not copy, disclose or distribute it to anyone else.  
>
> If you have received this email in error, please notify us immediately by e-mail on [hidden email] and then delete the email and any copies.  The SEGA Group have made all reasonable efforts to ensure that this e-mail and any attached documents or software are free from software viruses, but it is the recipient's responsibility to confirm this.
>
> ========================================
>

--
Thomas Lavergne                       "Le vrai rêveur est celui qui rêve
                                       de l'impossible."  (Elsa Triolet)
[hidden email]                           http://reveurs.org
Reply | Threaded
Open this post in threaded view
|

Re: "module" tutorial

Petite Abeille
In reply to this post by Mildred Ki'Lya

On Apr 13, 2006, at 23:11, Mildred wrote:

> Something Idon't like about the module() functiuon is that this
> functiuon creates a global table that contains the module.
> I prefer when no global table is created, then, the global environment
> is always clean and you know what you put in there.
> Any thoughts about that ?

Yes. This is a very unfortunate situation.

I was seriously contemplating re-implementing my class system in terms
of the module function, but this global namespace pollution is a
show-stopper :(

Any reason(s) for 'module' littering the global environment?

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/

Reply | Threaded
Open this post in threaded view
|

Re: "module" tutorial

Mildred Ki'Lya

> Any reason(s) for 'module' littering the global environment?

I think it's for compatibility with lua 5.0 ... no ?
But we also changed the whole module package, so why ? Moreover, the
documentation of the module function don't specify that it uses a
global table (unless the global table already exists :

> module (name [, ...])
>
> Creates a module. If there is a table in package.loaded[name], this
> table is the module. Otherwise, if there is a global table t with the
> given name, this table is the module. Otherwise creates a new table t
> and sets it as the value of the global name and the value of
> package.loaded[name].


--
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]
Reply | Threaded
Open this post in threaded view
|

Re: "module" tutorial

Greg Falcon
In reply to this post by Petite Abeille
On 4/13/06, PA <[hidden email]> wrote:
> I was seriously contemplating re-implementing my class system in terms
> of the module function, but this global namespace pollution is a
> show-stopper :(
>
> Any reason(s) for 'module' littering the global environment?

Probably because this is what modules are for:

C: #include <math.h>
C++: #include <cmath>
Java: import java.lang.Math;
Python: import math

Though the semantics of the above vary greatly depending on the
language, all of the above "litter" the global namespace with a symbol
(or in the case of C, many symbols) in order to provide math functions
to programmer.

If Lua modules by default did not set a global variable, then the
idiomatic usage of 'require' would probably look something like
  local foo = require "foo"
whose benefits don't seem to be worth the extra verbosity.

If one of 'module' or 'require' must set a global variable, it seems
better that 'module' should do it, since then it is possible to create
modules that don't affect the global namespace, if that's what you
really want.  (Perhaps you want to create a module that when required,
instead of introducing code, changes the behavior of the language or
host application in some way, similar to "from __future__ import
division" in Python.)  'require' works fine even if the module being
required does not use 'module'.  Chapter 15 of blue PiL gives a few
examples of modules that don't use 'module'.

Why do you want to use 'module' for your class system?  If your goal
is to be able to 'require' classes, then you could write a 'module'
workalike that doesn't affect the global namespace.  If your goal is
only to get the enviornment setting effects of 'module', then the
problem is you're trying to use the function as something it's not.
In either case, you probably should write your own function ('class'?)
that uses the same setfenv trick that module does.

My two cents,
Greg F
Reply | Threaded
Open this post in threaded view
|

Re: "module" tutorial

Pavol Severa
In reply to this post by Stockdale, Daire, SOE
>> I was seriously contemplating re-implementing my class system in terms
>> of the module function, but this global namespace pollution is a
>> show-stopper :(
>>
>> Any reason(s) for 'module' littering the global environment?

>Probably because this is what modules are for:
>
>C: #include <math.h>
>C++: #include <cmath>
>Java: import java.lang.Math;
>Python: import math

I don't know about other languages, but in python it is very common to use

import x as y

and also

from x import y

or even

from x import y as z

Although each of these polute the global namespace, you can choose the
name of the variable if you wish so.
(there is also the function __import__ that doesn't bind any variable,
just returns the module; it is probably used just in hacks)
The lua5.1 module system (if I understand it at all:) doesn't give you
any freedom for the choice of the name.
Coming from python I find it restrictive (maybe just because I still
think too much python when using lua, which is of course wrong). So I
would also like to know the reason why this approach to modules was
taken in lua - just to understand lua a bit better.

P.
Reply | Threaded
Open this post in threaded view
|

Re: "module" tutorial

Petite Abeille
In reply to this post by Greg Falcon

On Apr 14, 2006, at 21:19, Greg Falcon wrote:

> Probably because this is what modules are for:
> C: #include <math.h>
> C++: #include <cmath>
> Java: import java.lang.Math;
> Python: import math

You mean require()?

Lua: require( "something" )

> Why do you want to use 'module' for your class system?

Because it's there.

Considering the paucity of built-in functions, I was hoping to leverage
the few which are provided. But yes, you are right, why bother...
sigh...

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/

Reply | Threaded
Open this post in threaded view
|

Re: "module" tutorial

Petite Abeille
In reply to this post by Pavol Severa

On Apr 15, 2006, at 13:26, Pavol Severa wrote:

> The lua5.1 module system (if I understand it at all:) doesn't give you
> any freedom for the choice of the name.

Aside from the global environment pollution issue, you can refer to a
module whichever way you want:

local abacus = require( "math" )

abacus.min( 1, 2 )

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/