Lua module and Package learning notes _lua

Source: Internet
Author: User
Tags constant lua

Starting with Lua 5.1, LUA has joined the standard modular management mechanism to put some common code in a file, calling it elsewhere in the form of an API interface, which facilitates code reuse and reduced code coupling.

Creating a Module

In fact, the Lua module is a table of known elements, such as variables and functions, so creating a module is simple, creating a table, and then putting the constants and functions that need to be exported into them, and finally returning to the table. The format is as follows:

Copy Code code as follows:

--Define a module named module
module = {}

--Define a constant
Module.constant = "This is a constant"

--Define a function
function Module.func1 ()
Io.write ("This are a public function!\n")
End

Local function Func2 ()
Print ("This is a private function!")
End

function module.func3 ()
Func2 ()
End

Return module

From the above, the structure of a module is the structure of a table, so you can manipulate the constants or functions in the calling module as you would an element in the call table. However, the above FUNC2 is declared as a local variable of the program block, which means a private function, so it is not possible to access the private function from the outside of the module, it must be called by a common function in the module.

Finally, save the above module code in a LUA file like the module name (for example, Module.lua above), and a custom module is created successfully.

Loading modules

Lua provides a function called require to load a module, and it's simple to use, and it has only one parameter, which is to specify the module name to load, for example:

Copy Code code as follows:

Require ("< module name >")
--or a
--Require "< module name >"

A table consisting of a module constant or function is then returned, and a global variable that contains the table is also defined.

Or define an alias variable for the loaded module to facilitate the invocation:

Copy Code code as follows:

Local m = Require ("module")

Print (m.constant)

M.FUNC3 ()

Loading mechanism

For a custom module, the module file is not in which file directory, the function require has its own file path loading policy, and it tries to load the module from the Lua file or the C program library.

The path that require uses to search for Lua files is stored in the global variable Package.path, and when Lua starts, the environment variable is initialized with the value of the environment variable Lua_path. If the environment variable is not found, it is initialized with a compile-time defined default path.

Of course, if you do not lua_path this environment variable, you can also customize the settings to open the. profile file under the current user root (not created, open the. bashrc file, as well), such as adding the "~/lua/" path to the LUA_PATH environment variable:

Copy Code code as follows:

#LUA_PATH
Export lua_path= "~/lua/?" LUA;; "

The file path is delimited with the ";" number, and the last 2 ";" indicates that the new path is followed by the original default path.

Next, update the environment variable parameters so that they take effect immediately:

Copy Code code as follows:

SOURCE ~/.profile

This assumes that the value of the Package.path is:

Copy Code code as follows:

/users/dengjoe/lua/? Lua;. /?. LUA;/USR/LOCAL/SHARE/LUA/5.1/? LUA;/USR/LOCAL/SHARE/LUA/5.1/?/INIT.LUA;/USR/LOCAL/LIB/LUA/5.1/? Lua;/usr/local/lib/lua/5.1/?/init.lua

Then the call to require ("module") will attempt to open the following file directory to search for the target

Copy Code code as follows:

/users/dengjoe/lua/module.lua;
./module.lua
/usr/local/share/lua/5.1/module.lua
/usr/local/share/lua/5.1/module/init.lua
/usr/local/lib/lua/5.1/module.lua
/usr/local/lib/lua/5.1/module/init.lua

If the destination file is found, Package.loadfile is invoked to load the module. Otherwise, you will find the C program library. The file path of the search is obtained from the global variable Package.cpath, and this variable is initialized by the environment variable Lua_cpath. The search strategy is the same as above, except that you are now searching for files of so or DLL type. If you can find it, then require will load it through Package.loadlib.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.