lua - How to require multiple modules in a single statement? -


i want require several lua modules @ once, similar asterisk signifier java (import java.awt.*). structure organized modules in subdirectories:

<myapp>  -- calculations     -- calc1     -- calc2     -- calc3  -- helper      -- help1      -- help2      -- print           --graphprinter           --matrixprinter 

my client requires each module of subpath:

 local graphprinter = require("myapp.helper.print.graphprinter")  local matrixprinter = require("myapp.helper.print.matrixprinter") 

i prefer automatic multi-require, derives local table names module path , requires whole subpath @ once. format: require("myapp.helper.print.*"). automatically local table names should created each module of subdirectory, there isn't difference have required them module module.

the module env partially achieves looking for, though far perfect.

it allows grouped / named imports, caveats - main 1 being must manually manage environments. in addition, you'll need write index files (default init.lua, unless write custom path set), since intended use modules export tables.

here's bit of example. first need set our file structure.

 -- main.lua  -- calculations /     -- calc1.lua     -- calc2.lua     -- calc3.lua     -- init.lua  -- helper /      -- print /           -- init.lua           -- graphprinter.lua           -- matrixprinter.lua 

the index files, tedious:

-- calculations/init return {     calc1 = require 'calculations.calc1',     calc2 = require 'calculations.calc2',     calc3 = require 'calculations.calc3' } 

and

-- helpers/print/init return {     graphprinter = require 'helper.print.graphprinter',     matrixprinter = require 'helper.print.matrixprinter' } 

inside main file. major caveat shows quickly, must use function returned requiring 'env' override local environment. passing no arguments create clone of current environment (preserving require, etc.).

-- main.lua local _env = require 'env' () -- (see notes below) 

the new environment given import function, takes single argument, string represent path or module name import current environment. return value transient table can used further alter environment state.

import 'helper/print' :use '*' import 'calculations' :use '*' 

one of functions on transient table :use, either takes table indicating values pull required table, or string '*', indicates want all values required table placed in current environment

print(matrixprinter, graphprinter) --> function: 0x{...} function: 0x{...} (or so) 

the final caveat paths you've seen reliant on cwd being same 1 holds main.lua. lua myapp/main.lua fail loudly, unless place sub modules in static location, , adjust package.path / import.paths correctly.

seems lot of work avoid couple of lines of require statements.


disclaimer: wrote env bit of experiment.

note import not support . syntax (you need use os path delimiter), or proper exploding of tables table chains. have bit of patch in works addresses this.

lua 5.2+ uses _env override local environments. lua 5.1, you'll need use setfenv.


as mentioned above, lua has no real notion of directories. want (with less overhead), you'll need write own custom module loader, environment handler, , make use of module luafilesystem reliably 'automatically' load files in directory.


tl;dr:

  • this tricky topic.
  • there's nothing built language make easy.
  • you'll need write custom.
  • there drawbacks.

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -