lua-users home
lua-l archive

Re: require vs dofile and how to organise many functions

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


On Jul 22, 2009, at 12:19 PM, startx wrote:
not that i want to discuss taste only ;) ... but what would be your
alternative suggestion to organise a huge bunch of functions? im open
to try different flavours ...
There are many ways to skin that cat, but I personally use module( 'MyModule' ) and that's all. In general, one file, one named module, one private name space, one public API exposed through meta methods.
For example, assuming a module 'HMAC':
http://dev.alt.textdrive.com/browser/HTTP/HMAC.lua
% cat HMAC.lua
-- import external dependencies
local getmetatable = getmetatable
local setmetatable = setmetatable
-- explicitly name the module
module( 'HMAC' )
-- add meta table when appropriate
local self = setmetatable( _M, {} )
local meta = getmetatable( self )
-- do stuff in the privacy of the module
local function HMAC( aKey, aValue )
end
-- expose stuff to the outside
function meta:__call( aKey, aValue )
 return HMAC( aKey, aValue )
end
FWIW, the above convention is followed by Nanoki:
http://dev.alt.textdrive.com/browser/HTTP
As with many other things in Lua, it takes a while to come up with an organizational style which consistently support one programming mannerism due to Lua's inherent flexibility.
http://lua-users.org/wiki/LuaStyleGuide
Cheers,
PA.

AltStyle によって変換されたページ (->オリジナル) /