Possible Duplicate:
Why make global Lua functions local?
In many lua-scripts from the community I see something like to add a module to a script:
local module = require("module")
But according to the lua-manual, this is not the way, to load modules. The modules itself have their module-name inside, so that
require("module")
is just enough to use the module like this: module.myfunction(). The first example with the local-definition just writes “true” into the module-var – indicating, that the module has loaded successfully.
The stange thing is, that I see this kind of loading “local module = require(“module”)” everywhere in the web. Most of the scripts from the lua-community I do not get to working because of this error. I also wonder why I do not found any issues about this on the web yet.
The next thing is, that also actually loading the modules sometimes results in strange errors. For example: I have a script with
require("purexml.lua")
than I got this error:
no field package.preload['purexml.lua']
no file './purexml/lua.lua'
no file '/usr/local/share/lua/5.1/purexml/lua.lua'
no file '/usr/local/share/lua/5.1/purexml/lua/init.lua'
no file '/usr/local/lib/lua/5.1/purexml/lua.lua'
no file '/usr/local/lib/lua/5.1/purexml/lua/init.lua'
no file './purexml/lua.so'
no file '/usr/local/lib/lua/5.1/purexml/lua.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file './purexml.so'
no file '/usr/local/lib/lua/5.1/purexml.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
But doing this works fine:
require("purexml")
Am I missing something really obvious here? I use Lua 5.1.4 by the way…
local module = require("module")creates a local variable containing the module, in addition to the default global (the local shadows the global, of course). Locals are a lot faster in Lua to access, so performance is most likely the reason this is used.And the error you get is obvious, you mustn’t add the file extension while loading modules.