Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Modules and module functions in Lua


May 12, 2021 Lua



This article focuses on modules and packages in Lua, and explains the need function, writing a module, package.loaded, module function, and more.

Since the Lua5.1 release, new support has been added to modules and packages, but require and modules are used to define and use modules and packages. R equire is used to use modules, modules are used to create modules. S imply said, a module is a library that can be loaded by need. T hen you get a global variable that represents a table. T his table is like a namespace, its content is everything exported in the module, such as functions and constants, and a compliant module should also have the require return to the table. L et's summarize the need and module functions in detail now. Such as:

require "mod"
mod.foo()
local m2 = require "mod2"
local f = mod2.foo
f()

1. Require function:

The require function is called in the form of require "module name". T he call returns a table consisting of module functions and defines a global variable that contains the table. Y ou can use the standard libraries in Lua without the call need that appears because Lua has preloaded them.

When the require function loads a module in search of a search, it has a set of custom patterns, such as:
?;?. l ua; c :/windows/?;/ u sr/local/lua/?/?. Lua
In the mode above, only the question mark (?) and the half sign (;) i s a pattern character that represents the argument (module name) of the require function and the separator between patterns, respectively. If you call require "sql", the following files will open:
Sql
sql.lua
c:/windows/sql
/usr/local/lua/sql/sql.lua
Lua places the pattern string of the require search in the variable package.path. W hen Lua is started, the variable is LUA_PATH the value of the environment variable. I f the environment variable is not found, it is initialized using a default path defined at compile time. I f you can't find a Lua file that matches the module name, you'll look for the C library. th. This variable is initialized by LUA_CPATH environment variable.

2. Basic methods for writing modules:

Create a new file named game .lua code as follows:

local M = {};
local modelName = ...;
_G[modelName] = M;
function M.play()
    print("那么,开始吧");
end
function M.quit()
    print("你走吧,我保证你不会出事的,呵,呵呵");
end
return M;

Load the game .lua code as follows:

game = require "test"

game.play()

Run:

lua -e "io.stdout:setvbuf 'no'" "HelloWorld.lua"
So, let's get started
Exit code: 0

3. Environment in use:

Read the code in the example above carefully and we can find some details on the problem. F or example, calls between functions within a module still want to preserve the qualifier for the module name, and if it is a private variable, you need to add the local keyword, and you cannot add a module name qualifier. /b11>If you need to change private to public, or the other step, you need some modification. S o how do you avoid these problems? We can effectively solve these problems through Lua's function "global environment".

We set the game .lua the global environment in this module to M, so we don't need to have an M prefix when we define the function directly.
Because the global environment at this time is M, the variable is defined without a prefix, which is the global variable, and the global variable is saved in M.
So, in fact, the play and quit functions are still in the M table.

local M = {};
local modelName = ...;
_G[modelName] = M;
package.loaded[modname] = M
setfenv(1, M);
function play()
    print("那么,开始吧");
end
function quit()
    print("你走吧,我保证你不会出事的,呵,呵呵");
end
return M;

4. Module function:

In Lua 5.1, we can replace the following code with the module(...) function, such as:

local modname = ...
local M = {}
_G[modname] = M
package.loaded[modname] = M
     --[[
     和普通Lua程序块一样声明外部函数。
     --]]
setfenv(1,M)

That is:

module(..., package.seeall);

function play()
    print("那么,开始吧")
end

function quit()
    print("你走吧,我保证你不会出事的,呵,呵呵");
end

Because module does not provide external access by default, the appropriate local variables must be declared for external functions or modules that need access before calling it. Lua then provides a more convenient way to implement that when the module function is called, an argument of one more package.seeall is passed in, which is equivalent to setmetable (M, s__index s _G).

Such as:

module(...,package.seeall)