title: Recommended way to have 2+ modules recursively refer to each other in Lua 5.2
body: <p>Is there a way to have</p>
  
  <ul>
  <li>Two Lua modules (let's call them <code>A</code> and <code>B</code>)</li>
  <li>Each module uses functions from the other, so they must <code>require</code> each other</li>
  <li>A third module (let's call it <code>C</code>) can use <code>A</code> but not <code>B</code> e.g.</li>
  </ul>
  
  <p><code>C.lua</code>:</p>
  
  <pre><code>local A = require 'A'
  
  -- ...
  
  A.foo()
  </code></pre>
  
  <ul>
  <li>There may be another module <code>D</code> that requires <code>B</code> but not <code>A</code> and/or <code>E</code> requiring both <code>A</code> and <code>B</code></li>
  <li>Neither <code>A</code> nor <code>B</code> nor their members should be added to the global namespace.</li>
  <li>Avoid using the <code>module</code> and <code>setfenv</code> functions (deprecated in Lua 5.2)</li>
  </ul>
  
  <hr>
  
  <p><strong>Related</strong>: <a href="https://stackoverflow.com/questions/7939774/lua-how-do-i-use-one-lib-from-another">Lua - how do I use one lib from another?</a> (note: this solution does not handle circular dependencies.)</p>