|
Am 15.04.2013 08:38 schröbte Dirk Laurie:
[...] You can tell the programs written by the supporters of this ideology. They start out something like this: local pairs, ipairs, print, tostring, getmetatable, setmetatable = pairs, ipairs, print, tostring, getmetatable, setmetatable
IMHO, that has the following advantages (in the context of modules):- you document the dependencies of your module (even though Lua's standard library is small, there are still cases where not all standard functions are available) - if used like 'local setmetatable = assert( setmetatable )' you fail early, not in the middle of something important, if a required function is not available - no one can mess with your functions once you have them copied (whether you consider this a good thing depends on how you think about monkey patching) - luac5.1 -l also lists line numbers -- if "luac5.1 -l x.lua | grep ETGLOBAL" gets you get a line number that is not at the beginning of the file, you found a typo (or a missing dependency) - you say more accurately what you want, which is that those functions be available *to you* (you typically don't care if they are also available to everyone else) - you can also use it for sandboxing if for some reason you can't or don't want to use separate environments -- just remove all unwanted globals after loading the module
That's why I prefer the above technique. Obviously avoiding additional globals in third-party modules is much more important, but others have already written about that.
Philipp