Lua-modules and packages, Best Practices

Source: Internet
Author: User

 

Recently, Lua has been learning many questions about the use of modules and packages, some of which are referenced and used. Some of which are: what is the situation? This afternoon! A Package is a collection of modules: A Package is a collection of modules. starting from Lua 5.1, you can use the require and module functions to reference and create modules in Lua respectively. The simple method to call functions in the module is [java] view plaincopyrequire "mod" mod. foo () if the module name is too long, you can set a local variable [java] local m = require "mod" m. foo () can also rename functions in the module: [java] require "mod" local f = mod. foo f () because the concept of a module is based on a Table, when creating a module, you must set the module name, declare module M, and perform other operations as follows: 1 -- set the module name to the require parameter, in this way, when renaming a module, you only need to rename the file name. 2 local modname =... 3 local M = {} 4 _ G [modname] = M 5 6 M. I = {r = 0, I = 1} -- defines constants in a module. 7 function M. new (r, I) return {r = r, I = I} end 8 function M. add (c1, c2) 9 return M. new (c1.r + c2.r, c1. I + c2. I) 10 end11 12 function M. sub (c1, c2) 13 return M. new (c1.r-c2.r, c1. I-c2. I) 14 end15 -- returns the table corresponding to the module. 16 return M, but the above is too complex and repetitive. Therefore, after 5.1, a module function is introduced to encapsulate the above basic functions. Add module (..., package. you can declare the current lua file as a module without declaring the current module name. A sub-module uses. to differentiate the level of a name. For example, a module named mod. sub is a sub-module of mod, And a package is the full tree of all modules. When to use. or: [java] function Account. withdraw (self, v) self. balance = self. when balance-v end calls the above method: [java] a1 = Account; Account = nil... a1.withdraw (a1, 100.00) -- OK or without self: [java] function Account: withdraw (v) self. balance = self. balance-v end usage: a: withdraw (100.00) Best practices for LuaModule usage: Define the referenced name [java] // in the main. lua file require "logging. test "require" test22 "local function main () print (" OOLL ") basi. beforePrint () see. beforePrint () end // logging. test. lua, logging is a folder module ('basi', package. seeall) function beforePrint () print ("IOK") end // test2.lua and main. lua is located in the same folder module ('use', package. seeall) function beforePrint () print ("insss") end output result: OOLLIOKinsss

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.