title: How can I structure a self-contained module in Lua?
body: <p>I'm making a Lua library for Love2D, which contains quite a bit of internal sub-modules, class files, and the like.</p>
  
  <p>What I'm doing now looks like this:</p>
  
  File <code>./libname/init.lua</code>
  
  <pre><code>lib.prefix = (...):match("(.-)[^%.]+$") .. "libname."
  lib = {}
  
  lib.class = require(lib.prefix .. "lib.class")
  lib.types.Blah = require(lib.prefix .. "types.Blah")
  
  return lib
  </code></pre>
  
  File <code>./libname/types/Blah.lua</code>
  
  <pre><code>local Blah = lib.class()
  ...
  return Blah
  </code></pre>
  
  <p>Except the thing here is that lib is a global, and if I make it into a local, I cannot properly structure the submodules like <code>Blah</code>, because they no longer have access to the <code>lib</code> table.</p>
  
  <p>This is obviously a stripped down example, but I think it demonstrates my problem well - I would like to make the <code>lib</code> table local, and return it, so that inclusion of the library goes like <code>lib = require "libs.libname"</code>, rather than have the whole thing imported into the global scope when I require the module itself. Is it possible?</p>