simple module.

Ok, i’m feeling really stupid here. I just want to create a simple code module where I can share some functions globally. I’ve seen other posts discuss this, and i’ve tried to replicate a simple example, but i’m at a loss here. I was following the example from this URL:
lua-users wiki: Modules Tutorial and
http://forum.micasaverde.com/index.php/topic,8237.msg52500.html#msg52500

I’ve created a simple file called “mymodule.lua” and uploaded it through the “Luup files” area.

Contents:

local mymodule = {}

function mymodule.foo()
	return true
end

return mymodule

In the “test luup code” area, i’m trying to call it

local mymodule = require("mymodule")
mymodule.foo()

The end result of these few lines of code is an infuriating “code failed”. Is there another example I can look at? What am I doing wrong?

There are multiple ways to do modules in LUA … they do not all work in Vera.

The following template should work for filename XXX.lua (actually stored as XXX.lua.lzo on Vera when uploaded)

lua module …

module("XXX", package.seeall)
...
function foo()
   luup.log("foo")
end

local function oops()
   luup.log("oops")
end

function baz()
   luup.log("baz")
   oops()
end
-- The following records the function in the global name space
_G.baz = baz

Then in your startup LUA:

---- Refer to this module with global variable X
X = require(XXX)

Then in your startup code or any scene LUA

-- the following should all work
X.foo()
X.baz()
baz()
-- the following will fail
foo()
X.oops()

Outstanding! That’s exactly what I needed.