[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [ANN] Lua 5.2.0 (work4) now available
- From: David Manura <dm.lua@...>
- Date: Sat, 7 Aug 2010 00:08:58 -0400
On Fri, Aug 6, 2010 at 5:22 PM, Petite Abeille <petite.abeille@gmail.com> wrote:
> On Aug 6, 2010, at 11:01 PM, Matthew Wild wrote:
>> the caller now *only* needs to know the name of the
>> module on disk (or wherever it is being loaded from).
>
> Which limits one to have a one-to-one representation
> between module and their physical incarnation. Which
> is not a step forward as one cannot define multiple modules
> in one file or a module across multiple files or in dynamically
> loaded chunks... or any combination of the above. Now instead
> one will have to mess with package.loaded directly on a case
> by case scenario, instead of letting module deal with it.
If you want to split the module up into multiple files, you may do
some variant of this:
-- bar-core-private.lua
return {}
-- bar-baz-private.lua
local M = require "bar-core-private"
function M.baz() print 'baz' end
return M
-- bar.lua
local M = require "bar-core-private", require "bar-baz-private"
function M.foo() print 'foo' end
return M
-- note: safe to reuse (rather than copy) M if user code never
requires private modules directly.
-- And for dynamically loaded chunks (e.g. Perl style AUTOLOAD), set
__index on M calling require.
-- main.lua
local bar = require "bar"
bar.foo(); bar.baz()
and to combine modules into one file, you may mechanically translate
(to use the example above) into some variant of this:
-- bar-archive.lua
package.preload["bar-core-private"] = function()
return {}
end
package.preload["bar-baz-private"] = function()
local M = require "bar-core-private"
function M.baz() print 'baz' end
return M
end
package.preload["bar"] = function()
local M = require "bar-core-private", require "bar-baz-private"
function M.foo() print 'foo' end
return M
end
-- main.lua
require "bar-archive" -- called only for side-effect; alternately
use "lua -lbar-archive main.lua"
local bar = require "bar"
bar.foo(); bar.baz()
I think the main use of combining modules though is for archiving
purposes (i.e. the Java jar/zip or C archive concept or the Lua links
at the bottom of [1]), optionally with a custom package.loaders
implementation.
[1] http://lua-users.org/wiki/BinToCee