Module Definition

lua-users home

There are many ways to define a "module" [1] in Lua.

From a Table

-- mymodule.lua
local M = {} -- public interface

-- private
local x = 1
local function baz() print 'test' end

function print("foo", x) end

  print "bar"

return M

-- Example usage:
local MM = require 'mymodule'

This is a common approach. It is simple, relies on no external code, avoids globals, and has few pitfalls. Externally facing variables are prefixed by "M." and clearly seen.

See Also

RecentChanges · preferences
edit · history
Last edited February 19, 2015 5:01 pm GMT (diff)