lua-users home
lua-l archive

Re: [Feature request]: analogous to --- if __name__ == “__main__”: ---- of python in lua modules

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


in loadlib.c:

/* check whether table already has a _NAME field */
lua_getfield(L, -1, "_NAME");
if (!lua_isnil(L, -1)) /* is table an initialized module? */
lua_pop(L, 1);
else { /* no; initialize it */
lua_pop(L, 1);
modinit(L, modname);
}

So, if the module did not set _NAME, lua will help the module to do that.


At 2014年09月24日 22:48:28, "Peng Zhicheng" <pengzhicheng1986@gmail.com> wrote: >On 09/22/2014 08:22 PM, zzf wrote: >> >> in lua module >> if _NAME == nil then >> test() >> end >> equals >> if __name__ == “__main__”: >> >> like this: >> >> Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio >> > require('md5') >> > print(md5._NAME) >> md5 >> > >> >No, you have taken it wrong -- the _NAME field of the module is set by the module itself, not by Lua. >if the module does not set it, it won't be there. > >if you check your md5.lua, you would probably see something like this: > >~~~ >module("md5", package.seeall) >_NAME = "md5" >_VERSION = <...> >_COPYRIGHT = <...> ><...> >~~~ > > > >



AltStyle によって変換されたページ (->オリジナル) /