Latest web development tutorials

Lua module package

Module is similar to a library package, starting from Lua 5.1, Lua added standard module management mechanism, you can put some common code in one file, in the form of API interface calls in other places, in favor of code reuse and reduce Code coupling.

Lua module is the table from the variables, functions, and other known elements, thus creating a very simple module, is to create a table, then the need to export the constants, functions into which, finally returned to the table on the line. The following is creating a custom module module.lua, file code format is as follows:

-- 文件名为 module.lua
-- 定义一个名为 module 的模块
module = {}
-- 定义一个常量
module.constant = "这是一个常量"
-- 定义一个函数
function module.func1()
local function func2()
function module.func3()
return module

From the foregoing, the structure of the module is a table structure, so you can call the table as an element in the operation to operate as a constant in the calling module or function.

The above func2 declared as local variables block, that means a private function, and therefore can not be accessed from outside the private function module must be called by the module public function.

require function

Lua provides a function called require is used to load modules. To load a module, simply call it. E.g:



require "<模块名>"

Will return after execution require a constant or function by a module consisting of table, and will define a global variable that contains the table.

-- test_module.lua 文件
-- module 模块为上文提到到 module.lua

The above code is executed as a result of:


Or to load modules define an alias variable, easy call:

-- test_module2.lua 文件
-- module 模块为上文提到到 module.lua
-- 别名变量 m
local m = require("module")

The above code is executed as a result of:


Loading mechanism

For custom modules, not on which file directory will do, require function has its own file path loading strategy, it tries to load module file from Lua or C program library.

Require search path for Lua file is stored in the global variable package.path, after the Lua boots, LUA_PATH initial value of the environment variable to the environment variable. If you do not find the environment variable, the default path defined by a compile-time initialization.

Of course, if there is no LUA_PATH this environment variable, you can also customize the settings, open in the current user's home directory .profile file (it does not create, you can also open the .bashrc file), for example, the "~ / lua /" path to join LUA_PATH environment variable:

export LUA_PATH="~/lua/?.lua;;"

File path ";" separated by the final two ";;" stands for the newly added plus the original path of the default path.

Next, update your environment variables parameters to take effect immediately.

source ~/.profile

Then package.path assumed values ​​are:


So when calling require ( "module") will try to open the following file directories to search for targets.


If approached the target file will be called package.loadfile to load modules. Otherwise, go to C libraries.

File search path is package.cpath obtained from a global variable, and this variable is LUA_CPATH to initial environment variables.

Search strategy with the same as above, but now the search is so replaced or dll file type. If you find it, then it will require by package.loadlib to load it.

C package

Lua and C are easily combined using C to write Lua package.

Write a different package and Lua, C package before use must first be loaded and connected, in most systems is the easiest way to achieve dynamic link library mechanisms.

Lua provides all the functionality of dynamic linking in a function called loadlib. This function takes two arguments: the absolute path to the library and initialization function. So an example of a typical call is:

local path = "/usr/local/lua/lib/"
local f = loadlib(path, "luaopen_socket")

loadlib function loads the specified library and connected to Lua, but it does not open the library (ie no call initialization function), otherwise he returns initialization function as a Lua function, so that we can call him directly in Lua.

If the error occurred while loading a dynamic library or initialization function lookup, loadlib returns nil and error messages. We can modify the previous section of code to detect errors and then call the initialization function:

local path = "/usr/local/lua/lib/"
-- 或者 path = "C:\\windows\\luasocket.dll",这是 Window 平台下
local f = assert(loadlib(path, "luaopen_socket"))
f()  -- 真正打开库

Under normal circumstances we expect the release of the binary library contains a previous code snippet similar stub file, the installation binaries can be easily placed in a directory, you only need to modify the binary library stub file corresponds to the actual path.

The directory where the file was added to the stub LUA_PATH, this setting can be used after loading the C library functions require the.