Organizing modules into packages
- Lua allows modules to be organized into packages
- This is similar to the packages of Java
- Folders are created with Lua files in them
- The dot notation specifies the package hierarchy
- It also specifies the folder hierarchy
An example of the code in a module
newModule = {} local masterTable = {} local i = 1 function newModule.addString(inputVariable) if type(inputVariable) ~= "string" then print("Error - input is not a string") else masterTable[i] = inputVariable i = i + 1 end end function newModule.printStrings() for j = 1, i-1 do print(masterTable[j]) end end return newModule
An example of using Lua modules and packages
local testModule = require "moduleSample" local anotherModule = require "modules.sample" testModule.addString("This is the first string") testModule.printStrings() anotherModule.addString("This is the first string from the modules package") anotherModule.printStrings()