2

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...

Community
  • 1
  • 1
nodepond
  • 501
  • 6
  • 24
  • @Nicol Not really the same question. In particular because Lua 5.2 now requires the module to be referenced locally. – prapin Sep 29 '12 at 14:54
  • @prapin: No it doesn't. 5.2, like 5.1, doesn't have any particular requirements for what modules do. If you want to stick things in the global table in a module, you can. It's not *advised*, but you can do it. Indeed, while the `module` function is deprecated, it's *still there* and can be used. – Nicol Bolas Sep 29 '12 at 14:57
  • 1
    To be honest... why did this question get closed? This is not a duplicate to the indicated question... – nodepond Sep 30 '12 at 18:01
  • Bump. Ill jump on the pile. Someone please moderate this and remove the duplicate in title, "possible duplicate" heading etc... require / module is completely different issue. – Beeeaaar Mar 19 '13 at 06:19
  • Ok I tried to un-dup it by editing but was rejected by the "get my points for the day squad". I say this of course wiht all the love in the world. :) – Beeeaaar Mar 19 '13 at 06:51

2 Answers2

7

In Lua 5.2, well behaved modules do not export a global variable anymore, as they did in Lua 5.1.

In addition to the speed increase provided by the local variable, using local module = require "module" shall be used for compatibility with the newest Lua version.

prapin
  • 6,395
  • 5
  • 26
  • 44
4

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.

orlp
  • 112,504
  • 36
  • 218
  • 315
  • But somehow it doesn't. I only get a boolean varable returned from require("module"). Example: local xml = require("purexml") print (xml) -> true And then when I try: xml:collect() The console says: attempt to index local 'xml' (a boolean value) (still using Lua 5.1.4) – nodepond Sep 29 '12 at 15:22
  • Ah, I think I got it now!! Understand the module(..., package.seeall) or the alternative way to go with i.e. local M = {} -- public interface in the module file with return M at the end. Careful reading of this file helps: http://lua-users.org/wiki/ModuleDefinition – nodepond Sep 29 '12 at 15:30
  • 3
    It's not a performance-related thing. Using locals instead of setting stuff in the global space is a way of structuring the code so it gives less "surprises". Unfortunately not all lua libs follow this convention. – kikito Sep 29 '12 at 23:18