lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, Apr 10, 2013 at 11:01 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
I was bitten for about two weeks by the possibility of doing as
in your example, but quickly discovered that having the standard
library functions undefined except those cached as upvalues
was intensely annoying.

Oh it is, and the various tricks to restore a fake global environment are troublesome and inefficient (package.seeall, anybody?)

Although it's still useful to put required globals up front for documentation and performance reasons.

This is one of the services that the before-mentioned lglob provides. A silly example:

-- one.lua
local M = {}
function M.join(t)
  return table.concat(t,',')
end
function M.hex(s)
  return tostring(s,16)
end   
function M.sin(n)
  return math.sin(n)
end
return M

$ lglob -gd one.lua
local tostring = tostring
local table,math = table,math

(end of infomercial)

Remembering to return the module is one of those things a person forgets, especially if they're used to module.

function newmod(name)
  local mod = {}
  package.loaded[name] = mod
  return mod
end

local M = newmod(...)
...

And then voila, no need to return M at the end!