title: Lua: setfenv() vs. _ENV
body: <p>What is the big deal with Lua switching from <code>setfenv()</code> to <code>_ENV</code>?</p>
  
  <p>In various "What's New" sources, this move is mentioned as one of the most important changes between Lua versions 5.1 and 5.2.</p>
  
  <p>However, the examples given in PIL and elsewhere can be trimmed to the following:</p>
  
  <pre><code>-- Lua 5.1                   -- Lua 5.2
  function myfunc()            function myfunc()
      setfenv(1, {})               _ENV = {}
  end                          end
  </code></pre>
  
  <p>So far, what we gained here is we have saved five key strokes. (I believe the situation is not much different from the C side.) Moreover, if I got it right, <code>setfenv()</code> can be used both from the outside and from the inside of a function, whereas <code>_ENV</code> can be accessed only from the inside of a function. (Of course, when using the C API, one can access the upvalues directly.) From what I wrote, the 5.2 approach seems much less flexible.</p>
  
  <p>In his <a href="http://www.inf.puc-rio.br/~roberto/talks/novelties-5.2.pdf" rel="noreferrer">The Novelties of Lua 5.2</a>, Roberto writes:</p>
  
  <p><em>"Being a syntactic sugar, it is much simpler than the old environments"</em> </p>
  
  <p>Where's the simplicity? What have I overlooked?</p>
  
  <p>I believe this topic is worth better treatment that it is given in the 5.2 User Manual.</p>