Basic tutorial on using modules in Lua
This article mainly introduces the basic usage of modules in Lua. It is the basic knowledge of Lua beginners. For more information, see
What is a module?
A module is an image. You can use a library that needs to be loaded and contains a single global name in the table. This module can contain several functions and variables. All these functions and variables are wrapped in a table that acts as a namespace. It is also a necessary rule for a very good module to return what is needed on this table.
Lua Module
The use of modules in tables can help us to manipulate modules in the same way as any other lua tables. As a result of the ability to manipulate the module, it provides additional features such as the amount of languages that require special mechanisms. Because of the lua module, you can call the Lua function in multiple ways in this free mode. For example:
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 above sample code, we can see how to flexibly program Lua without any special extra code.
Function provision
Lua provides an advanced function called to specify to load all necessary modules. It is kept as simple as possible to avoid loading too much information on the module. This function only assumes that the module defines some values as a code block, which is actually a function or contains a function table.
Example
Let's take a simple example. One function has a mathematical operation function and allows calling this module as mymath and the file name is mymath. lua. The content of this file is 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
To access another file moduletutorial. lua In The lua module, the following figure shows the result.
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 two lua files in the same directory, or the module files in the package path, and create additional settings. When we run the above program, we will get the following output.
The Code is as follows:
30
10
200
1.5
Things to remember
Run the file in the same directory as the module.
The module name and its file name must be the same.
This is a function defined by the return module. Therefore, this module can be well implemented, as shown in. Even if you can find the best practices for other types of implementations.
Old methods for implementing modules
Now rewrite the same example, using the old package method. View all types of implementations. This is when Lua version 5.1 and 5.0 are used. The math module is as follows.
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 usage of the moduletutorial. lua module is as follows.
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 the above command, we will get the same output. However, we recommend that you use an earlier version of the code, which is considered unsafe. Many software development kits programmed using the Corona SDK in Lua are outdated.