module
Lua function

module

Summary

Creates a Lua module

Prototype

module (name, ···)



Description

Creates a module. This is intended for use with external "package" files, however it can be used internally as shown in the example below. The module effectively has its own global variable space (because module does a setfenv) so that any functions or variables used in the module are local to the module name (for example, foo.add in the example below).

If there is a table in package.loaded[name], this table is the module. Thus, if the module has already been requested (by a require statement) another new table is not created.

Otherwise, if there is a global table t with the given name, this table is the module.

Otherwise creates a new table t and sets it as the value of the global name and the value of package.loaded[name].

This function also initializes t._NAME with the given name, t._M with the module (t itself), and t._PACKAGE with the package name (the full module name minus last component).

Finally, module sets t as the new environment of the current function and the new value of package.loaded[name], so that require returns t.

The example below shows the creation of the module "foo". In practice you would probably put the contents of the "test" function into a separate file, and then: require "test"

The nice thing about this approach is that nothing inside the module will "pollute" the global namespace, excepting the module name itself (foo in this case). Internally inside the module functions can call each other without having to use the package name (eg. add could call subtract without using foo.subtract).

You can make a "private" function inside the "foo" package by simply putting "local" in front of the function name.


function test ()
  local print = print  --> we need access to this global variable
  
  module "foo"  --> create the module now
  
  function add (a, b)
    return a + b
  end -- add
  
  function subtract (a, b)
    return a - b
  end -- subtract

  function hello (s)
    print ("hello", s)
  end -- hello

end -- function test

test ()  -- install module

foo.hello ("world")   --> hello	world
print (foo.add (2, 3))  --> 5
print (foo.subtract (7, 8))  --> -1

print (package.loaded["foo"]) --> table: 003055F0
print (foo)  --> table: 003055F0

for k, v in pairs (foo) do
  print (k, v)
end -- for 

-->

_M	table: 003055F0
_NAME	foo
_PACKAGE	

hello	function: 00305810
subtract	function: 00305760
add	function: 00305780


After the module has been created, we can see that:

foo._M is foo itself (ie. the module)
foo._NAME is "foo"
foo._PACKAGE is an empty string (if the module was "foo.bar" then _PACKAGE would be "foo.")



See Also ...

Topics

DOC_lua_base Lua base functions
DOC_lua_bc Lua bc (big number) functions
DOC_lua_bit Lua bit manipulation functions
DOC_lua_coroutines Lua coroutine functions
DOC_lua_debug Lua debug functions
DOC_lua_io Lua io functions
DOC_lua_math Lua math functions
DOC_lua_os Lua os functions
DOC_lua_package Lua package functions
DOC_lua_rex Lua PCRE regular expression functions
DOC_lua Lua script extensions
DOC_lua_string Lua string functions
DOC_lua_tables Lua table functions
DOC_lua_utils Lua utilities
DOC_scripting Scripting

Lua functions

LUA_assert assert (Asserts that condition is not nil and not false)
LUA_collectgarbage collectgarbage (Collects garbage)
LUA_dofile dofile (Executes a Lua file)
LUA_error error (Raises an error message)
LUA_gcinfo gcinfo (Returns amount of dynamic memory in use)
LUA_getfenv getfenv (Returns the current environment table)
LUA_getmetatable getmetatable (Returns the metatable for the object)
LUA_ipairs ipairs (Iterates over a numerically keyed table)
LUA_load load (Loads a chunk by calling a function repeatedly)
LUA_loadfile loadfile (Loads a Lua file and parses it)
LUA_loadlib loadlib (Loads a DLL (obsolete in Lua 5.1))
LUA_loadstring loadstring (Compiles a string of Lua code)
LUA_next next (Returns next key / value pair in a table)
LUA_pairs pairs (Traverse all items in a table)
LUA_pcall pcall (Calls a function in protected mode)
LUA_print print (Prints its arguments)
LUA_rawequal rawequal (Compares two values for equality without invoking metamethods)
LUA_rawget rawget (Gets the value of a table item without invoking metamethods)
LUA_rawset rawset (Sets the value of a table item without invoking metamethods)
LUA_require require (Loads a module)
LUA_select select (Returns items in a list)
LUA_setfenv setfenv (Sets a function's environment)
LUA_setmetatable setmetatable (Sets the metatable for a table)
LUA_tonumber tonumber (Converts a string (of the given base) to a number)
LUA_tostring tostring (Converts its argument to a string)
LUA_type type (Returns the type of a variable)
LUA_unpack unpack (Unpacks a table into individual items)
LUA_xpcall xpcall (Calls a function with a custom error handler)

(Help topic: lua=module)

DOC_contents Documentation contents page