Lua study Note 6-Package

Source: Internet
Author: User

Like other languages, Lua also has the concept of modules/packages. A little different is that, by default, the Lua file is searched from the environment variable lua_path. If it cannot be searched, search for the c file from lua_cpath.

 

 

For me, changing the environment variables sometimes seems troublesome, and it is difficult to modify the environment variables during deployment in another environment. Therefore, it is better to add search paths as follows:

 -- Add the custom package path to the package search path. You can also add it to the environment variable lua_path <br/> Local p = "parent directory of the custom package path" <br/> Local m_package_path = package. path <br/> package. path = string. format ("% s; % s ?. Lua; % s? /Init. Lua ", <br/> m_package_path, p, p)

 

 

For example, if the file structure is as follows:

Blue indicates a folder, and purple indicates a file.

---------- Test. Lua

---------- Demopackage

--------------------- Init. Lua

--------------------- A. Lua

 

In this case, demopackage is a package. The demopackage function of the module is init. the Lua file is provided by the demopackage module. A by. the Lua file is provided. call the demopackage package in the Lua file:

 

-- Init. Lua File

 Module (..., package. seeall) </P> <p> function add (N1, N2) <br/> return N1 + N2 <br/> end </P> <p> Function Sub (N1, N2) <br/> return N1-N2 <br/> end </P> <p> function Div (N1, N2) <br/> If N2 ~ = 0 then <br/> return N1/N2 <br/> else <br/> error ("require N2 is not zero ") <br/> end </P> <p> function MUL (N1, N2) <br/> return N1 * N2 <br/> end <br/>

 

 

-- A. Lua

Module (..., package. seeall) </P> <p> function p () <br/> Print "module demopackage. A "<br/> end </P> <p> function lstostring (LS) <br/> return "{".. table. concat (LS ,",").. "}" <br/> end <br/>

 

In the above two files, there is such a line of code module (..., package. seeall), this is to name the module name with the file name, and to not pollute the global variable _ g, return the functions provided by the current module, and so on. Equivalent:

 

-- Set the module name as the file name and load the module

Local modname =...

Local m = {}

_ G [modname] = m

Package. Loaded [modname] = m

 

-- Introduce required modules

Local IO = Io

Local Print = print

 

Setfenv (1, m) --> set the current environment variable

 

-- Start to implement module functions

Function Test ()

Print ("test ")

End

 

......

 

 

Call the demo File

-- Test. Lua

-- Add the custom package path to the package search path. You can also add it to the environment variable lua_path <br/> Local p = "E:/DEP/code/Lua/" <br/> Local m_package_path = package. path <br/> package. path = string. format ("% s; % s ?. Lua; % s? /Init. lua ", <br/> m_package_path, p, p) </P> <p> -- print (package. path) --> Lua file search path <br/> -- print (package. cpath) --> Lua C file search path </P> <p> require "demopackage" <br/> require "demopackage. A "</P> <p> Print (" -------- package: demopackage -------------- ") <br/> for I in pairs (demopackage) DO <br/> Print (I, demopackage [I]) <br/> end </P> <p> Print ("-------- package: demopackage. A -------------- ") <br/> for I in pairs (demopackage. a) DO <br/> Print (I, demopackage. A [I]) <br/> end </P> <p> Print ("--------------- demo print --------------") <br/> Print (demopackage. add (1, 2) <br/> Print (demopackage. a. lstostring ({"first", "second"}) </P> <p> Print (demopackage. a. P () <br/>

 

 

In Lua, use require to load the package. to rename the loaded package, perform the following operations:

Local T = require "demopackage". At this time, you can use t to call the functions in demopackage.

 

When a BTW or Lua loads a package, it does not load all modules in the package. This is different from other languages. This should be done to improve the running speed.

 

 

 

 

 

 

 

 

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.