lua-users home
lua-l archive

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


2007/2/26, Norman Ramsey <nr@eecs.harvard.edu>:
I can't quite figure out what I might do with _PACKAGE after running
the module() function.  The manual says 'see below' and PIL says
'see the next section' but in neither case can I find an example or
an explanation.  Suggestions, anyone?

It can be used to locate sibling packages easily. Say I have an engine
called foo, with a foo.math module and a foo.physics module, you can
access the math module from the physics module through
require(_PACKAGE..".math"). Example:

-- foo/math.lua
module(..., package.seeall)
function poly(coefs, x)
   local fx = 0
   for i,coef in ipairs(coefs) do
       fx = fx + coef * x^i
   end
end
--------
-- foo/physics.lua
module(..., package.seeall)
local math = require(_PACKAGE.math)
function g(x)
   return math.poly({1,2,3}, x)
end
--------
As you can see neither module content mentions the name "foo", which
means it's really easy to reorganize your modules: you just have to
move files around. Moreover, in the example above the modules don't
even mention their own name, so renaming a module is just renaming
it's source file.