Upper n-level module path function sharing in Lua

Source: Internet
Author: User

It's not easy to use modules in LUA development, where LUA file positioning is less straightforward.

For example, there are the following file structures:

Suppose me is a module that is being developed. And because the Require function in Lua uses a path relative to the LUA interpreter, LUA does not. /Such an upper-level file, so if Ti/me/pk/folder.lua want to invoke the Ti/me/na module, it must be written out as a complete path. That is, the call statement in Floder.lua is require ("ti.me.na").

But if the Me module is not under Ti file, it is wrong to write when someone else is using it in the Xx/xxx folder. So do some processing in the Floder.lua file to ensure the correctness of the calling file.

It's going to be used in Lua. , in Lua ... Represents a mutable parameter, while ... In the Lua file domain (not in the function ...) ) represents the location of the current module. For example, in Folder.lua ... Is "Ti.me.pk.folder". Pay attention to the Init.lua in ... Represents "Ti.me.na" because this module is ti.me.na instead of ti.me.na.init.

So when Folder.lua calls Ti/me/na with require, it needs to be used first ... (denoted "Ti.me.pk.folder" in Folder.lua) find "ti.me" (up to level two) and connect the string ". Na" (Down level). The key is to generate an up-n-level module path, so we've written the following up n-level auxiliary functions:

---Get the upper n level of the LUA module path, and the returned string has a "." Character at the end--@usage getupfloder ("Math.base.core", 2)--will return "math."--@string the module path string that you want to process--@int up n-level, optional parameter, default = 1Local functiongetupfloder (path, N)--Error CheckingPath = Pathor ""N= Nor 1    assert(type(path) = ="string","The parameter passed in is not a string type! ")    --find each level of a folder    LocalPath_part = {}     forWinch String.gmatch(Path,"%.? [^%.] +") Dopath_part[#path_part +1] =W    End    --generate the required parts    LocalNew_path =""     fori =1, #path_part-N DoNew_path=new_path. Path_part[i]End    --return    returnNew_path."."End

You can get the module path of Na by writing this folder.lua:

Welcome to the wrong point, of course, if you have a better way also please @ I told me!

Upper n-level module path function sharing in Lua

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.