This article mainly introduces the basic usage of modules in Lua, is the basic knowledge in Lua's introductory learning, and the friends who need it can refer to
What is a module?
A module is an image that you can use to load and have a single global named library that contains a table. The module can contain several functions and variables. All of these functions and variables are wrapped in a table with it as a namespace. Also a very well-behaved module has the necessary requirements to return this form to the required.
LUA module
The use of the modules in the table can help us in a variety of ways so that we can manipulate the same way that we manipulate any other LUA tables in the module. As a result of the ability to manipulate modules, it provides additional functionality to the amount of languages that require special mechanisms. Because of the LUA module, in this free way, users can call the LUA function in a variety of ways. As in the following several:
The code is as follows:
--assuming we have a module printformatter
--Also Printformatter has a funtion simpleformat (ARG)
--Method 1
Require "Printformatter"
Printformatter.simpleformat ("Test")
--Method 2
Local formatter = require "Printformatter"
Formatter.simpleformat ("Test")
--Method 3
Require "Printformatter"
Local formatterfunction = Printformatter.simpleformat
Formatterfunction ("Test")
In the example code above, you can see how flexible programming Lua is without any special extra code.
function rules
LUA provides an advanced function called a requirement to load all necessary modules. It is kept as simple as possible to avoid too much information on the module to load it. The prescribed function simply assumes that the module defines some values as a block of code, which is actually a function or contains a function table.
Example
Let's consider a simple example where one of the functions has mathematical operations and allows the invocation of this module as MyMath and filename is Mymath.lua. The contents of the document are as follows.
The code is as follows:
Local MyMath = {}
function Mymath.add (a,b)
Print (A+B)
End
function Mymath.sub (a,b)
Print (A-b)
End
function Mymath.mul (a,b)
Print (A*B)
End
function Mymath.div (a,b)
Print (A/b)
End
Return MyMath
Now to access another file in this LUA module, Moduletutorial.lua, as shown below.
The code is as follows:
Mymathmodule = require ("MyMath")
Mymathmodule.add (10,20)
Mymathmodule.sub (30,20)
Mymathmodule.mul (10,20)
Mymathmodule.div (30,20)
To run the code, we need to put 2 LUA files in the same directory, or we can put the module files in the package path, and need to generate additional settings. When we run the above program, we get the output below.
The code is as follows:
30
10
200
1.5
Things to keep in mind
Run the file in the same directory as the module.
The module name and its file name must be the same.
This is the return module specified function, so the module can also be implemented well as shown in the previous illustration, even if other types of implementations are found to be best practices elsewhere.
The old ways to implement modules
Now rewrite the same example, which uses the old package method. View all types of implementations. This is in the use of LUA versions 5.1 and 5.0. The math module is shown below.
The code is as follows:
Module ("MyMath", Package.seeall)
function Mymath.add (a,b)
Print (A+B)
End
function Mymath.sub (a,b)
Print (A-b)
End
function Mymath.mul (a,b)
Print (A*B)
End
function Mymath.div (a,b)
Print (A/b)
End
The use of the Moduletutorial.lua module is shown below.
The code is as follows:
Require ("MyMath")
Mymath.add (10,20)
Mymath.sub (30,20)
Mymath.mul (10,20)
Mymath.div (30,20)
When we run above, we get the same output. However, it is recommended that the old version of the code be used, which is considered less secure. Many software development kits that use LUA programming like the Corona SDK are obsolete.